Skip to content

Commit 1d26de1

Browse files
authored
Async Support (#117)
* added async algo processing to tests, fixed issue where async response was thought of as error * fixed typo in test casese * fixed async tests, and ensured that the algo endpoint dummy works * also fixed raw output
1 parent 1edb330 commit 1d26de1

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Algorithmia/algo_response.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
from Algorithmia.errors import raiseAlgoApiError
3+
from Algorithmia.async_response import AsyncResponse
34
import sys
45

56

@@ -19,8 +20,12 @@ def __repr__(self):
1920

2021
@staticmethod
2122
def create_algo_response(response):
23+
24+
# Check if request is async
25+
if 'async_protocol' in response and 'request_id' in response:
26+
return AsyncResponse(response)
2227
# Parse response JSON, if it's indeed JSON
23-
if 'error' in response or 'metadata' not in response:
28+
elif 'error' in response or 'metadata' not in response:
2429
# Failure
2530
raise raiseAlgoApiError(response)
2631
else:

Test/algo_test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
from Algorithmia.errors import AlgorithmException
4+
from Algorithmia.algorithm import OutputType
45
import Algorithmia
56
# look in ../ BEFORE trying to import Algorithmia. If you append to the
67
# you will load the version installed on the computer.
@@ -26,6 +27,15 @@ def test_normal_call(self):
2627
self.assertEquals("text", result.metadata.content_type)
2728
self.assertEquals("foo", result.result)
2829

30+
def test_async_call(self):
31+
result = self.client.algo('util/echo').set_options(output=OutputType.void).pipe("foo")
32+
self.assertTrue(hasattr(result, "async_protocol"))
33+
self.assertTrue(hasattr(result, "request_id"))
34+
35+
def test_raw_call(self):
36+
result = self.client.algo('util/echo').set_options(output=OutputType.raw).pipe("foo")
37+
self.assertEquals("foo", result)
38+
2939
def test_dict_call(self):
3040
result = self.client.algo('util/echo').pipe({"foo": "bar"})
3141
self.assertEquals("json", result.metadata.content_type)
@@ -82,6 +92,15 @@ def test_call_binary(self):
8292
self.assertEquals('binary', result.metadata.content_type)
8393
self.assertEquals(bytearray('foo', 'utf-8'), result.result)
8494

95+
def test_async_call(self):
96+
result = self.client.algo('util/echo').set_options(output=OutputType.void).pipe("foo")
97+
self.assertTrue(hasattr(result, "async_protocol"))
98+
self.assertTrue(hasattr(result, "request_id"))
99+
100+
def test_raw_call(self):
101+
result = self.client.algo('util/echo').set_options(output=OutputType.raw).pipe("foo")
102+
self.assertEquals("foo", result)
103+
85104
def test_text_unicode(self):
86105
telephone = u"\u260E"
87106

Test/api/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
from fastapi import FastAPI, Request
3+
from typing import Optional
34
from fastapi.responses import Response
45
import json
56
import base64
@@ -19,11 +20,15 @@ def _start_webserver():
1920

2021

2122
@app.post("/v1/algo/{username}/{algoname}")
22-
async def process_algo_req(request: Request, username, algoname):
23+
async def process_algo_req(request: Request, username, algoname, output: Optional[str] = None):
2324
metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774}
2425
content_type = request.headers['Content-Type']
2526
request = await request.body()
26-
if algoname == "500":
27+
if output and output == "void":
28+
return {"async": "abcd123", "request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725"}
29+
elif output and output == "raw":
30+
return Response(request.decode(), status_code=200)
31+
elif algoname == "500":
2732
return Response("Internal Server Error", status_code=500)
2833
elif algoname == "raise_exception":
2934
return {"error": {"message": "This is an exception"}}
@@ -41,7 +46,6 @@ async def process_algo_req(request: Request, username, algoname):
4146
output = {"result": request, "metadata": metadata}
4247
return output
4348

44-
4549
@app.post("/v1/algo/{username}/{algoname}/{githash}")
4650
async def process_hello_world(request: Request, username, algoname, githash):
4751
metadata = {"request_id": "req-55c0480d-6af3-4a21-990a-5c51d29f5725", "duration": 0.000306774,

0 commit comments

Comments
 (0)