From b51b47fbc8ba984d192c1a56474012182570dce5 Mon Sep 17 00:00:00 2001 From: James Sutton <1892175+zeryx@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:02:04 -0500 Subject: [PATCH 1/4] added async algo processing to tests, fixed issue where async response was thought of as error --- Algorithmia/algo_response.py | 7 ++++++- Test/algo_test.py | 10 ++++++++++ Test/api/__init__.py | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Algorithmia/algo_response.py b/Algorithmia/algo_response.py index dfce6c1..d5abbc4 100644 --- a/Algorithmia/algo_response.py +++ b/Algorithmia/algo_response.py @@ -1,5 +1,6 @@ import base64 from Algorithmia.errors import raiseAlgoApiError +from Algorithmia.async_response import AsyncResponse import sys @@ -19,8 +20,12 @@ def __repr__(self): @staticmethod def create_algo_response(response): + + # Check if request is async + if 'async_protocol' in response and 'request_id' in response: + return AsyncResponse(response) # Parse response JSON, if it's indeed JSON - if 'error' in response or 'metadata' not in response: + elif 'error' in response or 'metadata' not in response: # Failure raise raiseAlgoApiError(response) else: diff --git a/Test/algo_test.py b/Test/algo_test.py index b22988e..52dce76 100644 --- a/Test/algo_test.py +++ b/Test/algo_test.py @@ -26,6 +26,11 @@ def test_normal_call(self): self.assertEquals("text", result.metadata.content_type) self.assertEquals("foo", result.result) + def test_async_call(self): + result = self.client.algo('util/echo').set_options(output=None).pipe("foo") + self.assertTrue("async_protocol" in result) + self.assertTrue("request_id" in result) + def test_dict_call(self): result = self.client.algo('util/echo').pipe({"foo": "bar"}) self.assertEquals("json", result.metadata.content_type) @@ -82,6 +87,11 @@ def test_call_binary(self): self.assertEquals('binary', result.metadata.content_type) self.assertEquals(bytearray('foo', 'utf-8'), result.result) + def test_async_call(self): + result = self.client.algo('util/echo').set_options(output=None).pipe("foo") + self.assertTrue("async_protocol" in result) + self.assertTrue("request_id" in result) + def test_text_unicode(self): telephone = u"\u260E" diff --git a/Test/api/__init__.py b/Test/api/__init__.py index 99e7e68..bae6cc0 100644 --- a/Test/api/__init__.py +++ b/Test/api/__init__.py @@ -42,6 +42,11 @@ async def process_algo_req(request: Request, username, algoname): return output +@app.post("/v1/algo/{username}/{algoname}?output=void") +async def process_async_req(request: Request, username, algoname): + return {"async_protocol": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"} + + @app.post("/v1/algo/{username}/{algoname}/{githash}") async def process_hello_world(request: Request, username, algoname, githash): metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774, From 426adf2b741645250ce2721dab29e37cb2f6b789 Mon Sep 17 00:00:00 2001 From: James Sutton <1892175+zeryx@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:05:28 -0500 Subject: [PATCH 2/4] fixed typo in test casese --- Test/algo_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/algo_test.py b/Test/algo_test.py index 52dce76..1568f34 100644 --- a/Test/algo_test.py +++ b/Test/algo_test.py @@ -27,7 +27,7 @@ def test_normal_call(self): self.assertEquals("foo", result.result) def test_async_call(self): - result = self.client.algo('util/echo').set_options(output=None).pipe("foo") + result = self.client.algo('util/echo').set_options(output="void").pipe("foo") self.assertTrue("async_protocol" in result) self.assertTrue("request_id" in result) @@ -88,7 +88,7 @@ def test_call_binary(self): self.assertEquals(bytearray('foo', 'utf-8'), result.result) def test_async_call(self): - result = self.client.algo('util/echo').set_options(output=None).pipe("foo") + result = self.client.algo('util/echo').set_options(output="void").pipe("foo") self.assertTrue("async_protocol" in result) self.assertTrue("request_id" in result) From f2772925ef9716e3101d887c692b7f3291559b44 Mon Sep 17 00:00:00 2001 From: James Sutton <1892175+zeryx@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:25:12 -0500 Subject: [PATCH 3/4] fixed async tests, and ensured that the algo endpoint dummy works --- Test/algo_test.py | 13 +++++++------ Test/api/__init__.py | 11 ++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Test/algo_test.py b/Test/algo_test.py index 1568f34..f1dbdf1 100644 --- a/Test/algo_test.py +++ b/Test/algo_test.py @@ -1,6 +1,7 @@ import sys import os from Algorithmia.errors import AlgorithmException +from Algorithmia.algorithm import OutputType import Algorithmia # look in ../ BEFORE trying to import Algorithmia. If you append to the # you will load the version installed on the computer. @@ -27,9 +28,9 @@ def test_normal_call(self): self.assertEquals("foo", result.result) def test_async_call(self): - result = self.client.algo('util/echo').set_options(output="void").pipe("foo") - self.assertTrue("async_protocol" in result) - self.assertTrue("request_id" in result) + result = self.client.algo('util/echo').set_options(output=OutputType.void).pipe("foo") + self.assertTrue(hasattr(result, "async_protocol")) + self.assertTrue(hasattr(result, "request_id")) def test_dict_call(self): result = self.client.algo('util/echo').pipe({"foo": "bar"}) @@ -88,9 +89,9 @@ def test_call_binary(self): self.assertEquals(bytearray('foo', 'utf-8'), result.result) def test_async_call(self): - result = self.client.algo('util/echo').set_options(output="void").pipe("foo") - self.assertTrue("async_protocol" in result) - self.assertTrue("request_id" in result) + result = self.client.algo('util/echo').set_options(output=OutputType.void).pipe("foo") + self.assertTrue(hasattr(result, "async_protocol")) + self.assertTrue(hasattr(result, "request_id")) def test_text_unicode(self): telephone = u"\u260E" diff --git a/Test/api/__init__.py b/Test/api/__init__.py index bae6cc0..028f3fc 100644 --- a/Test/api/__init__.py +++ b/Test/api/__init__.py @@ -1,5 +1,6 @@ import importlib from fastapi import FastAPI, Request +from typing import Optional from fastapi.responses import Response import json import base64 @@ -19,10 +20,12 @@ def _start_webserver(): @app.post("/v1/algo/{username}/{algoname}") -async def process_algo_req(request: Request, username, algoname): +async def process_algo_req(request: Request, username, algoname, output: Optional[str] = None): metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774} content_type = request.headers['Content-Type'] request = await request.body() + if output and output == "void": + return {"async": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"} if algoname == "500": return Response("Internal Server Error", status_code=500) elif algoname == "raise_exception": @@ -41,12 +44,6 @@ async def process_algo_req(request: Request, username, algoname): output = {"result": request, "metadata": metadata} return output - -@app.post("/v1/algo/{username}/{algoname}?output=void") -async def process_async_req(request: Request, username, algoname): - return {"async_protocol": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"} - - @app.post("/v1/algo/{username}/{algoname}/{githash}") async def process_hello_world(request: Request, username, algoname, githash): metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774, From 76aa24b82c3b20ac250928d74590439ef4f4d71e Mon Sep 17 00:00:00 2001 From: James Sutton <1892175+zeryx@users.noreply.github.com> Date: Tue, 9 Nov 2021 14:09:52 -0500 Subject: [PATCH 4/4] also fixed raw output --- Test/algo_test.py | 8 ++++++++ Test/api/__init__.py | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Test/algo_test.py b/Test/algo_test.py index f1dbdf1..0e3afdd 100644 --- a/Test/algo_test.py +++ b/Test/algo_test.py @@ -32,6 +32,10 @@ def test_async_call(self): self.assertTrue(hasattr(result, "async_protocol")) self.assertTrue(hasattr(result, "request_id")) + def test_raw_call(self): + result = self.client.algo('util/echo').set_options(output=OutputType.raw).pipe("foo") + self.assertEquals("foo", result) + def test_dict_call(self): result = self.client.algo('util/echo').pipe({"foo": "bar"}) self.assertEquals("json", result.metadata.content_type) @@ -93,6 +97,10 @@ def test_async_call(self): self.assertTrue(hasattr(result, "async_protocol")) self.assertTrue(hasattr(result, "request_id")) + def test_raw_call(self): + result = self.client.algo('util/echo').set_options(output=OutputType.raw).pipe("foo") + self.assertEquals("foo", result) + def test_text_unicode(self): telephone = u"\u260E" diff --git a/Test/api/__init__.py b/Test/api/__init__.py index 028f3fc..9057bcf 100644 --- a/Test/api/__init__.py +++ b/Test/api/__init__.py @@ -26,7 +26,9 @@ async def process_algo_req(request: Request, username, algoname, output: Optiona request = await request.body() if output and output == "void": return {"async": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"} - if algoname == "500": + elif output and output == "raw": + return Response(request.decode(), status_code=200) + elif algoname == "500": return Response("Internal Server Error", status_code=500) elif algoname == "raise_exception": return {"error": {"message": "This is an exception"}}