Skip to content

Commit

Permalink
sync support added for use case
Browse files Browse the repository at this point in the history
  • Loading branch information
ozgurkara committed Jan 12, 2021
1 parent 4ab6c23 commit 4194e2a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pydiator_core/default_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import inspect

from pydiator_core.interfaces import BaseRequest, BasePipeline
from pydiator_core.mediatr_container import BaseMediatrContainer

Expand All @@ -16,4 +18,8 @@ async def handle(self, req: BaseRequest, **kwargs) -> object:
if not callable(handle_func):
raise Exception("handle_function_has_not_found_in_handler")

is_async = inspect.iscoroutinefunction(handler.handle)
if not is_async:
return handler.handle(req)

return await handler.handle(req)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pydiator-core",
version="1.0.8",
version="1.0.9",
author="Özgür Kara",
author_email="[email protected]",
description="Pydiator",
Expand Down
5 changes: 5 additions & 0 deletions tests/base_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ async def handle(self, req: BaseRequest):
return TestResponse(success=True)


class TestSyncHandler(BaseHandler):
def handle(self, req: BaseRequest):
return TestResponse(success=True)


class TestPipeline(BasePipeline):
def __init__(self, response_success):
self.response_success = response_success
Expand Down
14 changes: 13 additions & 1 deletion tests/test_default_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydiator_core.default_pipeline import DefaultPipeline
from pydiator_core.interfaces import BaseResponse
from pydiator_core.mediatr_container import MediatrContainer
from tests.base_test_case import BaseTestCase, TestRequest, TestHandler
from tests.base_test_case import BaseTestCase, TestRequest, TestHandler, TestSyncHandler


class TestDefaultPipeline(BaseTestCase):
Expand Down Expand Up @@ -45,3 +45,15 @@ def test_handle_return_handle_response(self):

# Then
assert isinstance(response, BaseResponse)

def test_handle_return_handle_response_when_handle_is_sync(self):
# Given
container = MediatrContainer()
container.register_request(TestRequest, TestSyncHandler())
self.pipeline = DefaultPipeline(container)

# When
response = self.async_loop(self.pipeline.handle(req=TestRequest()))

# Then
assert isinstance(response, BaseResponse)

0 comments on commit 4194e2a

Please sign in to comment.