Skip to content

added a retry mechanic to PostJsonHelper to avoid 400 error issues #130

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

Merged
merged 2 commits into from
Aug 3, 2022
Merged
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
4 changes: 2 additions & 2 deletions Algorithmia/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def publish(self, details={}, settings={}, version_info={}, source={}, scmsCrede
url = "/v1/algorithms/" + self.username + "/" + self.algoname + "/versions"
publish_parameters = {"details": details, "settings": settings,
"version_info": version_info, "source": source, "scmsCredentials": scmsCredentials}
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True)
api_response = self.client.postJsonHelper(url, publish_parameters, parse_response_as_json=True, retry=True)
return api_response

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

# Pipe an input into this algorithm
Expand Down
6 changes: 3 additions & 3 deletions Algorithmia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from time import time



class Client(object):
'Algorithmia Common Library'

Expand Down Expand Up @@ -231,7 +230,7 @@ def report_insights(self, insights):
return Insights(insights)

# Used internally to post json to the api and parse json response
def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query_parameters):
def postJsonHelper(self, url, input_object, parse_response_as_json=True, retry=False, **query_parameters):
headers = {}
if self.apiKey is not None:
headers['Authorization'] = self.apiKey
Expand Down Expand Up @@ -263,6 +262,8 @@ def postJsonHelper(self, url, input_object, parse_response_as_json=True, **query
return response
else:
return response
elif retry:
return self.postJsonHelper(url, input_object, parse_response_as_json, False, **query_parameters)
else:
raise raiseAlgoApiError(response)

Expand Down Expand Up @@ -293,7 +294,6 @@ def getJsonHelper(self, url, **query_parameters):
response = response.json()
raise raiseAlgoApiError(response)


def getStreamHelper(self, url, **query_parameters):
headers = {}
if self.apiKey is not None:
Expand Down
7 changes: 7 additions & 0 deletions Test/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,16 @@ async def compile_algorithm(username, algoname):
"resource_type": "algorithm"
}

fail_cnt = 0

@regular_app.post("/v1/algorithms/{username}/{algoname}/versions")
async def publish_algorithm(request: Request, username, algoname):
global fail_cnt
if "failonce" == algoname and fail_cnt == 0:
fail_cnt +=1
return JSONResponse(content="This is an expected failure mode, try again", status_code=400)
elif "failalways" == algoname:
return JSONResponse(status_code=500)
return {"id": "2938ca9f-54c8-48cd-b0d0-0fb7f2255cdc", "name": algoname,
"details": {"summary": "Example Summary", "label": "QA", "tagline": "Example Tagline"},
"settings": {"algorithm_callability": "private", "source_visibility": "open",
Expand Down
12 changes: 12 additions & 0 deletions Test/regular/algo_failure_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,15 @@ def test_throw_500_error_HTTP_response_on_algo_request(self):
result = e
pass
self.assertEqual(str(self.error_message), str(result))

def test_retry_on_400_error_publish(self):
result = self.client.algo("util/failonce").publish()
self.assertEqual(result['version_info']['semantic_version'], "0.1.0")

def test_throw_on_always_500_publish(self):
try:
result = self.client.algo("util/failalways").publish()
except Exception as e:
result = e
pass
self.assertEqual(str(self.error_message), str(result))