Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_retry_over_query_limit(self):
responses.add(responses.GET,
"https://maps.googleapis.com/foo",
body='{"status":"OVER_QUERY_LIMIT"}',
status=200,
content_type="application/json")
client = googlemaps.Client(key="AIzaasdf",
retry_over_query_limit=False)
with self.assertRaises(googlemaps.exceptions.ApiError):
client._request("/foo", {})
self.assertEqual(1, len(responses.calls))
def test_transit_without_time(self):
# With mode of transit, we need a departure_time or an
# arrival_time specified
with self.assertRaises(googlemaps.exceptions.ApiError):
self.client.directions("Sydney Town Hall", "Parramatta, NSW",
mode="transit")
def test_elevation_along_path_single(self):
with self.assertRaises(googlemaps.exceptions.ApiError):
results = self.client.elevation_along_path(
[(40.714728, -73.998672)], 5)
# If we have been given a string of coordinates
if location.count(',') == 1:
try:
parts = location.split(',')
pos_lat = float(parts[0])
pos_lng = float(parts[1])
# we need to ask google for the altitude
response = self.google_maps.elevation((pos_lat, pos_lng))
if response is not None and len(response) and "elevation" in response[0]:
return pos_lat, pos_lng, response[0]["elevation"]
else:
raise ValueError
except ApiError:
self._log("Could not fetch altitude from google. Trying geolocator.", color='yellow')
except ValueError:
self._log("Location was not Lat/Lng. Trying geolocator.", color='yellow')
# Fallback to geolocation if no Lat/Lng can be found
loc = self.google_maps.geocode(location)
return loc.latitude, loc.longitude, loc.altitude
async def _get_body(self, response):
if response.status != 200:
raise googlemaps.exceptions.HTTPError(response.status)
body = await response.json()
api_status = body['status']
if api_status == 'OK' or api_status == 'ZERO_RESULTS':
return body
if api_status == 'OVER_QUERY_LIMIT':
raise googlemaps.exceptions._OverQueryLimit(
api_status, body.get('error_message'))
raise googlemaps.exceptions.ApiError(api_status,
body.get('error_message'))
# Check if location is a trackable entity
if origin.split('.', 1)[0] in TRACKABLE_DOMAINS:
self._origin_entity_id = origin
else:
self._origin = origin
if destination.split('.', 1)[0] in TRACKABLE_DOMAINS:
self._destination_entity_id = destination
else:
self._destination = destination
import googlemaps
self._client = googlemaps.Client(api_key, timeout=10)
try:
self.update()
except googlemaps.exceptions.ApiError as exp:
_LOGGER .error(exp)
self.valid_api_connection = False
return
def _geolocation_extract(response):
"""
Mimics the exception handling logic in ``client._get_body``, but
for geolocation which uses a different response format.
"""
body = response.json()
if response.status_code in (200, 404):
return body
elif response.status_code == 403:
raise exceptions._RetriableRequest()
else:
try:
error = body["error"]["errors"][0]["reason"]
except KeyError:
error = None
raise exceptions.ApiError(response.status_code, error)
if resp.status_code != 200:
raise googlemaps.exceptions.HTTPError(resp.status_code)
body = resp.json()
api_status = body["status"]
if api_status == "OK" or api_status == "ZERO_RESULTS":
return body
if api_status == "OVER_QUERY_LIMIT":
# Retry request.
return self._get(url, params, first_request_time, retry_counter + 1)
if "error_message" in body:
raise googlemaps.exceptions.ApiError(api_status,
body["error_message"])
else:
raise googlemaps.exceptions.ApiError(api_status)