Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Empty response error. #37

Merged
merged 3 commits into from Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 16 additions & 11 deletions WazeRouteCalculator/WazeRouteCalculator.py
Expand Up @@ -34,6 +34,12 @@ class WazeRouteCalculator(object):
'IL': 'il-SearchServer/mozi',
'AU': 'row-SearchServer/mozi'
}
ROUTING_SERVERS = {
'US': 'RoutingManager/routingRequest',
'EU': 'row-RoutingManager/routingRequest',
'IL': 'il-RoutingManager/routingRequest',
'AU': 'row-RoutingManager/routingRequest'
}
COORD_MATCH = re.compile('^([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(\s*)(([-+]?)([\d]{1,3})((\.)(\d+))?)$')

def __init__(self, start_address, end_address, region='EU', vehicle_type='', log_lvl=logging.INFO):
Expand Down Expand Up @@ -109,9 +115,8 @@ def address_to_coords(self, address):
def get_route(self, npaths=1, time_delta=0):
"""Get route data from waze"""

routing_servers = ["row-RoutingManager/routingRequest",
"RoutingManager/routingRequest",
"il-RoutingManager/routingRequest"]
routing_server = self.ROUTING_SERVERS[self.region]

url_options = {
"from": "x:%s y:%s" % (self.start_coords["lon"], self.start_coords["lat"]),
"to": "x:%s y:%s" % (self.end_coords["lon"], self.end_coords["lat"]),
Expand All @@ -126,18 +131,18 @@ def get_route(self, npaths=1, time_delta=0):
if self.vehicle_type:
url_options["vehicleType"] = self.vehicle_type

for routing_srv in routing_servers:
response = requests.get(self.WAZE_URL + routing_srv, params=url_options, headers=self.HEADERS)
response.encoding = 'utf-8'
response_json = self._check_response(response)
if response_json and 'error' not in response_json:
response = requests.get(self.WAZE_URL + routing_server, params=url_options, headers=self.HEADERS)
response.encoding = 'utf-8'
response_json = self._check_response(response)
if response_json:
if 'error' in response_json:
raise WRCError(response_json.get("error"))
else:
if response_json.get("alternatives"):
return [alt['response'] for alt in response_json['alternatives']]
if npaths > 1:
return [response_json['response']]
return response_json['response']
if response_json and 'error' not in response_json:
raise WRCError(response_json.get("error"))
else:
raise WRCError("empty response")

Expand Down Expand Up @@ -196,4 +201,4 @@ def calc_all_routes_info(self, npaths=3, real_time=True, stop_at_bounds=False, t
route_time = [route[0] for route in results.values()]
route_distance = [route[1] for route in results.values()]
self.log.info('Time %.2f - %.2f minutes, distance %.2f - %.2f km.', min(route_time), max(route_time), min(route_distance), max(route_distance))
return results
return results
4 changes: 2 additions & 2 deletions tests.py
Expand Up @@ -77,9 +77,9 @@ def test_get_route(self):
assert self.routing_req in m.request_history[2].url

def test_get_route_next_server(self):
fail_routing_req = self.waze_url + "row-RoutingManager/routingRequest"
fail_routing_req = self.waze_url + "RoutingManager/routingRequest"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right :)
But this change only hide your change not fixing the real test.
As you can see this test was for old get_route which loop through routing servers list.
Now as I can see you use region for selecting correct routing server.
I will merge your PR but please next time write correct tests for the change.
😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I didn't want to remove your test though. To me it appears as if this test is redundant because the previous test grabs the correct response server. I suppose it could have been rewritten to test all 3 servers separately?

fail_routing_response = '{}'
ok_routing_req = self.waze_url + "RoutingManager/routingRequest"
ok_routing_req = self.waze_url + "row-RoutingManager/routingRequest"
ok_routing_response = '{"response":{"results":[{"length":%s,"crossTime":%s}]}}' % (self.length, self.time)
with requests_mock.mock() as m:
m.get(self.address_req, text=self.address_to_coords_response)
Expand Down