-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
54 lines (46 loc) · 1.54 KB
/
api.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
# Fast api
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.exceptions import HTTPException as FastHTTPException
from starlette.exceptions import HTTPException as StarletteHTTPException
from pydantic import BaseModel
from color_analyser import anaylyze_and_save_report
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:8080",
"http://localhost:3000",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return JSONResponse(
status_code=422,
content=jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code, content=jsonable_encoder({"detail": exc.detail})
)
@app.exception_handler(FastHTTPException)
async def fast_http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code, content=jsonable_encoder({"detail": exc.detail})
)
class UrlBody(BaseModel):
url: str
@app.post('/api/v1/analyze')
async def color_analyse(requestBody: UrlBody):
return {
'filePath': anaylyze_and_save_report(requestBody.url)
}