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_transport_error(self):
responses.add(responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
status=404,
content_type='application/json')
client = googlemaps.Client(key="AIzaasdf")
with self.assertRaises(googlemaps.exceptions.HTTPError) as e:
client.geocode("Foo")
self.assertEqual(e.exception.status_code, 404)
def test_transport_error(self):
responses.add(responses.GET,
"https://maps.googleapis.com/maps/api/geocode/json",
status=404,
content_type='application/json')
client = googlemaps.Client(key="AIzaasdf")
with self.assertRaises(googlemaps.exceptions.HTTPError) as e:
client.geocode("Foo")
self.assertEqual(e.exception.status_code, 404)
def _get_body(self, response):
if response.status_code != 200:
raise googlemaps.exceptions.HTTPError(response.status_code)
body = 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"))
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'))
"street_address" in result[0]["types"]
or "establishment" in result[0]["types"]
or "premise" in result[0]["types"]
or "subpremise" in result[0]["types"]
):
loc = result[0].get("geometry").get("location")
self.latitude = loc.get("lat")
self.longitude = loc.get("lng")
self.geocode_error = None
else:
self.latitude = None
self.longitude = None
self.geocode_error = _("Address not found")
self.geocode_status = True
return result
except (googlemaps.exceptions.HTTPError, googlemaps.exceptions.ApiError), inst:
self.geocode_error = str(inst)
self.geocode_status = True
except googlemaps.exceptions.Timeout, inst:
self.geocode_error = _("API Timeout")
self.geocode_status = False
finally:
self.geocode_date = datetime.datetime.now().replace(tzinfo=UTC())
if save:
self.save()
try:
resp = requests.get(_BASE_URL + url,
headers={"User-Agent": _USER_AGENT},
timeout=self.timeout,
verify=True) # NOTE(cbro): verify SSL certs.
except requests.exceptions.Timeout:
raise googlemaps.exceptions.Timeout()
except Exception as e:
raise googlemaps.exceptions.TransportError(e)
if resp.status_code in _RETRIABLE_STATUSES:
# Retry request.
return self._get(url, params, first_request_time, retry_counter + 1)
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)
"Received a malformed response.")
if "error" in j:
error = j["error"]
status = error["status"]
if status == "RESOURCE_EXHAUSTED":
raise googlemaps.exceptions._RetriableRequest()
if "message" in error:
raise googlemaps.exceptions.ApiError(status, error["message"])
else:
raise googlemaps.exceptions.ApiError(status)
if resp.status_code != 200:
raise googlemaps.exceptions.HTTPError(resp.status_code)
return j