python-cq 0.1.1__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-cq
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: Lightweight CQRS library.
5
5
  Home-page: https://github.com/100nm/python-cq
6
6
  License: MIT
@@ -1,3 +1,4 @@
1
+ from ._core.bus import Bus
1
2
  from ._core.command import Command, CommandBus, command_handler, find_command_bus
2
3
  from ._core.dto import DTO
3
4
  from ._core.event import Event, EventBus, event_handler, find_event_bus
@@ -5,6 +6,7 @@ from ._core.middleware import Middleware, MiddlewareResult
5
6
  from ._core.query import Query, QueryBus, find_query_bus, query_handler
6
7
 
7
8
  __all__ = (
9
+ "Bus",
8
10
  "Command",
9
11
  "CommandBus",
10
12
  "DTO",
@@ -39,7 +39,8 @@ class Bus[I, O](Protocol):
39
39
  *(
40
40
  self.dispatch(input_value)
41
41
  for input_value in (first_input_value, *input_values)
42
- )
42
+ ),
43
+ return_exceptions=True,
43
44
  )
44
45
 
45
46
  @abstractmethod
@@ -19,7 +19,7 @@ class MiddlewareGroup[**P, T]:
19
19
  return iter(self.__middlewares)
20
20
 
21
21
  def add(self, *middlewares: Middleware[P, T]) -> Self:
22
- self.__middlewares.extend(middlewares)
22
+ self.__middlewares.extend(reversed(middlewares))
23
23
  return self
24
24
 
25
25
  async def invoke(
@@ -44,12 +44,13 @@ class MiddlewareGroup[**P, T]:
44
44
  try:
45
45
  await anext(generator)
46
46
 
47
- try:
48
- value = await handler(*args, **kwargs)
49
- except BaseException as exc:
50
- await generator.athrow(exc)
51
- else:
52
- await generator.asend(value)
47
+ while True:
48
+ try:
49
+ value = await handler(*args, **kwargs)
50
+ except BaseException as exc:
51
+ await generator.athrow(exc)
52
+ else:
53
+ await generator.asend(value)
53
54
 
54
55
  except StopAsyncIteration:
55
56
  ...
@@ -0,0 +1,34 @@
1
+ import asyncio
2
+ from typing import Any
3
+
4
+ from cq import MiddlewareResult
5
+
6
+
7
+ class RetryMiddleware:
8
+ __slots__ = ("__delay", "__exceptions", "__retry")
9
+
10
+ def __init__(
11
+ self,
12
+ retry: int,
13
+ delay: float = 0,
14
+ exceptions: tuple[type[BaseException], ...] = (Exception,),
15
+ ) -> None:
16
+ self.__delay = delay
17
+ self.__exceptions = exceptions
18
+ self.__retry = retry
19
+
20
+ async def __call__(self, *args: Any, **kwargs: Any) -> MiddlewareResult[Any]:
21
+ retry = self.__retry
22
+
23
+ for attempt in range(1, retry + 1):
24
+ try:
25
+ yield
26
+
27
+ except self.__exceptions as exc:
28
+ if attempt == retry:
29
+ raise exc
30
+
31
+ else:
32
+ break
33
+
34
+ await asyncio.sleep(self.__delay)
File without changes
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-cq"
3
- version = "0.1.1"
3
+ version = "0.1.3"
4
4
  description = "Lightweight CQRS library."
5
5
  license = "MIT"
6
6
  authors = ["remimd"]
File without changes
File without changes
File without changes
File without changes
File without changes