-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
49 lines (43 loc) · 1.67 KB
/
app.py
File metadata and controls
49 lines (43 loc) · 1.67 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
# pylint: disable=missing-module-docstring
# pylint: disable=missing-function-docstring
from datetime import date
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from fake_data_app import create_app
store_dict = create_app()
app = FastAPI()
@app.get("/")
def visit(
store_name: str, year: int, month: int, day: int, sensor_id: int | None = None
) -> JSONResponse:
# If the store is not in the dictionary
if store_name not in store_dict.keys():
return JSONResponse(status_code=404, content="Store Not found")
# Check the value of sensor_id
if sensor_id and (sensor_id > 7 or sensor_id < 0):
return JSONResponse(
status_code=404, content="Sensor_id should be between 0 and 7"
)
# Check the year
if year < 2019:
return JSONResponse(status_code=404, content="No data before 2019")
# Check the date
try:
date(year, month, day)
except TypeError:
return JSONResponse(status_code=404, content="Enter a valid date")
# Check the date is in the past
if date.today() < date(year, month, day):
return JSONResponse(status_code=404, content="Choose a date in the past")
# If no sensor choose return the visit for the whole store
if sensor_id is None:
visit_counts = store_dict[store_name].get_all_traffic(date(year, month, day))
else:
visit_counts = store_dict[store_name].get_sensor_traffic(
sensor_id, date(year, month, day)
)
if visit_counts < 0:
return JSONResponse(
status_code=404, content="The store was closed try another date"
)
return JSONResponse(status_code=200, content=visit_counts)