Skip to content

Commit e23c8a7

Browse files
authored
fixes non 2xx responses (handles failures better) (#126)
1 parent d295aea commit e23c8a7

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

Algorithmia/algorithm.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ def exists(self):
104104
_ = self.client.getJsonHelper(url)
105105
return True
106106
except AlgorithmException as e:
107-
print(e)
108-
return False
107+
if "404" in str(e) or "No such algorithm" in str(e):
108+
return False
109+
elif "403" in str(e):
110+
raise Exception("unable to check exists on algorithms you don't own.")
111+
else:
112+
raise e
109113

110114
# Get all versions of the algorithm, with the given filters
111115
def versions(self, limit=None, marker=None, published=None, callable=None):

Algorithmia/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ def get_algorithm_errors(self, algorithm_id):
214214
"""
215215

216216
url = '/v1/algorithms/%s/errors' % algorithm_id
217-
response = self.getHelper(url)
218-
return response.json()
217+
return self.getJsonHelper(url)
219218

220219
# Used to send insight data to Algorithm Queue Reader in cluster
221220
def report_insights(self, insights):
@@ -280,6 +279,8 @@ def getJsonHelper(self, url, **query_parameters):
280279
else:
281280
return response
282281
else:
282+
if response.content is not None:
283+
response = response.json()
283284
raise raiseAlgoApiError(response)
284285

285286

Algorithmia/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def raiseAlgoApiError(result):
3636
if 'message' in result['error']:
3737
message = result['error']['message']
3838
else:
39-
message = None
39+
message = result['error']
4040
if 'error_type' in result['error']:
4141
err_type = result['error']['error_type']
4242
else:

Test/api/app.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from fastapi import FastAPI, Request
1+
from fastapi import FastAPI, Request, status
22
from typing import Optional
3-
from fastapi.responses import Response
3+
from fastapi.responses import Response, JSONResponse
44
import json
55
import base64
66
from multiprocessing import Process
@@ -49,7 +49,7 @@ async def process_algo_req(request: Request, username, algoname, output: Optiona
4949
return output
5050

5151

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

8487

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

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

106109

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

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

260264

261265
### Admin Routes

Test/regular/client_test.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,11 @@ def test_get_user_errors(self):
114114
self.assertEqual(0, len(response))
115115

116116
def test_get_algorithm_errors(self):
117-
response = self.client.get_algorithm_errors('hello')
118-
self.assertTrue(response is not None)
119-
120-
if type(response) is dict:
121-
self.assertTrue(u'error' in response)
122-
else:
123-
self.assertEqual(404, response.status_code)
117+
try:
118+
_ = self.client.get_algorithm_errors('hello')
119+
self.assertFalse(True)
120+
except AlgorithmException as e:
121+
self.assertTrue(e.message == "No such algorithm")
124122

125123

126124
def test_no_auth_client(self):

0 commit comments

Comments
 (0)