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..0e3afdd 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. @@ -26,6 +27,15 @@ 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=OutputType.void).pipe("foo") + 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) @@ -82,6 +92,15 @@ 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=OutputType.void).pipe("foo") + 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 99e7e68..9057bcf 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,11 +20,15 @@ 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 algoname == "500": + if output and output == "void": + return {"async": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"} + 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"}} @@ -41,7 +46,6 @@ async def process_algo_req(request: Request, username, algoname): output = {"result": request, "metadata": metadata} return output - @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,