-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
74 lines (62 loc) · 2.43 KB
/
controller.py
File metadata and controls
74 lines (62 loc) · 2.43 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
import validations
from database import Mongo
from apis import Google
def get_address(request):
"""
This function follows the following process:
1. Validates if the parameters of the request is valid.
2. Checks the cache if an existing address is available based on the
provided latitute & longitude.
3a. If no address exists in the cache, get the address from Google API.
3b. Store the result from Google into the cache.
4. Return the result as response.
"""
# Validate if request is complete
valid, code, response = validate_request(request)
if not valid:
return code, response
# Extract parameters from request
coordinates = request.args.get('latlng').split(',')
latitude = coordinates[0]
longitude = coordinates[1]
key = request.args.get('key')
# Check cache for address
db = Mongo()
result = db.find_result(latitude, longitude)
if result:
return 200, result
# Check Google API for address
api = Google()
result = api.get_result(latitude, longitude, key)
# Store Google API results into MongoDB
db.store_result(latitude, longitude, result)
return 200, result
def validate_request(request):
# Check if latlng parameter exists
validity, code, response = validations.check_parameter_existence(
request, 'latlng')
if validity is False:
return validity, code, response
# Check if latlng can be split into latitude & longitude coordinates
validity, code, response = validations.check_comma_delimited(
request, 'latlng')
if validity is False:
return validity, code, response
# Check if latitude & longitude values are valid
coordinates = request.args.get('latlng').split(',')
latitude = coordinates[0]
longitude = coordinates[1]
validity, code, response = validations.check_non_zero(latitude, 'latlng')
if validity is False:
return validity, code, response
validity, code, response = validations.check_non_zero(longitude, 'latlng')
if validity is False:
return validity, code, response
# Coordinates should be numerical
# validity, code, response = validations.check_numeric(latitude, 'latlng')
# if validity is False:
# return validity, code, response
# validity, code, response = validations.check_numeric(longitude, 'latlng')
# if validity is False:
# return validity, code, response
return True, None, None