API_Microservices/sport/main.py

141 lines
3.9 KiB
Python
Raw Permalink Normal View History

2024-03-27 16:42:52 +01:00
import json
import os
from flask import Flask, request, jsonify
from pydantic import BaseModel
app = Flask(__name__)
SAVE_FILE: str = "./data/sport.json"
class Sport(BaseModel):
current_idx: int = 0
id: int
name: str
category: str
def modify_from_key(self, key, value):
if key == "name":
self.name = value
if key == "category":
self.category = value
def serialize(self):
return {
'id': self.id,
'name': self.name,
'category': self.category,
}
def load_sport_from_json(path: str) -> dict[int, Sport]:
if not os.path.isfile(path):
Sport.current_idx = 0
return {}
with open(path, "r") as file:
content = file.read()
list_test = json.loads(content)
if len(list_test) != 0:
Sport.current_idx = list_test[-1]['id']
else:
Sport.current_idx = 0
print(Sport.current_idx)
obj: dict[int, Sport] = {}
for element in list_test:
obj[int(element['id'])] = Sport(id=int(element['id']),
name=element['name'], category=element['category'])
return obj
def save_sport_in_json(path: str, sport_dict: dict[int, Sport]):
sport_list = []
for _, value in sport_dict.items():
sport_list.append(value.serialize())
with open(path, "w") as file:
file.write(json.dumps(sport_list))
@ app.route('/')
def root():
return jsonify({"name": "Jo API", "description": "An API to get information on 2024 olympic games"}), 200
@ app.route('/sport', methods=['GET', 'POST'])
def handleSport():
if request.method == 'POST':
return handleSportPOST()
else:
return handleSportGET()
def handleSportPOST():
sport_list: dict[int, Sport] = load_sport_from_json(SAVE_FILE)
try:
post_data = request.get_json()
id = Sport.current_idx + 1
name = post_data['name']
category = post_data['category']
sport = Sport(id=id, name=name, category=category)
sport_list[sport.id] = sport
Sport.current_idx += 1
save_sport_in_json(SAVE_FILE, sport_list)
except Exception:
return jsonify({"Message": "Bad request"}), 400
return jsonify({'id': sport.id}), 200
def handleSportGET():
sport_list: dict[int, Sport] = load_sport_from_json(SAVE_FILE)
ret: list(Sport) = []
for _, val in sport_list.items():
ret.append(val.serialize())
return jsonify(ret)
@ app.route('/sport/<id>', methods=['GET', 'PATCH', 'DELETE'])
def handleSportId(id: int):
if request.method == 'GET':
return handleSportIdGET(id)
elif request.method == 'PATCH':
return handleSportIdPATCH(id)
else:
return handleSportIdDELETE(id)
def handleSportIdGET(id: int):
sport_list: dict[int, Sport] = load_sport_from_json(SAVE_FILE)
try:
sport = sport_list[int(id)]
return jsonify(sport.serialize())
except Exception:
return jsonify({'Message': 'Not found'}), 404
def handleSportIdPATCH(id: int):
try:
sport_list: dict[int, Sport] = load_sport_from_json(
SAVE_FILE)
patch_data = request.get_json()
key_list = ['name', 'category']
print(patch_data)
_ = sport_list[int(id)]
for key, value in patch_data.items():
if key in key_list:
sport_list[int(id)].modify_from_key(key, value)
else:
return jsonify({'Message': 'Bad request'}), 400
save_sport_in_json(SAVE_FILE, sport_list)
return "OK", 200
except Exception:
return jsonify({'Message': 'Not found'}), 404
def handleSportIdDELETE(id: int):
sport_list: dict[int, Sport] = load_sport_from_json(SAVE_FILE)
try:
sport_list.pop(int(id))
save_sport_in_json(SAVE_FILE, sport_list)
except Exception:
return jsonify({"Message": "Not found"})
return "Ok"