diff --git a/Algorithmia/algorithm.py b/Algorithmia/algorithm.py index 5d3c778..b181efa 100644 --- a/Algorithmia/algorithm.py +++ b/Algorithmia/algorithm.py @@ -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): diff --git a/Algorithmia/client.py b/Algorithmia/client.py index 30dec03..74d88fa 100644 --- a/Algorithmia/client.py +++ b/Algorithmia/client.py @@ -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): @@ -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) diff --git a/Algorithmia/errors.py b/Algorithmia/errors.py index f23662a..22ad68e 100644 --- a/Algorithmia/errors.py +++ b/Algorithmia/errors.py @@ -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: diff --git a/Test/api/app.py b/Test/api/app.py index cd80621..8871d91 100644 --- a/Test/api/app.py +++ b/Test/api/app.py @@ -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 @@ -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"} @@ -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", @@ -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}") @@ -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}") @@ -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 { @@ -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 diff --git a/Test/regular/client_test.py b/Test/regular/client_test.py index 3a0e1be..c7ed9f2 100644 --- a/Test/regular/client_test.py +++ b/Test/regular/client_test.py @@ -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):