Commit Inicial
This commit is contained in:
commit
0f47953453
24 changed files with 1366 additions and 0 deletions
40
app/clientes/registry.py
Normal file
40
app/clientes/registry.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import logging
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from .base import ClienteBase
|
||||
|
||||
_CLIENTES: dict[str, ClienteBase] = {}
|
||||
|
||||
|
||||
def registrar(cliente: ClienteBase) -> ClienteBase:
|
||||
"""Chamado no fim do modulo de cada cliente (app/v1/<cliente>/cliente.py)."""
|
||||
chave = cliente.nome.lower()
|
||||
if chave in _CLIENTES:
|
||||
logging.warning(f"Cliente '{chave}' registrado mais de uma vez, sobrescrevendo")
|
||||
_CLIENTES[chave] = cliente
|
||||
return cliente
|
||||
|
||||
|
||||
def obter(nome: str) -> ClienteBase:
|
||||
if cliente := _CLIENTES.get(nome.lower()):
|
||||
return cliente
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"cliente '{nome}' nao atendido por esta API"
|
||||
)
|
||||
|
||||
|
||||
def listar() -> list[str]:
|
||||
return sorted(_CLIENTES)
|
||||
|
||||
|
||||
def get_cliente(cliente: str) -> ClienteBase:
|
||||
"""
|
||||
Dependencia do FastAPI. Resolve o cliente pelo path param da rota:
|
||||
|
||||
@router.post("/{cliente}/compras/dados")
|
||||
async def rota(cli: ClienteBase = Depends(get_cliente)): ...
|
||||
|
||||
O nome do parametro tem que ser `cliente` pra bater com o {cliente} da rota.
|
||||
"""
|
||||
return obter(cliente)
|
||||
Loading…
Add table
Add a link
Reference in a new issue