-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
155 lines (128 loc) · 4.46 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
Sri Lanka Holidays API
Main module for the API - V0.1.10
Author: Dilshan-H (https://github.com/Dilshan-H)
License: MIT License
URL: https://github.com/Dilshan-H/srilanka-holidays
API endpoints.
- / (home page)
- /api/v1/status (status of the API)
- /api/v1/version (version of the API)
- /api/v1/coverage/{year} (data coverage for a given year)
- /api/v1/check_holiday (check whether a given date is a holiday or not)
- /api/v1/holiday_info (information about a given holiday)
- /api/v1/holidays (list of holidays for a given year/month)
Docs:
- /docs (Swagger UI)
- /redoc (ReDoc)
"""
# pylint: disable=import-error
from typing import Annotated
import os
from datetime import datetime, date
import json
from fastapi import FastAPI, Response, status, Path, Query
app = FastAPI()
@app.get("/")
async def root():
"""Return home page"""
# TODO: deliver home page
return {"response": "Under construction"}
@app.get("/api/v1/status")
async def api_status():
"""Return status of the API"""
return {"status": "ok"}
@app.get("/api/v1/version")
async def api_version():
"""Return version of the API"""
return {"version": "0.1.10"}
@app.get("/api/v1/coverage/{year}")
async def api_coverage_year(
year: Annotated[int, Path(title="The year to be checked", ge=2000, le=3000)]
):
"""Return current data coverage in the API for a given year"""
filename = f"json/{year}.json"
# check if data exists
if os.path.isfile(filename):
return {
"year": year,
"coverage": "ok",
}
return {
"year": year,
"coverage": "data not available",
}
@app.get("/api/v1/check_holiday")
async def check_holiday(
year: Annotated[int, Query(ge=2000, le=3000)],
month: Annotated[int, Query(ge=1, le=12)],
day: Annotated[int, Query(ge=1, le=31)],
response: Response,
):
"""Return whether a given date is a holiday or not"""
date_provided, status_code, result = await get_holiday_info(year, month, day)
if response:
response.status_code = status_code
if status_code != status.HTTP_200_OK:
return {"response": result}
if result["is_holiday"]:
return {"date": date_provided, "response": True}
return {"date": date_provided, "response": False}
@app.get("/api/v1/holiday_info")
async def holiday_info(
year: Annotated[int, Query(ge=2000, le=3000)],
month: Annotated[int, Query(ge=1, le=12)],
day: Annotated[int, Query(ge=1, le=31)],
response: Response,
):
"""Return information about a given holiday"""
date_provided, status_code, result = await get_holiday_info(year, month, day)
if response:
response.status_code = status_code
return {"date": date_provided, "response": result}
@app.get("/api/v1/holidays")
async def holidays_list():
"""Return list of holidays for a given year/month"""
return {"response": "Under construction"}
async def get_holiday_info(year: int, month: int, day: int):
"""Process provided date and return holiday information with status code"""
try:
date_to_check = date(year, month, day)
except (ValueError, TypeError):
return None, status.HTTP_400_BAD_REQUEST, {"error": "invalid date"}
filename = f"json/{year}.json"
try:
with open(filename, "r", encoding="utf-8") as file:
holiday_data = json.load(file)
except FileNotFoundError:
return (
date_to_check,
status.HTTP_404_NOT_FOUND,
{"error": "requested year not available"},
)
for holiday in holiday_data:
start_date = datetime.strptime(holiday["start"], "%Y-%m-%d").date()
end_date = datetime.strptime(holiday["end"], "%Y-%m-%d").date()
if start_date <= date_to_check < end_date:
return (
date_to_check,
status.HTTP_200_OK,
{
"day": date_to_check.strftime("%A"),
"week": date_to_check.strftime("%W"),
"month": date_to_check.strftime("%B"),
"is_holiday": True,
"id": holiday["uid"],
"holiday": holiday["summary"],
"type": holiday["catagories"],
"holiday_start": holiday["start"],
"holiday_end": holiday["end"],
},
)
return (
date_to_check,
status.HTTP_200_OK,
{
"is_holiday": False,
},
)