Skip to content
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

Add async call #19

Merged
merged 9 commits into from
Feb 26, 2025
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ response = client.remove_service("service_name") # returns an http response
``` python
# make a synchronous execution
response = client.run_service("service_name", input="input", output="out.png", timeout=100) # returns an http response

# make an asynchronous execution
response = client.run_service("service_name", input="input", async_call=True) # returns an http response
```

#### Logs methods
Expand Down
33 changes: 17 additions & 16 deletions oscar_python/default_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@
import oscar_python._utils as utils

_RUN_PATH = "/run"
_JOB_PATH = "/job"
_POST = "post"


class DefaultClient(metaclass=abc.ABCMeta):

_AUTH_TYPE = ''

""" Run a synchronous execution.
""" Run an execution.
If async is set to True the execution is run asynchronously.
If an output is provided the result is decoded onto the file.
In both cases the function returns the HTTP response."""
def run_service(self, name, **kwargs):
if "token" in kwargs.keys() and kwargs["token"]:
if kwargs.get("token"):
token = kwargs["token"]
else:
token = self._get_token(name)

if "input" in kwargs.keys() and kwargs["input"]:
exec_input = kwargs["input"]
send_data = None
if kwargs.get("input"):
send_data = kwargs["input"]
if not isinstance(send_data, dict):
send_data = utils.encode_input(send_data)

if type(exec_input) is dict:
send_data = exec_input
else:
send_data = utils.encode_input(exec_input)
path = _RUN_PATH + "/"+name
if kwargs.get("async_call"):
path = _JOB_PATH + "/" + name

if "timeout" in kwargs.keys() and kwargs["timeout"]:
response = utils.make_request(self, _RUN_PATH+"/"+name, _POST, data=send_data, token=token, timeout=kwargs["timeout"])
else:
response = utils.make_request(self, _RUN_PATH+"/"+name, _POST, data=send_data, token=token)
response = utils.make_request(self, path, _POST, data=send_data,
token=token, timeout=kwargs.get("timeout"))

if "output" in kwargs.keys() and kwargs["output"]:
utils.decode_output(response.text, kwargs["output"])
return response
if kwargs.get("output"):
utils.decode_output(response.text, kwargs["output"])

return utils.make_request(self, _RUN_PATH+"/"+name, _POST, token=token)
return response
12 changes: 8 additions & 4 deletions tests/test_default_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from unittest.mock import patch, MagicMock
from oscar_python.default_client import DefaultClient, _RUN_PATH, _POST
from oscar_python.default_client import DefaultClient, _RUN_PATH, _POST, _JOB_PATH


class TestDefaultClient(DefaultClient):
Expand Down Expand Up @@ -41,7 +41,7 @@ def test_run_service_with_input_no_token(mock_encode_input, mock_make_request, c
response = client.run_service("test_service", input="test_input")

mock_encode_input.assert_called_once_with("test_input")
mock_make_request.assert_called_once_with(client, _RUN_PATH+"/test_service", _POST, data="encoded_input", token="test_token")
mock_make_request.assert_called_once_with(client, _RUN_PATH+"/test_service", _POST, data="encoded_input", token="test_token", timeout=None)
assert response == mock_response


Expand All @@ -51,7 +51,11 @@ def test_run_service_no_input(mock_make_request, client):
mock_response.text = "response_text"
mock_make_request.return_value = mock_response

response = client.run_service("test_service")
response = client.run_service("test_service", input="data")
mock_make_request.assert_called_with(client, _RUN_PATH+"/test_service", _POST,
token="test_token", data=b'ZGF0YQ==', timeout=None)

mock_make_request.assert_called_once_with(client, _RUN_PATH+"/test_service", _POST, token="test_token")
response = client.run_service("test_service", input="data", async_call=True, timeout=30)
mock_make_request.assert_called_with(client, _JOB_PATH+"/test_service", _POST,
token="test_token", data=b'ZGF0YQ==', timeout=30)
assert response == mock_response