Skip to content

Commit 8d8595e

Browse files
committed
cleaning and refactoring
1 parent 120d972 commit 8d8595e

19 files changed

Lines changed: 325 additions & 354 deletions

File tree

geolocation/api.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

geolocation/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
import requests
3+
from geolocation.validators import ResponseValidator
4+
5+
6+
class ApiClient(object):
7+
"""
8+
Client class:
9+
- sends request to API
10+
- returns data from API
11+
- handles exceptions
12+
All clients should inheritance by this class.
13+
"""
14+
api_key = None # api key for google maps.
15+
validator = ResponseValidator
16+
17+
def __repr__(self):
18+
return '<ApiClient: %s>' % self.api_key
19+
20+
def send_data(self, url, params=None):
21+
"""Method class url api and returns json data."""
22+
response = requests.get(url, params=params)
23+
self.validator(response)
24+
25+
return response.json()

geolocation/const.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

geolocation/distance_matrix/api.py

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
from decimal import Decimal
3+
from geolocation.client import ApiClient
4+
5+
6+
class DistanceMatrixApiClient(ApiClient):
7+
"""
8+
Client Class for Distance Matrix Google Maps Api.
9+
Field query_parameters has keys:
10+
- origins (multi origins are separated by '|')
11+
- destinations (multi destinations are separated by '|')
12+
- mode
13+
- avoid
14+
Documentation: https://developers.google.com/maps/documentation/distance-matrix/
15+
"""
16+
API_URL = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
17+
18+
ONE_MILE = Decimal(0.62137) # km units
19+
ONE_FEET = Decimal(3.2808)
20+
ONE_KILOMETER = Decimal(1000.00) # m units
21+
22+
UNIT_KM = 'km'
23+
UNIT_M = 'm'
24+
25+
MODE_DRIVING = 'driving'
26+
MODE_WALKING = 'walking'
27+
MODE_BICYCLING = 'bicycling'
28+
MODE_TRANSIT = 'transit'
29+
30+
MODES = (
31+
(MODE_DRIVING, 'driving'),
32+
(MODE_WALKING, 'walking'),
33+
(MODE_BICYCLING, 'bicycling'),
34+
(MODE_TRANSIT, 'transit')
35+
)
36+
37+
AVOID_TOLLS = 'tools'
38+
AVOID_HIGHWAYS = 'highways'
39+
AVOID_FERRIES = 'ferries'
40+
41+
AVOIDS = (
42+
(AVOID_TOLLS, 'tools'),
43+
(AVOID_HIGHWAYS, 'highways'),
44+
(AVOID_FERRIES, 'ferries')
45+
)
46+
47+
query_parameters = {}
48+
49+
def __repr__(self):
50+
return '<DistanceMatrixApiClient: %s>' % self.api_key
51+
52+
def set_query_parameters(self, origins, destinations, mode=MODE_DRIVING, avoid=None):
53+
"""Method sets values for query_parameters."""
54+
def prepare_multi_params(value):
55+
return '|'.join(value) if isinstance(value, list) else value
56+
57+
self.query_parameters['origins'] = prepare_multi_params(origins)
58+
self.query_parameters['destinations'] = prepare_multi_params(destinations)
59+
self.query_parameters['mode'] = mode
60+
61+
if avoid:
62+
self.query_parameters['avoid'] = avoid
63+
64+
def get_data(self, origins, destinations, mode=MODE_DRIVING, avoid=None):
65+
"""Method returns json data fetched by calls from API_URL."""
66+
self.set_query_parameters(
67+
origins=origins,
68+
destinations=destinations,
69+
mode=mode,
70+
avoid=avoid
71+
)
72+
return self.send_data(self.API_URL, self.query_parameters)
73+
74+
75+
76+

geolocation/distance_matrix/const.py

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
11
# encoding: utf-8
22

3-
from geolocation.distance_matrix.api import DistanceMatrixApi
3+
from geolocation.distance_matrix.client import DistanceMatrixApiClient
44
from geolocation.distance_matrix.models import DistanceMatrixModel
55
from geolocation.distance_matrix.parser import DistanceMatrixParser
66
from geolocation.managers import Manager
77

88

99
class DistanceMatrix(object):
10+
client = DistanceMatrixApiClient()
1011
parser = DistanceMatrixParser()
1112
manager = Manager()
1213

1314
def __init__(self, api_key):
14-
self.api = DistanceMatrixApi(api_key)
15+
self.client.api_key = api_key
1516

16-
def to_python(self, json_data):
17+
def build(self, data):
1718
"""Method should converts json_data to python object."""
1819
self.manager.clear() # always clear manager data.
1920

20-
self.parser.json_data = json_data
21+
self.parser.data = data
2122

22-
origins = self.parser.get_origin()
23-
destinations = self.parser.get_destination()
23+
origins = self.parser.prase_origin()
24+
destinations = self.parser.parse_destination()
2425

25-
rows = self.parser.get_rows()
26+
rows = self.parser.parse_rows()
2627

2728
origin_counter = 0
2829

2930
for origin in origins:
3031
destination_counter = 0
3132

3233
for element in rows[origin_counter].get('elements'):
33-
self.parser.json_data = element
34+
self.parser.data = element
3435

3536
model = DistanceMatrixModel()
3637
model.origin = origin
3738
model.destination = destinations[destination_counter]
38-
model.distance = self.parser.get_distance()
39-
model.duration = self.parser.get_duration()
39+
model.distance = self.parser.parse_distance()
40+
model.duration = self.parser.parse_duration()
4041

4142
self.manager.data.add(model)
4243

4344
destination_counter += 1
4445

4546
origin_counter += 1
4647

47-
def distance(self, origins, destinations, mode, avoid=None):
48+
def distance(self, origins, destinations, mode=None, avoid=None):
4849
"""Method returns distance between origins and destination."""
49-
json_data = self.api.query(origins, destinations, mode, avoid)
50-
51-
if json_data:
52-
self.to_python(json_data)
53-
54-
return self.manager
50+
data = self.client.get_data(origins, destinations, mode, avoid)
51+
if data:
52+
self.build(data)
53+
return self.manager

0 commit comments

Comments
 (0)