61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
from fastapi import FastAPI, HTTPException, APIRouter, Request, Header, Depends
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.templating import Jinja2Templates
|
|
from typing import Dict
|
|
from pydantic import BaseModel
|
|
from sqlalchemy.orm import Session
|
|
import json
|
|
from model.database import get_async_session, Client
|
|
from utils.clients import upsert_client
|
|
from typing import Union
|
|
|
|
router = APIRouter()
|
|
templates = Jinja2Templates(directory="templates")
|
|
API_KEY = "4545454"
|
|
|
|
# Пример данных
|
|
clients = {
|
|
1: {"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: Union[dict, str]
|
|
|
|
|
|
@router.post("/client/")
|
|
async def client(data: JsonData, x_api_key: str = Header(...), db: Session = Depends(get_async_session)):
|
|
if x_api_key != "4545454":
|
|
raise HTTPException(status_code=403, detail="Invalid API Key")
|
|
|
|
# Если json_data строка — декодируем
|
|
if isinstance(data.json_data, str):
|
|
try:
|
|
data.json_data = json.loads(data.json_data)
|
|
|
|
|
|
except json.JSONDecodeError:
|
|
raise HTTPException(status_code=400, detail="Invalid JSON format")
|
|
|
|
print("Полученные данные:", data.json_data)
|
|
return {"message": "JSON получен", "data": "ok"}
|
|
|
|
|
|
|
|
|
|
# first_name = parsed_data['first_name']
|
|
# last_name = parsed_data['last_name']
|
|
# email_addr = parsed_data['email_addr']
|
|
# user_id = parsed_data['user_id']
|
|
# phone_num = parsed_data['phone_num']
|
|
|
|
# print(first_name)
|
|
# # data = await upsert_client(db, first_name, last_name, email_addr, parsed_data)
|
|
# client = await upsert_client(db, user_id, first_name, last_name, email_addr, phone_num, str(parsed_data))
|
|
# print(f"Received data: data={client}") |