Skip to content

Commit 31f0e90

Browse files
authored
added a retry mechanic to PostJsonHelper to avoid 400 error issues (#130)
* added a retry mechanic to PostJsonHelper to avoid 400 error issues * added test cases to verify the retry once system
1 parent 5e2a8a8 commit 31f0e90

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Algorithmia/algorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def publish(self, details={}, settings={}, version_info={}, source={}, scmsCrede
106106
url = "/v1/algorithms/" + self.username + "/" + self.algoname + "/versions"
107107
publish_parameters = {"details": details, "settings": settings,
108108
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}
109-
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True)
109+
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True, retry=True)
110110
return api_response
111111

112112
def get_builds(self, limit=56, marker=None):
@@ -180,7 +180,7 @@ def versions(self, limit=None, marker=None, published=None, callable=None):
180180
def compile(self):
181181
# Compile algorithm
182182
url = '/v1/algorithms/' + self.username + '/' + self.algoname + '/compile'
183-
response = self.client.postJsonHelper(url, {}, parse_response_as_json=True)
183+
response = self.client.postJsonHelper(url, {}, parse_response_as_json=True, retry=True)
184184
return response
185185

186186
# Pipe an input into this algorithm

Algorithmia/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from time import time
1717

1818

19-
2019
class Client(object):
2120
'Algorithmia Common Library'
2221

@@ -231,7 +230,7 @@ def report_insights(self, insights):
231230
return Insights(insights)
232231

233232
# Used internally to post json to the api and parse json response
234-
def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query_parameters):
233+
def postJsonHelper(self, url, input_object, parse_response_as_json=True, retry=False, **query_parameters):
235234
headers = {}
236235
if self.apiKey is not None:
237236
headers['Authorization'] = self.apiKey
@@ -263,6 +262,8 @@ def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query
263262
return response
264263
else:
265264
return response
265+
elif retry:
266+
return self.postJsonHelper(url, input_object, parse_response_as_json, False, **query_parameters)
266267
else:
267268
raise raiseAlgoApiError(response)
268269

@@ -293,7 +294,6 @@ def getJsonHelper(self, url, **query_parameters):
293294
response = response.json()
294295
raise raiseAlgoApiError(response)
295296

296-
297297
def getStreamHelper(self, url, **query_parameters):
298298
headers = {}
299299
if self.apiKey is not None:

Test/api/app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,16 @@ async def compile_algorithm(username, algoname):
230230
"resource_type": "algorithm"
231231
}
232232

233+
fail_cnt = 0
233234

234235
@regular_app.post("/v1/algorithms/{username}/{algoname}/versions")
235236
async def publish_algorithm(request: Request, username, algoname):
237+
global fail_cnt
238+
if "failonce" == algoname and fail_cnt == 0:
239+
fail_cnt +=1
240+
return JSONResponse(content="This is an expected failure mode, try again", status_code=400)
241+
elif "failalways" == algoname:
242+
return JSONResponse(status_code=500)
236243
return {"id": "2938ca9f-54c8-48cd-b0d0-0fb7f2255cdc", "name": algoname,
237244
"details": {"summary": "Example Summary", "label": "QA", "tagline": "Example Tagline"},
238245
"settings": {"algorithm_callability": "private", "source_visibility": "open",

Test/regular/algo_failure_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,15 @@ def test_throw_500_error_HTTP_response_on_algo_request(self):
2828
result = e
2929
pass
3030
self.assertEqual(str(self.error_message), str(result))
31+
32+
def test_retry_on_400_error_publish(self):
33+
result = self.client.algo("util/failonce").publish()
34+
self.assertEqual(result['version_info']['semantic_version'], "0.1.0")
35+
36+
def test_throw_on_always_500_publish(self):
37+
try:
38+
result = self.client.algo("util/failalways").publish()
39+
except Exception as e:
40+
result = e
41+
pass
42+
self.assertEqual(str(self.error_message), str(result))

0 commit comments

Comments
 (0)