-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_handler.py
34 lines (31 loc) · 1.09 KB
/
api_handler.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
from csv_reader import CSVReader
from application import Application
from decorators import check_rate_limit
from flask import Flask, request,abort,make_response
from config import config
import json
app = Flask(__name__)
utils = Application()
@app.route("/")
def main():
return "Welcome!"
@app.route("/getCityHotels",methods=['POST'])
@check_rate_limit('api_key')
def get_hotels():
try:
request_body = json.loads(request.data)
city_id = request_body.get('city_id')
sort_type_value = request_body.get('sort_type')
meta_data = utils.get_city_hotels(city_id,sort_type=sort_type_value)
format_data = utils.format_data(meta_data,config.CSV_DATA.headers)
response = json.dumps(format_data)
return response
except KeyError:
response = "Key not found for {}".format(city_id)
abort(make_response(str(response), 404))
except Exception as e:
response = e.args
abort(make_response(str(response), 400))
if __name__ == "__main__":
config.CSV_DATA = CSVReader() # reads CSV while while starting the server
app.run()