-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (57 loc) · 2.34 KB
/
main.py
File metadata and controls
80 lines (57 loc) · 2.34 KB
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
# The purpose of this file is primarily to serve as a base for the API
# Most functionality should be represented in src/
from flask import Flask, request, jsonify, make_response
from flask_restful import Resource, Api, reqparse # reqparse allows for optional args
import pandas as pd
import json
from flask_cors import CORS
from src.college_retrieval import getCollegeByID
app = Flask(__name__)
CORS(app)
api = Api(app)
class College(Resource):
def get(self):
args = request.args
college_id = args['id']
college_id = int(college_id) # force the id to convert to int
print(f'Got id as {college_id}')
collegeDict = getCollegeByID(college_id, toJSON=True)
# return error if no college is found
if collegeDict is None:
return { 'message': f'College with id {college_id} not found.' }, 404
print(collegeDict)
return { 'data' : collegeDict }, 200 # return data and 200 (success) code
api.add_resource(College, '/college')
from src.location import getTopCollegeDictsInBox
class CollegesInBox(Resource):
def __init__(self):
self.reqparse = reqparse.RequestParser()
self.reqparse.add_argument('swLat', type = float)
self.reqparse.add_argument('swLon', type = float)
self.reqparse.add_argument('neLat', type = float)
self.reqparse.add_argument('neLon', type = float)
self.reqparse.add_argument('num', type = int, default = 10)
def get(self):
args = self.reqparse.parse_args()
swLat = args['swLat']
swLon = args['swLon']
neLat = args['neLat']
neLon = args['neLon']
num = args['num']
if (not swLat or not swLon or not neLat or not neLon):
return { 'message': 'At least one coordinate not provided.' }, 404
college_list = getTopCollegeDictsInBox(swLat, swLon, neLat, neLon, num)
return { 'data': college_list }, 200
api.add_resource(CollegesInBox, '/colleges_in_box')
from src.college_retrieval import getShortCollegeByID
class ShortCollege(Resource):
def get(self):
college_id = int(request.args['id'])
short_dict = getShortCollegeByID(college_id)
if not short_dict:
return { 'message': f'No college found with id {college_id}' }, 404
return { 'data': short_dict }, 200
api.add_resource(ShortCollege, '/short_college')
if __name__ == '__main__':
# run flask app on localhost:5000
app.run(host='localhost', port = 5000) # run our flask app