File size: 731 Bytes
2d2c435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
import os
from typing import Dict, Any

DATA_DIR = 'data'
REQUESTS_FILE = os.path.join(DATA_DIR, 'requests.json')

os.makedirs(DATA_DIR, exist_ok=True)


def _load_all():
    if not os.path.exists(REQUESTS_FILE):
        return []
    with open(REQUESTS_FILE, 'r') as f:
        return json.load(f)


def _save_all(items):
    with open(REQUESTS_FILE, 'w') as f:
        json.dump(items, f, indent=2, ensure_ascii=False)


def create_request(payload: Dict[str, Any]) -> Dict[str, Any]:
    items = _load_all()
    request_id = len(items) + 1
    payload['id'] = request_id
    payload['status'] = 'pending'
    items.append(payload)
    _save_all(items)
    return payload


def list_requests():
    return _load_all()