first
This commit is contained in:
+53
-3
@@ -1,5 +1,18 @@
|
||||
import logging
|
||||
import os
|
||||
from fastapi import FastAPI
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
|
||||
from app.models import CreateCompanyRequest, CreateCompanyResponse
|
||||
from app.raynet_client import (
|
||||
RaynetAuthError,
|
||||
RaynetClient,
|
||||
RaynetError,
|
||||
RaynetValidationError,
|
||||
)
|
||||
|
||||
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
APP_NAME = os.getenv("APP_NAME", "raynet")
|
||||
APP_VERSION = os.getenv("APP_VERSION", "1.0.0")
|
||||
@@ -8,18 +21,55 @@ ROOT_PATH = os.getenv("ROOT_PATH", "")
|
||||
app = FastAPI(
|
||||
title=APP_NAME,
|
||||
version=APP_VERSION,
|
||||
root_path=ROOT_PATH
|
||||
root_path=ROOT_PATH,
|
||||
description="Stateless proxy nad RAYNET CRM API v2.",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/version")
|
||||
def version():
|
||||
return {
|
||||
"app": APP_NAME,
|
||||
"version": APP_VERSION,
|
||||
"language": "python",
|
||||
"root_path": ROOT_PATH
|
||||
"root_path": ROOT_PATH,
|
||||
}
|
||||
|
||||
|
||||
@app.post("/company", response_model=CreateCompanyResponse, tags=["company"])
|
||||
def create_company(
|
||||
request: CreateCompanyRequest,
|
||||
x_api_key: str = Header(
|
||||
...,
|
||||
alias="X-Api-Key",
|
||||
description="RAYNET API klíč (secret). Předává se výhradně hlavičkou.",
|
||||
),
|
||||
):
|
||||
"""Vytvoří firmu v RAYNET CRM.
|
||||
|
||||
- **X-Api-Key** (hlavička, secret): API klíč z RAYNET (Nastavení > API).
|
||||
- **email** / **instance_name** (tělo): identifikátory účtu a instance.
|
||||
- **company** (tělo): data firmy dle RAYNET CompanyInsertDto.
|
||||
"""
|
||||
try:
|
||||
with RaynetClient(
|
||||
api_key=x_api_key,
|
||||
email=request.email,
|
||||
instance_name=request.instance_name,
|
||||
) as client:
|
||||
# Posíláme jen vyplněná pole (žádné None), aby RAYNET nedostal prázdné hodnoty.
|
||||
data = request.company.model_dump(exclude_none=True)
|
||||
result = client.create_company(data)
|
||||
except RaynetAuthError as exc:
|
||||
raise HTTPException(status_code=401, detail=exc.message) from exc
|
||||
except RaynetValidationError as exc:
|
||||
raise HTTPException(status_code=400, detail=exc.message) from exc
|
||||
except RaynetError as exc:
|
||||
raise HTTPException(status_code=502, detail=exc.message) from exc
|
||||
|
||||
return CreateCompanyResponse(id=result.get("id"), success=True, raw=result)
|
||||
|
||||
Reference in New Issue
Block a user