Skip to content

Async Support #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Algorithmia/algo_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
from Algorithmia.errors import raiseAlgoApiError
from Algorithmia.async_response import AsyncResponse
import sys


Expand All @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions Test/algo_test.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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"

Expand Down
10 changes: 7 additions & 3 deletions Test/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
from fastapi import FastAPI, Request
from typing import Optional
from fastapi.responses import Response
import json
import base64
Expand All @@ -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"}}
Expand All @@ -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,
Expand Down