Skip to content

Commit aaead4c

Browse files
committed
More responses
1 parent 25a51e2 commit aaead4c

7 files changed

Lines changed: 77 additions & 66 deletions

File tree

app/api/v1/bankomat.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
TopupRequest,
2929
TransferRequest,
3030
)
31-
from app.schemas.common import MessageResponse, TopupResponse
31+
from app.schemas.common import HTTP_400, HTTP_402, HTTP_403, HTTP_404, HTTP_409, MessageResponse, TopupResponse
3232
from app.schemas.transaction import TransactionResponse
3333
from app.schemas.user import UserPinVerify
3434

@@ -229,7 +229,7 @@ def list_targets(
229229
return db.query(BookingTarget).order_by(BookingTarget.name).all()
230230

231231

232-
@router.post("/targets", response_model=BookingTargetResponse, status_code=201)
232+
@router.post("/targets", response_model=BookingTargetResponse, status_code=201, responses={**HTTP_409})
233233
def create_target(
234234
body: BookingTargetCreate,
235235
admin: dict = Depends(require_admin_user),
@@ -246,7 +246,7 @@ def create_target(
246246

247247
# --- ATM operations ---
248248

249-
@router.post("/topup", response_model=TopupResponse)
249+
@router.post("/topup", response_model=TopupResponse, responses={**HTTP_400, **HTTP_404})
250250
def topup_user(
251251
body: TopupRequest,
252252
device: Machine = Depends(get_current_device),
@@ -283,7 +283,7 @@ def topup_user(
283283
return {"detail": f"Topped up {body.amount} {settings.CURRENCY}. New balance: {user.balance} {settings.CURRENCY}", "balance": user.balance}
284284

285285

286-
@router.post("/target-topup", response_model=MessageResponse)
286+
@router.post("/target-topup", response_model=MessageResponse, responses={**HTTP_404})
287287
def topup_target_only(
288288
body: TargetTopupRequest,
289289
device: Machine = Depends(get_current_device),
@@ -326,7 +326,7 @@ def user_transactions(
326326
)
327327

328328

329-
@router.post("/transfer", response_model=MessageResponse)
329+
@router.post("/transfer", response_model=MessageResponse, responses={**HTTP_400, **HTTP_402, **HTTP_404})
330330
def transfer(
331331
body: TransferRequest,
332332
device: Machine = Depends(get_current_device),
@@ -383,7 +383,7 @@ def transfer(
383383
return {"detail": f"Transferred {body.amount} {settings.CURRENCY} from {body.from_nfc_id} to {body.to_nfc_id}"}
384384

385385

386-
@router.post("/verify-pin", response_model=MessageResponse)
386+
@router.post("/verify-pin", response_model=MessageResponse, responses={**HTTP_403, **HTTP_404})
387387
def verify_pin(
388388
body: UserPinVerify,
389389
device: Machine = Depends(get_current_device),
@@ -400,7 +400,7 @@ def verify_pin(
400400
return {"detail": "PIN valid"}
401401

402402

403-
@router.post("/payout", response_model=MessageResponse)
403+
@router.post("/payout", response_model=MessageResponse, responses={**HTTP_400, **HTTP_402, **HTTP_403, **HTTP_404})
404404
def payout(
405405
body: PayoutRequest,
406406
device: Machine = Depends(get_current_device),
@@ -442,7 +442,7 @@ def payout(
442442

443443
# --- PIN management ---
444444

445-
@router.post("/pin", response_model=MessageResponse)
445+
@router.post("/pin", response_model=MessageResponse, responses={**HTTP_404})
446446
def set_pin(
447447
body: SetPinRequest,
448448
admin: dict = Depends(require_admin_user),
@@ -457,7 +457,7 @@ def set_pin(
457457
return {"detail": "PIN updated"}
458458

459459

460-
@router.get("/statement/{target_slug}", response_class=Response)
460+
@router.get("/statement/{target_slug}", response_class=Response, responses={**HTTP_400, **HTTP_404})
461461
def get_statement(
462462
target_slug: str,
463463
from_year: int = Query(...),
@@ -522,7 +522,7 @@ def get_statement(
522522
raise HTTPException(500, f"PDF generation failed: {e}") from e
523523

524524

525-
@router.get("/statement-all", response_class=Response)
525+
@router.get("/statement-all", response_class=Response, responses={**HTTP_400})
526526
def get_statement_all(
527527
from_year: int = Query(...),
528528
from_month: int = Query(...),

app/api/v1/machines.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
MachineUpdate,
2929
MachineUserSummary,
3030
)
31-
from app.schemas.common import MessageResponse
31+
from app.schemas.common import HTTP_403, HTTP_404, HTTP_409, MessageResponse
3232

3333
router = APIRouter()
3434

@@ -41,7 +41,7 @@ def list_machines(
4141
return db.query(Machine).order_by(Machine.name).all()
4242

4343

44-
@router.post("", response_model=MachineCreateResponse, status_code=201)
44+
@router.post("", response_model=MachineCreateResponse, status_code=201, responses={**HTTP_409})
4545
def register_machine(
4646
body: MachineCreate,
4747
admin: dict = Depends(require_admin_user),
@@ -84,7 +84,7 @@ def list_my_machines(
8484
)
8585

8686

87-
@router.get("/{slug}", response_model=MachineResponse)
87+
@router.get("/{slug}", response_model=MachineResponse, responses={**HTTP_404})
8888
def get_machine(
8989
slug: str,
9090
request: Request,
@@ -94,7 +94,7 @@ def get_machine(
9494
return machine
9595

9696

97-
@router.put("/{slug}", response_model=MachineResponse)
97+
@router.put("/{slug}", response_model=MachineResponse, responses={**HTTP_404, **HTTP_409})
9898
def update_machine(
9999
slug: str,
100100
body: MachineUpdate,
@@ -119,7 +119,7 @@ def update_machine(
119119
return machine
120120

121121

122-
@router.delete("/{slug}", response_model=MessageResponse)
122+
@router.delete("/{slug}", response_model=MessageResponse, responses={**HTTP_404})
123123
def deactivate_machine(
124124
slug: str,
125125
admin: dict = Depends(require_admin_user),
@@ -133,7 +133,7 @@ def deactivate_machine(
133133
return {"detail": "Machine deactivated"}
134134

135135

136-
@router.post("/{slug}/token", response_model=MachineCreateResponse)
136+
@router.post("/{slug}/token", response_model=MachineCreateResponse, responses={**HTTP_404})
137137
def regenerate_token(
138138
slug: str,
139139
admin: dict = Depends(require_admin_user),
@@ -152,7 +152,7 @@ def regenerate_token(
152152

153153
# --- Machine admins (by OIDC sub) ---
154154

155-
@router.get("/{slug}/admins", response_model=list[MachineAdminResponse])
155+
@router.get("/{slug}/admins", response_model=list[MachineAdminResponse], responses={**HTTP_404})
156156
def list_machine_admins(
157157
slug: str,
158158
request: Request,
@@ -171,7 +171,7 @@ def list_machine_admins(
171171
return result
172172

173173

174-
@router.post("/{slug}/admins", response_model=MachineAdminResponse, status_code=201)
174+
@router.post("/{slug}/admins", response_model=MachineAdminResponse, status_code=201, responses={**HTTP_404, **HTTP_409})
175175
def add_machine_admin(
176176
slug: str,
177177
body: MachineAdminCreate,
@@ -197,7 +197,7 @@ def add_machine_admin(
197197
return {"machine_id": entry.machine_id, "oidc_sub": entry.oidc_sub, "user_name": linked_user.name if linked_user else None}
198198

199199

200-
@router.delete("/{slug}/admins/{oidc_sub}", response_model=MessageResponse)
200+
@router.delete("/{slug}/admins/{oidc_sub}", response_model=MessageResponse, responses={**HTTP_404})
201201
def remove_machine_admin(
202202
slug: str,
203203
oidc_sub: str,
@@ -221,7 +221,7 @@ def remove_machine_admin(
221221

222222
# --- Session history ---
223223

224-
@router.get("/{slug}/sessions", response_model=list[MachineSessionResponse])
224+
@router.get("/{slug}/sessions", response_model=list[MachineSessionResponse], responses={**HTTP_404})
225225
def list_sessions(
226226
slug: str,
227227
request: Request,
@@ -273,7 +273,7 @@ def list_sessions(
273273

274274
# --- Users (for authorization dropdown) ---
275275

276-
@router.get("/{slug}/users", response_model=list[MachineUserSummary])
276+
@router.get("/{slug}/users", response_model=list[MachineUserSummary], responses={**HTTP_404})
277277
def list_machine_users(
278278
slug: str,
279279
request: Request,
@@ -286,7 +286,7 @@ def list_machine_users(
286286

287287
# --- Authorizations ---
288288

289-
@router.get("/{slug}/authorizations", response_model=list[AuthorizationResponse])
289+
@router.get("/{slug}/authorizations", response_model=list[AuthorizationResponse], responses={**HTTP_404})
290290
def list_authorizations(
291291
slug: str,
292292
request: Request,
@@ -299,7 +299,7 @@ def list_authorizations(
299299
]
300300

301301

302-
@router.post("/{slug}/authorizations", response_model=AuthorizationResponse, status_code=201)
302+
@router.post("/{slug}/authorizations", response_model=AuthorizationResponse, status_code=201, responses={**HTTP_404, **HTTP_409})
303303
def grant_authorization(
304304
slug: str,
305305
body: AuthorizationCreate,
@@ -335,7 +335,7 @@ def grant_authorization(
335335
return auth
336336

337337

338-
@router.put("/{slug}/authorizations/{nfc_id}", response_model=AuthorizationResponse)
338+
@router.put("/{slug}/authorizations/{nfc_id}", response_model=AuthorizationResponse, responses={**HTTP_404})
339339
def update_authorization(
340340
slug: str,
341341
nfc_id: int,
@@ -365,7 +365,7 @@ def update_authorization(
365365
return auth
366366

367367

368-
@router.delete("/{slug}/authorizations/{nfc_id}", response_model=MessageResponse)
368+
@router.delete("/{slug}/authorizations/{nfc_id}", response_model=MessageResponse, responses={**HTTP_404})
369369
def revoke_authorization(
370370
slug: str,
371371
nfc_id: int,
@@ -388,7 +388,7 @@ def revoke_authorization(
388388
return {"detail": "Authorization revoked"}
389389

390390

391-
@router.get("/{slug}/authorize/{nfc_id}", response_model=AuthorizeUserResponse)
391+
@router.get("/{slug}/authorize/{nfc_id}", response_model=AuthorizeUserResponse, responses={**HTTP_403, **HTTP_404})
392392
def check_authorization(
393393
slug: str,
394394
nfc_id: int,

app/api/v1/products.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from app.models.product import Product, ProductAlias, ProductAudit, ProductAuditType, ProductCategory
1919
from app.models.transaction import Transaction, TransactionType
2020
from app.models.user import User
21-
from app.schemas.common import MessageResponse
21+
from app.schemas.common import HTTP_400, HTTP_402, HTTP_404, HTTP_409, MessageResponse
2222
from app.schemas.product import (
2323
CategoryCreate,
2424
ProductAliasCreate,
@@ -80,7 +80,7 @@ def list_categories(db: Session = Depends(get_db)):
8080
return sorted(from_table | from_products)
8181

8282

83-
@router.post("/categories", response_model=str, status_code=201)
83+
@router.post("/categories", response_model=str, status_code=201, responses={**HTTP_400, **HTTP_409})
8484
def create_category(
8585
body: CategoryCreate,
8686
user: dict = Depends(require_product_manager_user),
@@ -97,7 +97,7 @@ def create_category(
9797
return name
9898

9999

100-
@router.delete("/categories/{name}", response_model=MessageResponse)
100+
@router.delete("/categories/{name}", response_model=MessageResponse, responses={**HTTP_404, **HTTP_409})
101101
def delete_category(
102102
name: str,
103103
user: dict = Depends(require_product_manager_user),
@@ -114,13 +114,13 @@ def delete_category(
114114
return {"detail": f"Category '{name}' deleted"}
115115

116116

117-
@router.get("/products/{ean}", response_model=ProductDetailResponse)
117+
@router.get("/products/{ean}", response_model=ProductDetailResponse, responses={**HTTP_404})
118118
def get_product(ean: str, db: Session = Depends(get_db)):
119119
product = _resolve_product(ean, db)
120120
return product
121121

122122

123-
@router.post("/products", response_model=ProductResponse, status_code=201)
123+
@router.post("/products", response_model=ProductResponse, status_code=201, responses={**HTTP_409})
124124
def create_product(
125125
body: ProductCreate,
126126
user: dict = Depends(require_product_manager_user),
@@ -148,7 +148,7 @@ def create_product(
148148
return product
149149

150150

151-
@router.put("/products/{ean}", response_model=ProductResponse)
151+
@router.put("/products/{ean}", response_model=ProductResponse, responses={**HTTP_404})
152152
def update_product(
153153
ean: str,
154154
body: ProductUpdate,
@@ -192,7 +192,7 @@ def update_product(
192192
return product
193193

194194

195-
@router.post("/products/{ean}/stock", response_model=ProductResponse)
195+
@router.post("/products/{ean}/stock", response_model=ProductResponse, responses={**HTTP_400, **HTTP_404})
196196
def adjust_stock(
197197
ean: str,
198198
body: ProductStockAdjust,
@@ -218,7 +218,7 @@ def adjust_stock(
218218
return product
219219

220220

221-
@router.post("/products/{ean}/stocktaking", response_model=ProductResponse)
221+
@router.post("/products/{ean}/stocktaking", response_model=ProductResponse, responses={**HTTP_400, **HTTP_404})
222222
def stocktaking(
223223
ean: str,
224224
body: ProductStocktaking,
@@ -243,7 +243,7 @@ def stocktaking(
243243
return product
244244

245245

246-
@router.get("/products/{ean}/audit", response_model=list[ProductAuditResponse])
246+
@router.get("/products/{ean}/audit", response_model=list[ProductAuditResponse], responses={**HTTP_404})
247247
def get_product_audit(
248248
ean: str,
249249
user: dict = Depends(require_product_manager_user),
@@ -258,7 +258,7 @@ def get_product_audit(
258258
)
259259

260260

261-
@router.get("/products/{ean}/popularity", response_model=ProductPopularityResponse)
261+
@router.get("/products/{ean}/popularity", response_model=ProductPopularityResponse, responses={**HTTP_404})
262262
def product_popularity(
263263
ean: str,
264264
days: int = Query(default=7, ge=1, le=365),
@@ -287,13 +287,13 @@ def product_popularity(
287287

288288
# --- Aliases ---
289289

290-
@router.get("/products/{ean}/aliases", response_model=list[ProductAliasResponse])
290+
@router.get("/products/{ean}/aliases", response_model=list[ProductAliasResponse], responses={**HTTP_404})
291291
def list_aliases(ean: str, db: Session = Depends(get_db)):
292292
product = _resolve_product(ean, db)
293293
return product.aliases
294294

295295

296-
@router.post("/products/{ean}/aliases", response_model=ProductAliasResponse, status_code=201)
296+
@router.post("/products/{ean}/aliases", response_model=ProductAliasResponse, status_code=201, responses={**HTTP_404, **HTTP_409})
297297
def add_alias(
298298
ean: str,
299299
body: ProductAliasCreate,
@@ -312,7 +312,7 @@ def add_alias(
312312
return alias
313313

314314

315-
@router.delete("/products/{ean}/aliases/{alias_ean}", response_model=MessageResponse)
315+
@router.delete("/products/{ean}/aliases/{alias_ean}", response_model=MessageResponse, responses={**HTTP_404})
316316
def delete_alias(
317317
ean: str,
318318
alias_ean: str,
@@ -334,7 +334,7 @@ def delete_alias(
334334

335335
# --- Purchase (checkout device) ---
336336

337-
@router.post("/products/{ean}/purchase", response_model=PurchaseResponse)
337+
@router.post("/products/{ean}/purchase", response_model=PurchaseResponse, responses={**HTTP_400, **HTTP_402, **HTTP_404})
338338
def purchase_product(
339339
ean: str,
340340
body: PurchaseBody,

0 commit comments

Comments
 (0)