-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathufc_api.py
104 lines (89 loc) · 3.49 KB
/
ufc_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
from flask import Flask, jsonify, abort, make_response, request, url_for
from flask_restful import Resource, Api
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_marshmallow import Marshmallow
from config import Config, DevConfig, ProdConfig
import os,sys, json
app = Flask(__name__)
app.config.from_object(Config)
if os.environ.get('FP_CONFIG',"DEV") == 'PROD':
print("production")
app.config.from_object(ProdConfig)
else:
print("dev")
app.config.from_object(DevConfig)
app.debug = app.config['DEBUG']
api=Api(app)
db = SQLAlchemy(app)
migrate = Migrate(app,db)
ma = Marshmallow(app)
cors = CORS(app)
from models import Bouts,Events,BoutSchema,EventSchema
class PredictionsResource(Resource):
"""
for the predictions module
"""
def get(self):
with open(app.config['ROUTES']['basedir'] + '/pred_fights.json') as jf:
data = json.load(jf)
return make_response(jsonify(data), 200)
class EventResouce(Resource):
"""
Get a specfic event
"""
def get(self,event_id):
msg = {}
if not isinstance(event_id, int):
msg['Error'] = "Invalid event_id. Should be int"
return make_response(msg,400)
elif len(str(event_id)) >= 6:
msg['Error'] = "Invalid event_id."
return make_response(msg,400)
top_keys = set(['red_fighter','blue_fighter','winner','loser','result','time','method','end_round','event_id','blue_stats','red_stats'])
bout_schema = BoutSchema(only=top_keys)
event_schema = EventSchema()
print("Recieved " + str(event_id), file=sys.stderr)
rows = Bouts.query.filter_by(event_id=str(event_id)).all()
event = Events.query.filter_by(id=event_id).first()
msg['event'] = event_schema.dump(event)
msg['bouts'] = []
for bout in rows:
#print(bout_schema.dump(bout),file=sys.stderr)
msg['bouts'].append(bout_schema.dump(bout))
return make_response(jsonify(msg),200)
class EventsResource(Resource):
"""
Get all events with their corresponding id
param: all gets all of the events, regardless if there are bouts available for them
exist: only gets events that have bouts available
"""
def get(self, param):
msg = {}
if not isinstance(param, str):
msg['Error'] = "Invalid param. Should be string."
return make_response(msg,400)
elif len(param) >= 64:
msg['Error'] = "Invalid param."
return make_response(msg,400)
event_schema = EventSchema()
msg['events'] = []
error_code = 200
event_rows = Events.query.filter_by().all()
if param == "all":
for event in event_rows:
msg['events'].append(event_schema.dump(event))
else:
msg['Error'] = "Invalid url, param needs to be all or existing."
error_code = 400
return make_response(jsonify(msg),error_code)
api.add_resource(PredictionsResource,'/predictions')
api.add_resource(EventResouce,'/event/<int:event_id>')
api.add_resource(EventsResource,'/events/<string:param>')
@app.errorhandler(405)
def request_not_supported(e):
return("this method is unsupported"), 405
if __name__ == '__main__':
print(app.config)
app.run(host=app.config['HOST'], debug=app.config['DEBUG'])