-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
165 lines (142 loc) · 3.82 KB
/
app.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
163
164
165
from flask import Flask, request
from database_helpers import should_save_data, save_data, get_nearby_data_default, get_nearby_data_self, get_containing_area
from flask import jsonify
app = Flask(__name__)
@app.route('/')
def index():
response = {
"API_INFORMATION": {
"post_location": {
"method": "POST",
"data": [
{
"lat": "float",
"long": "float",
"pincode": "string",
"city": "string",
"state": "string",
}
]
},
"get_using_postgres": {
"method": "GET",
"data": [
{
"lat": "float",
"long": "float",
"distance": "float",
}
]
},
"get_using_self": {
"method": "GET",
"data": [
{
"lat": "float",
"long": "float",
"distance": "float",
}
]
},
"get_containing_area": {
"method": "GET",
"data": [
{
"lat": "float",
"long": "float"
}
]
}
}
}
return jsonify(response)
@app.route('/post_location', methods=['POST'])
def postLocationHandler():
nearby_offset_distance = 5 # in km
response = {
"payload": {},
"status": {
"code": ''
}
}
if request.method == 'POST':
try:
_lat = float(request.form.get('lat'))
_long = float(request.form.get('long'))
_pincode = request.form.get('pincode')
_city = request.form.get('city')
_state = request.form.get('state')
if not _lat or not _long or not _pincode:
response['status']['code'] = 'VALIDATION_ERROR :: pincode, latitude and longitude are compulsary'
else:
shouldSave = False
shouldSave, response['payload'] = should_save_data(_pincode, _lat, _long, nearby_offset_distance)
if shouldSave:
response['status']['code'] = save_data(_pincode, _city, _state, _lat, _long)
else:
response['status']['code'] = 'ALREADY_EXISTS'
except:
response['status']['code'] = 'INCORRECT_DATA'
return jsonify(response)
@app.route('/get_using_postgres', methods=['GET'])
def getUsingPostgresHandler():
response = {
"payload": {},
"status": {
"code": ''
}
}
if request.method == 'GET':
try:
_lat = float(request.args.get('lat'))
_long = float(request.args.get('long'))
_distance = float(request.args.get('distance'))
_distance_meters = _distance * 1000
if not _lat or not _long or not _distance:
response['status']['code'] = 'VALIDATION_ERROR :: latitude, longitude and Distance are compulsary'
else:
response['status']['code'], response["payload"] = get_nearby_data_default(_lat, _long, _distance_meters)
except:
response['status']['code'] = 'INCORRECT_DATA'
return jsonify(response)
@app.route('/get_using_self', methods=['GET'])
def getUsingSelfHandler():
response = {
"payload": {},
"status": {
"code": ''
}
}
if request.method == 'GET':
try:
_lat = float(request.args.get('lat'))
_long = float(request.args.get('long'))
_distance = float(request.args.get('distance'))
if not _lat or not _long or not _distance:
response['status']['code'] = 'VALIDATION_ERROR :: latitude, longitude and Distance are compulsary'
else:
response['status']['code'], response["payload"] = get_nearby_data_self(_lat, _long, _distance)
except:
response['status']['code'] = 'INCORRECT_DATA'
return jsonify(response)
@app.route('/get_containing_area', methods=['GET'])
def getContainingAreaHandler():
response = {
"payload": {},
"status": {
"code": ''
}
}
if request.method == 'GET':
try:
_lat = float(request.args.get('lat'))
_long = float(request.args.get('long'))
if not _lat or not _long:
response['status']['code'] = 'VALIDATION_ERROR :: latitude and longitude are compulsary'
else:
response['status']['code'], response["payload"] = get_containing_area(_lat, _long)
except:
response['status']['code'] = 'INCORRECT_DATA'
return jsonify(response)
if __name__ == "__main__":
app.run(debug = True)