-
Notifications
You must be signed in to change notification settings - Fork 3
/
clients.py
138 lines (124 loc) · 3.79 KB
/
clients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Function to define FastAPI routes
"""
from fastapi import (
APIRouter,
Depends,
HTTPException
)
from sqlalchemy.orm import Session
from sqlalchemy.exc import OperationalError
from schemas import (
ClientsBase,
Clients
)
from responsesSettings import responsesMessages
import connection
import crud_clients
from observability import (
LOGGER,
tracer
)
LOGGER = LOGGER.getChild(__name__)
traces = tracer.get_tracer(__name__)
router = APIRouter(
tags=["clients"],
responses={
400: responsesMessages['400'],
403: responsesMessages['403'],
404: responsesMessages['404'],
405: responsesMessages['405'],
422: responsesMessages['422'],
500: responsesMessages['500']
}
)
def get_db():
"""
Database connexion
"""
db = connection.SessionLocal()
try:
LOGGER.debug("Open database connexion")
yield db
finally:
LOGGER.debug("Close database connexion")
db.close()
@router.post(path="/clients",
response_model=Clients,
status_code=201,
summary="Client creation"
)
async def post_client(client: ClientsBase, db: Session = Depends(get_db)):
"""
Create client with all parameters :
- **firstname**: Firstname of the client
- **lastname**: Lastname of the client
"""
with traces.start_as_current_span("Router GET client by ID"):
LOGGER.info("Start POST Client")
new_client = crud_clients.create(db=db, client=client)
return new_client
@router.get(path="/clients",
response_model=list[Clients],
status_code=200,
summary="List clients"
)
async def get_clients(db: Session = Depends(get_db)):
"""
List all clients
"""
with traces.start_as_current_span("Router GET clients List"):
LOGGER.info("Start GET Clients List")
try:
list_clients = crud_clients.get_list(db=db)
return list_clients
except OperationalError as error:
LOGGER.error(error)
return ""
@router.get(path="/clients/{client_id}",
response_model=Clients,
status_code=200,
summary="Details for specific client")
async def get_client(client_id: str, db: Session = Depends(get_db)):
"""
Details for specific client
"""
with traces.start_as_current_span("Router GET client by ID"):
LOGGER.info("Start GET Client by ID")
client = crud_clients.get_client(db=db, client_id=client_id)
LOGGER.debug(f"Return client : {str(client)}")
return client
@router.put(path="/clients/{client_id}",
response_model=Clients,
status_code=200,
summary="Modify details for a specific client")
async def put_client(client_id: str, client: ClientsBase, db: Session = Depends(get_db)):
"""
Modify details for a specific client :
- **firstname**: Firstname of the client
- **lastname**: Lastname of the client
"""
with traces.start_as_current_span("Router Modify clients by ID"):
LOGGER.info("Start PUT Client by ID")
try:
modify_client = crud_clients.put(db=db, client_id=client_id, client=client)
return modify_client
except OperationalError as error:
LOGGER.error(error)
return ""
@router.delete(path="/clients/{client_id}",
response_model=None,
status_code=204,
summary="Delete a specific client"
)
async def delete_client(client_id: str, db: Session = Depends(get_db)):
"""
Delete a specific client
"""
with traces.start_as_current_span("Router DELETE clients by ID"):
LOGGER.info("Start DELETE Client by ID")
delete_client = crud_clients.get_client(db=db, client_id=client_id)
if delete_client is None:
raise HTTPException(status_code=405)
crud_clients.delete(db=db, client_id=client_id)
return None