-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
162 lines (141 loc) · 3.87 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
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
156
157
158
159
160
161
162
from flask import Flask, jsonify
from flasgger import Swagger
from dynaconf.contrib import FlaskDynaconf
from game.games import Game, GameDoesNotExist
from parser import LogParser, MemoryGameRepository
app = Flask('game')
# setup settings
FlaskDynaconf(app)
# setup swagger
app.config['SWAGGER'] = {
'title': 'Game Documentation',
'description': 'Quake log parser',
'uiversion': 3,
'specs_route': '/',
}
swagger = Swagger(app)
game_repository = MemoryGameRepository()
def format_game_to_dict(game: Game) -> dict:
return {
'uid': game.uid,
'total_kills': game.total_kills,
'players': [player.name for player in game.players],
'kills': {player.name: player.kills for player in game.players},
}
@app.route('/ping')
def ping():
return 'pong!'
@app.route('/games', methods=['GET'])
def get_games():
"""Endpoint returning a list of Games.
---
definitions:
Game:
type: object
properties:
kills:
type: object
properties:
player_name:
type: int
description: Player's kills
players:
type: array
items:
type: string
description: A list of players' names
total_kills:
type: int
description: The total number of kills in the Game
uid:
type: string
format: uuid
description: Game's uid
responses:
200:
description: A list of Games
schema:
$ref: '#/definitions/Game'
examples:
{
"games": [
{
kills: {
Dono da Bola: 0,
Isgalamido: 1,
Mocinha: 0,
Zeh: 0
},
players: ["Zeh", "Mocinha", "Dono da Bola", "Isgalamido"],
total_kills: 4,
uid: "795bf0eb-5691-477d-ae91-856adb648385"
}
]
}
"""
games = game_repository.get_games()
response = []
for uid, game in games.items():
response.append(format_game_to_dict(game))
return jsonify({'games': response}), 200
@app.route('/games/<uid>', methods=['GET'])
def get_game_by_uid(uid):
"""Endpoint returning a Game by uid.
---
parameters:
- name: uid
in: path
type: string
required: true
definitions:
Game:
type: object
properties:
kills:
type: object
properties:
player_name:
type: int
description: Player's kills
players:
type: array
items:
type: string
description: A list of players' names
total_kills:
type: int
description: The total number of kills in the Game
uid:
type: string
format: uuid
description: Game's uid
responses:
200:
description: Game
schema:
$ref: '#/definitions/Game'
examples:
{
kills: {
Dono da Bola: 0,
Isgalamido: 1,
Mocinha: 0,
Zeh: 0
},
players: ["Zeh", "Mocinha", "Dono da Bola", "Isgalamido"],
total_kills: 4,
uid: "795bf0eb-5691-477d-ae91-856adb648385"
}
"""
try:
game = game_repository.get_game_by_uid(uid)
return jsonify(format_game_to_dict(game)), 200
except GameDoesNotExist:
response = {
'message': 'Game not found'
}
return jsonify(response), 404
if __name__ == '__main__':
parser = LogParser(game_repository)
parser.parse('./data/games.log')
app.run()