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

#1122 HERE: fail on non-token related http errors, fail after max token attempts #1129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions here/traffic/here_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,28 @@ def get_download_url(request_id, status_base_url, access_token, user_id, api_con
status='Pending'
status_url = status_base_url + str(user_id) + '/requests/' + str(request_id)

#try polling same request_id for up to 8 hrs
token_counter = 0
while status != "Completed Successfully" and token_counter < 8:
#try polling same request_id for up to max_tokens hrs
token_counter=0
max_tokens=8
while status != "Completed Successfully" and token_counter < max_tokens:
sleep(60)
LOGGER.info('Polling status of query request: %s', request_id)
status_header = {'Authorization': 'Bearer ' + access_token}
query_status = requests.get(status_url, headers = status_header)
try:
query_status.raise_for_status()
status = str(query_status.json()['status'])
except requests.exceptions.HTTPError:
#access token expires after 1 hr, try to generate up to 3 times.
access_token = get_access_token(api_conn)
token_counter+=1
except requests.exceptions.HTTPError as err:
error_desc = query_status.json()['error_description']
if error_desc.startswith('Token Validation Failure'):
#access token expires after 1 hr, try to generate up to max_tokens times.
LOGGER.info('Token expired; refreshing.')
access_token = get_access_token(api_conn)
token_counter+=1
else:
LOGGER.error("HTTP error in query status response.")
LOGGER.error(query_status.text)
raise HereAPIException(err)
except KeyError as err:
error = 'Error in polling status of query request \n'
error += 'err\n'
Expand All @@ -148,9 +156,12 @@ def get_download_url(request_id, status_base_url, access_token, user_id, api_con
LOGGER.warning("JSON error in query status response.")
LOGGER.warning(query_status.text)
continue

LOGGER.info('Requested query completed')

if token_counter==max_tokens:
LOGGER.error("Maximum number of token retries used.")
raise HereAPIException

LOGGER.info('Requested query completed')
return query_status.json()['outputUrl']

@click.group(invoke_without_command=True)
Expand Down
Loading