Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:type first_request_time: datetime.datetime
:param retry_counter: The number of this retry, or zero for first attempt.
:type retry_counter: int
:raises ApiError: when the API returns an error.
:raises Timeout: if the request timed out.
:raises TransportError: when something went wrong while trying to
exceute a request.
"""
if not first_request_time:
first_request_time = datetime.now()
elapsed = datetime.now() - first_request_time
if elapsed > self.retry_timeout:
raise googlemaps.exceptions.Timeout()
if retry_counter > 0:
# 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration,
# starting at 0.5s when retry_counter=0. The first retry will occur
# at 1, so subtract that first.
delay_seconds = 0.5 * 1.5 ** (retry_counter - 1)
# Jitter this value by 50% and pause.
time.sleep(delay_seconds * (random.random() + 0.5))
url = self._generate_auth_url(url, params)
try:
resp = requests.get(_BASE_URL + url,
headers={"User-Agent": _USER_AGENT},
timeout=self.timeout,
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()
__init__, but provided here to allow overriding internally on a
per-request basis.
:type requests_kwargs: dict
:raises ApiError: when the API returns an error.
:raises Timeout: if the request timed out.
:raises TransportError: when something went wrong while trying to
exceute a request.
"""
if not first_request_time:
first_request_time = datetime.now()
elapsed = datetime.now() - first_request_time
if elapsed > self.retry_timeout:
raise googlemaps.exceptions.Timeout()
if retry_counter > 0:
# 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration,
# starting at 0.5s when retry_counter=0. The first retry will occur
# at 1, so subtract that first.
delay_seconds = 0.5 * 1.5 ** (retry_counter - 1)
# Jitter this value by 50% and pause.
time.sleep(delay_seconds * (random.random() + 0.5))
authed_url = self._generate_auth_url(url, params, accepts_clientid)
# Default to the client-level self.requests_kwargs, with method-level
# requests_kwargs arg overriding.
requests_kwargs = requests_kwargs or {}
final_requests_kwargs = dict(self.requests_kwargs, **requests_kwargs)