-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrummolo_api.py
61 lines (47 loc) · 1.7 KB
/
scrummolo_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
55
56
57
58
59
60
61
"""API for scrummolo web."""
import dataclasses
import uuid
from flask import Flask, request, make_response
from pydantic import ValidationError
from scrummolo.repository.mem_repository import MemRepository
from scrummolo.requests.team_request import TeamRequest
from scrummolo.use_cases.add_team_use_case import add_team
from scrummolo.use_cases.find_team_by_id_use_case import find_team_by_id
from scrummolo.use_cases.find_teams_by_user_use_case import find_teams_by_user
app = Flask(__name__)
db = MemRepository(None)
@app.route("/teams/<string:user>", methods=["GET"])
def get_teams(user):
"""Returns a team resources."""
teams = find_teams_by_user(db, user)
return {"teams": teams}
@app.route("/team/<string:team_id>", methods=["GET"])
def get_team(team_id):
"""Returns a team resource given an id."""
try:
team = find_team_by_id(db, team_id)
return {"team": team}
except IndexError:
return {"message": "Not found"}, 404
@app.route("/team", methods=["POST"])
def create_team():
"""Creates a team resource."""
try:
req = request.json
req["team_id"] = str(uuid.uuid4())
parsed_req = TeamRequest(**req)
parsed_dict = dataclasses.asdict(parsed_req)
add_team(db, parsed_dict)
return {"team": parsed_dict}, 201
except TypeError as te:
return {"message": str(te)}, 400
except ValidationError as ve:
return {"message": str(ve)}, 400
except ValueError as ve:
return {"message": str(ve)}, 403
@app.errorhandler(404)
def not_found(error):
"""Returns a not found as json response."""
return make_response({"error": "Not found"}, 404)
if __name__ == "__main__":
app.run(debug=True)