Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (59 loc) · 1.71 KB
/
Copy pathmain.py
File metadata and controls
66 lines (59 loc) · 1.71 KB
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
from fastapi import FastAPI, Request
from app.routers import users
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from app.errors import AppError
app = FastAPI(
title="Training API",
version="1.0.0",
description="API interkoneksi data"
)
@app.exception_handler(AppError)
async def app_error_handler(request: Request, exc: AppError):
return JSONResponse(
status_code=exc.status_code,
content={
"success": False,
"error": {
"code": exc.code,
"message": exc.message,
"detail": exc.detail,
},
"meta": {
"path": str(request.url.path),
"request_id": request.headers.get("X-Request-ID", "")
},
},
)
@app.exception_handler(RequestValidationError)
async def validation_error_handler(request: Request, exc: RequestValidationError):
detail = []
for err in exc.errors():
loc = ".".join([str(x) for x in err.get("loc", []) if x != "body"])
detail.append({
"field": loc,
"issue": err.get("msg", "Validation failed")
})
return JSONResponse(
status_code=422,
content={
"success": False,
"error": {
"code": "VALIDATION_ERROR",
"message": "Input tidak valid",
"detail": detail,
},
"meta": {"path": str(request.url.path)},
},
)
app.include_router(
users.router,
prefix="/v1",
tags=["users"]
)
@app.get("/")
def root():
return {"message": "API is running"}
@app.get("/health")
def health_check():
return {"status": "healthy"}