Skip to content

fixes non 2xx responses (handles failures better) #126

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 1 commit into from
May 13, 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
8 changes: 6 additions & 2 deletions Algorithmia/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ def exists(self):
_ = self.client.getJsonHelper(url)
return True
except AlgorithmException as e:
print(e)
return False
if "404" in str(e) or "No such algorithm" in str(e):
return False
elif "403" in str(e):
raise Exception("unable to check exists on algorithms you don't own.")
else:
raise e

# Get all versions of the algorithm, with the given filters
def versions(self, limit=None, marker=None, published=None, callable=None):
Expand Down
5 changes: 3 additions & 2 deletions Algorithmia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ def get_algorithm_errors(self, algorithm_id):
"""

url = '/v1/algorithms/%s/errors' % algorithm_id
response = self.getHelper(url)
return response.json()
return self.getJsonHelper(url)

# Used to send insight data to Algorithm Queue Reader in cluster
def report_insights(self, insights):
Expand Down Expand Up @@ -280,6 +279,8 @@ def getJsonHelper(self, url, **query_parameters):
else:
return response
else:
if response.content is not None:
response = response.json()
raise raiseAlgoApiError(response)


Expand Down
2 changes: 1 addition & 1 deletion Algorithmia/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def raiseAlgoApiError(result):
if 'message' in result['error']:
message = result['error']['message']
else:
message = None
message = result['error']
if 'error_type' in result['error']:
err_type = result['error']['error_type']
else:
Expand Down
18 changes: 11 additions & 7 deletions Test/api/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, status
from typing import Optional
from fastapi.responses import Response
from fastapi.responses import Response, JSONResponse
import json
import base64
from multiprocessing import Process
Expand Down Expand Up @@ -49,7 +49,7 @@ async def process_algo_req(request: Request, username, algoname, output: Optiona
return output


@regular_app.post("/v1/algo/{username}/{algoname}/{githash}")
@regular_app.post("/v1/algo/{username}/{algoname}/{githash}", status_code=status.HTTP_200_OK)
async def process_hello_world(request: Request, username, algoname, githash):
metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774,
'content_type': "text"}
Expand All @@ -61,7 +61,7 @@ async def process_hello_world(request: Request, username, algoname, githash):
### Algorithm Routes
@regular_app.get('/v1/algorithms/{username}/{algoname}')
async def process_get_algo(request: Request, username, algoname):
if algoname == "echo":
if algoname == "echo" and username == 'quality':
return {"id": "21df7a38-eab8-4ac8-954c-41a285535e69", "name": "echo",
"details": {"summary": "", "label": "echo", "tagline": ""},
"settings": {"algorithm_callability": "public", "source_visibility": "closed",
Expand All @@ -78,8 +78,11 @@ async def process_get_algo(request: Request, username, algoname):
"compilation": {"successful": True, "output": ""},
"self_link": "https://api.algorithmia.com/v1/algorithms/quality/echo/versions/0cfd7a6600f1fa05f9fe93016e661a9332c4ded2",
"resource_type": "algorithm"}
elif algoname == "echo":
return JSONResponse(content={"error": {"id": "1cfb98c5-532e-4cbf-9192-fdd45b86969c", "code": 2001,
"message": "Caller is not authorized to perform the operation"}}, status_code=403)
else:
return {"error": "No such algorithm"}
return JSONResponse(content={"error": "No such algorithm"}, status_code=404)


@regular_app.get("/v1/algorithms/{username}/{algoname}/builds/{buildid}")
Expand All @@ -101,7 +104,7 @@ async def get_scm_status(username, algoname):

@regular_app.get("/v1/algorithms/{algo_id}/errors")
async def get_algo_errors(algo_id):
return {"error": {"message": "not found"}}
return JSONResponse(content={"error": {"message": "not found"}}, status_code=404)


@regular_app.post("/v1/algorithms/{username}")
Expand All @@ -116,6 +119,7 @@ async def create_algorithm(request: Request, username):
"source": {"scm": {"id": "internal", "provider": "internal", "default": True, "enabled": True}},
"resource_type": "algorithm"}


@regular_app.put('/v1/algorithms/{username}/{algoname}')
async def update_algorithm(request: Request, username, algoname):
return {
Expand Down Expand Up @@ -255,7 +259,7 @@ async def get_algorithm_info(username, algoname, algohash):
"source": {"scm": {"id": "internal", "provider": "internal", "default": True, "enabled": True}},
"compilation": {"successful": True, "output": ""}, "resource_type": "algorithm"}
else:
return {"error": {"message": "not found"}}
return JSONResponse(content={"error": {"message": "not found"}}, status_code=404)


### Admin Routes
Expand Down
12 changes: 5 additions & 7 deletions Test/regular/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,11 @@ def test_get_user_errors(self):
self.assertEqual(0, len(response))

def test_get_algorithm_errors(self):
response = self.client.get_algorithm_errors('hello')
self.assertTrue(response is not None)

if type(response) is dict:
self.assertTrue(u'error' in response)
else:
self.assertEqual(404, response.status_code)
try:
_ = self.client.get_algorithm_errors('hello')
self.assertFalse(True)
except AlgorithmException as e:
self.assertTrue(e.message == "No such algorithm")


def test_no_auth_client(self):
Expand Down