40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
from fastapi import FastAPI, HTTPException
|
|
from uuid import uuid4
|
|
|
|
app = FastAPI()
|
|
|
|
# Временное хранилище для клиентов
|
|
clients = {}
|
|
|
|
@app.get("/")
|
|
def get_clients():
|
|
return list(clients.values())
|
|
|
|
@app.get("/{client_id}")
|
|
def get_client(client_id: str):
|
|
if client_id not in clients:
|
|
raise HTTPException(status_code=404, detail="Client not found")
|
|
return clients[client_id]
|
|
|
|
@app.post("/")
|
|
def create_client(client: dict):
|
|
client_id = str(uuid4())
|
|
new_client = {"id": client_id, **client}
|
|
clients[client_id] = new_client
|
|
return new_client
|
|
|
|
@app.put("/{client_id}")
|
|
def update_client(client_id: str, client: dict):
|
|
if client_id not in clients:
|
|
raise HTTPException(status_code=404, detail="Client not found")
|
|
updated_client = {"id": client_id, **client}
|
|
clients[client_id] = updated_client
|
|
return updated_client
|
|
|
|
@app.delete("/{client_id}")
|
|
def delete_client(client_id: str):
|
|
if client_id not in clients:
|
|
raise HTTPException(status_code=404, detail="Client not found")
|
|
del clients[client_id]
|
|
return {"detail": "Client deleted successfully"}
|