Skip to content

Commit

Permalink
Fix retry for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rageshkrishna committed Feb 18, 2016
1 parent 6ec164d commit 7bcd997
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 36 deletions.
Binary file modified dist/main/main
Binary file not shown.
89 changes: 53 additions & 36 deletions shippable_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ def __init__(self, api_token):
self.api_url = self.config['SHIPPABLE_API_URL']

def __get(self, url):
self.log.debug('GET {0}'.format(url))
headers = {
'Authorization': 'apiToken {0}'.format(self.api_token),
'Content-Type': 'application/json'
}
while True:
try:
err = None
r = requests.get(url, headers=headers)
self.log.debug('GET {0} completed with {1}'.format(
url, r.status_code))
res_obj = json.loads(r.text)
if r.status_code > 500:
# API server error, we must retry
err_msg = 'API server error: {0} {1}'.format(r.status_code,
r.text)
raise Exception(err_msg)
elif r.status_code is not 200:
err = r.status_code
return err, res_obj
self.log.debug('GET {0}'.format(url))
headers = {
'Authorization': 'apiToken {0}'.format(self.api_token),
'Content-Type': 'application/json'
}
while True:
try:
err = None
response = requests.get(url, headers=headers)
self.log.debug('GET {0} completed with {1}'.format(
url, response.status_code))
res_obj = json.loads(response.text)
if response.status_code >= 500:
# API server error, we must retry
err_msg = 'API server error: {0} {1}'.format(
response.status_code, response.text)
raise Exception(err_msg)
elif response.status_code is not 200:
err = response.status_code
return err, res_obj

except Exception as exc:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(exc), trace)
self.log.error('Exception GETting {0}: {1}'.format(
url, error))
time.sleep(self.config['SHIPPABLE_API_RETRY_INTERVAL'])
except Exception as exc:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(exc), trace)
self.log.error('Exception GETting {0}: {1}'.format(
url, error))
time.sleep(self.config['SHIPPABLE_API_RETRY_INTERVAL'])


def __post(self, url, body):
Expand All @@ -48,13 +48,22 @@ def __post(self, url, body):
}
while True:
try:
r = requests.post(
err = None
response = requests.post(
url,
data=json.dumps(body),
headers=headers)
self.log.debug('POST {0} completed with {1}'.format(
url, r.status_code))
break
url, response.status_code))
if response.status_code >= 500:
# API server error, we must retry
err_msg = 'API server error: {0} {1}'.format(
response.status_code, response.text)
raise Exception(err_msg)
elif response.status_code is not 200:
err = response.status_code
res_obj = json.loads(response.text)
return err, res_obj
except Exception as exc:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(exc), trace)
Expand All @@ -70,13 +79,21 @@ def __put(self, url, body):
}
while True:
try:
r = requests.put(
response = requests.put(
url,
data=json.dumps(body),
headers=headers)
self.log.debug('PUT {0} completed with {1}'.format(
url, r.status_code))
break
url, response.status_code))
res_obj = json.loads(response.text)
if response.status_code >= 500:
# API server error, we must retry
err_msg = 'API server error: {0} {1}'.format(
response.status_code, response.text)
raise Exception(err_msg)
elif response.status_code is not 200:
err = response.status_code
return err, res_obj
except Exception as exc:
trace = traceback.format_exc()
error = '{0}: {1}'.format(str(exc), trace)
Expand All @@ -89,12 +106,12 @@ def post_job_consoles(self, job_id, body):
self.__post(url, body)

def get_job_by_id(self, job_id):
url = '{0}/jobs/{1}'.format(self.api_url, job_id)
return self.__get(url)
url = '{0}/jobs/{1}'.format(self.api_url, job_id)
return self.__get(url)

def put_job_by_id(self, job_id, job):
url = '{0}/jobs/{1}'.format(self.api_url, job_id)
self.__put(url, job)
url = '{0}/jobs/{1}'.format(self.api_url, job_id)
self.__put(url, job)

def post_test_results(self, body):
url = '{0}/jobTestReports'.format(self.api_url)
Expand Down

0 comments on commit 7bcd997

Please sign in to comment.