linkedin2/routers/client.py

38 lines
1.5 KiB
Python

from fastapi import FastAPI, HTTPException, APIRouter, Request, Header
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
from typing import Dict
from pydantic import BaseModel
import json
router = APIRouter()
templates = Jinja2Templates(directory="templates")
API_KEY = "4545454"
# Пример данных
clients = {
27: {"username": "John Doe", "email": "john@example.com", "phone": "+123456781"},
44: {"username": "Jane Smith", "email": "jane@example.com", "phone": "+987654321"}
}
@router.get("/get_client/{client_id}", include_in_schema=False)
async def get_client_modal(client_id: int):
client = clients.get(client_id) # Используй clients вместо clients_db
if not client:
raise HTTPException(status_code=404, detail="Client not found")
return JSONResponse(content=client)
class JsonData(BaseModel):
json_data: str
@router.post("/client/")
async def client(data: JsonData, x_api_key: str = Header(None)):
# Проверяем API-ключ
if x_api_key != "4545454":
raise HTTPException(status_code=403, detail="Invalid API Key")
try:
parsed_data = json.loads(data.json_data) # Декодируем JSON
print("Полученные данные:", parsed_data)
return {"message": "JSON получен", "data": parsed_data}
except json.JSONDecodeError as e:
raise HTTPException(status_code=400, detail=f"Invalid JSON: {str(e)}")