From 6e6dee8b72da5df4cc17e7a9a74f3cd9200a4b73 Mon Sep 17 00:00:00 2001 From: zeryx <1892175+zeryx@users.noreply.github.com> Date: Wed, 3 Aug 2022 03:09:14 -0300 Subject: [PATCH 1/2] added a retry mechanic to PostJsonHelper to avoid 400 error issues --- Algorithmia/algorithm.py | 4 ++-- Algorithmia/client.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Algorithmia/algorithm.py b/Algorithmia/algorithm.py index 04dfbaf..40be378 100644 --- a/Algorithmia/algorithm.py +++ b/Algorithmia/algorithm.py @@ -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): @@ -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 diff --git a/Algorithmia/client.py b/Algorithmia/client.py index a5121f1..dc26e1a 100644 --- a/Algorithmia/client.py +++ b/Algorithmia/client.py @@ -16,7 +16,6 @@ from time import time - class Client(object): 'Algorithmia Common Library' @@ -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 @@ -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) @@ -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: From 6578366837247620c32d27371eda073e209c1e51 Mon Sep 17 00:00:00 2001 From: zeryx <1892175+zeryx@users.noreply.github.com> Date: Wed, 3 Aug 2022 03:20:31 -0300 Subject: [PATCH 2/2] added test cases to verify the retry once system --- Test/api/app.py | 7 +++++++ Test/regular/algo_failure_test.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Test/api/app.py b/Test/api/app.py index 0384ae6..db7efd2 100644 --- a/Test/api/app.py +++ b/Test/api/app.py @@ -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", diff --git a/Test/regular/algo_failure_test.py b/Test/regular/algo_failure_test.py index 0804b4a..0ec4fc2 100644 --- a/Test/regular/algo_failure_test.py +++ b/Test/regular/algo_failure_test.py @@ -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))