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

Retrying requests #92

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ python-dateutil==2.7.0
pytz==2019.3
requests==2.20.0
requests-toolbelt==0.9.1
retrying==1.3.3
six==1.12.0
Sphinx==1.7.5
sphinx-rtd-theme==0.2.4
Expand Down
58 changes: 24 additions & 34 deletions voxel51/users/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dateutil.parser
import mimetypes
import requests
from retrying import retry

import voxel51.users.auth as voxa
import voxel51.users.jobs as voxj
Expand Down Expand Up @@ -161,8 +162,7 @@ def list_analytics(self, all_versions=False):
'''
endpoint = voxu.urljoin(self.base_url, "analytics", "list")
data = {"all_versions": all_versions}
res = self._requests.get(endpoint, headers=self._header, json=data)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header, json=data)
return _parse_json_response(res)["analytics"]

def query_analytics(self, analytics_query):
Expand All @@ -181,9 +181,8 @@ def query_analytics(self, analytics_query):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "analytics")
res = self._requests.get(
res = self._request_get(
endpoint, headers=self._header, json=analytics_query.to_dict())
_validate_response(res)
return _parse_json_response(res)

def get_analytic_id(self, name, version=None):
Expand Down Expand Up @@ -236,8 +235,7 @@ def get_analytic_details(self, analytic_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "analytics", analytic_id)
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["analytic"]

def get_analytic_doc(self, analytic_id):
Expand All @@ -253,8 +251,7 @@ def get_analytic_doc(self, analytic_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "analytics", analytic_id, "doc")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)

def upload_analytic(self, doc_json_path, analytic_type=None):
Expand Down Expand Up @@ -331,6 +328,7 @@ def delete_analytic(self, analytic_id):
res = self._requests.delete(endpoint, headers=self._header)
_validate_response(res)


def batch_get_analytic_details(self, analytic_ids):
'''Gets details about the analytics with the given IDs.

Expand Down Expand Up @@ -361,8 +359,7 @@ def list_data(self):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "data", "list")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["data"]

def query_data(self, data_query):
Expand All @@ -380,9 +377,8 @@ def query_data(self, data_query):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "data")
res = self._requests.get(
res = self._request_get(
endpoint, headers=self._header, json=data_query.to_dict())
_validate_response(res)
return _parse_json_response(res)

def upload_data(self, path, ttl=None):
Expand Down Expand Up @@ -476,8 +472,7 @@ def get_data_details(self, data_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "data", data_id)
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["data"]

def download_data(self, data_id, output_path=None):
Expand Down Expand Up @@ -515,8 +510,7 @@ def get_data_download_url(self, data_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "data", data_id, "download-url")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["url"]

def update_data_ttl(self, data_id, days=None, expiration_date=None):
Expand Down Expand Up @@ -649,8 +643,7 @@ def list_jobs(self):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", "list")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["jobs"]

def query_jobs(self, jobs_query):
Expand All @@ -668,9 +661,8 @@ def query_jobs(self, jobs_query):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs")
res = self._requests.get(
res = self._request_get(
endpoint, headers=self._header, json=jobs_query.to_dict())
_validate_response(res)
return _parse_json_response(res)

def upload_job_request(
Expand Down Expand Up @@ -720,8 +712,7 @@ def get_job_details(self, job_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id)
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["job"]

def get_job_request(self, job_id):
Expand All @@ -738,8 +729,7 @@ def get_job_request(self, job_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id, "request")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return voxj.JobRequest.from_dict(_parse_json_response(res))

def start_job(self, job_id):
Expand Down Expand Up @@ -927,8 +917,7 @@ def get_job_status(self, job_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id, "status")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)

def download_job_output(self, job_id, output_path=None):
Expand Down Expand Up @@ -967,8 +956,7 @@ def get_job_output_download_url(self, job_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id, "output-url")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["url"]

def download_job_logfile(self, job_id, output_path=None):
Expand All @@ -990,8 +978,7 @@ def download_job_logfile(self, job_id, output_path=None):
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id, "log")
if output_path is None:
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return res.content.decode()

self._stream_download(endpoint, output_path)
Expand All @@ -1014,8 +1001,7 @@ def get_job_logfile_download_url(self, job_id):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "jobs", job_id, "log-url")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["url"]

def delete_job(self, job_id):
Expand Down Expand Up @@ -1198,8 +1184,7 @@ def get_platform_status(self):
:class:`APIError` if the request was unsuccessful
'''
endpoint = voxu.urljoin(self.base_url, "status", "all")
res = self._requests.get(endpoint, headers=self._header)
_validate_response(res)
res = self._request_get(endpoint, headers=self._header)
return _parse_json_response(res)["statuses"]

# PRIVATE METHODS #########################################################
Expand All @@ -1224,6 +1209,11 @@ def _stream_download(self, url, output_path):
for chunk in res.iter_content(chunk_size=_CHUNK_SIZE):
f.write(chunk)

@retry(wait_exponential_multiplier=100, wait_exponential_max=10000)
def _request_get(self, *args, **kwargs):
res = self._requests.get(*args, **kwargs)
_validate_response(res)
return res

class APIError(Exception):
'''Exception raised when an :class:`API` request fails.'''
Expand Down