-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
244 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Any | ||
|
||
|
||
class TaskiqSerializer(ABC): | ||
"""Custom serializer for brokers.""" | ||
|
||
@abstractmethod | ||
def dumpb(self, value: Any) -> bytes: | ||
""" | ||
Dump value to bytes for sending through the wire. | ||
:param value: value to encode. | ||
:return: encoded value. | ||
""" | ||
|
||
@abstractmethod | ||
def loadb(self, value: bytes) -> Any: | ||
""" | ||
Parse byte-encoded value received from the wire. | ||
:param message: value to parse. | ||
:return: decoded value. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from taskiq.abc.formatter import TaskiqFormatter | ||
from taskiq.compat import model_dump, model_validate | ||
from taskiq.message import BrokerMessage, TaskiqMessage | ||
|
||
if TYPE_CHECKING: | ||
from taskiq.abc.broker import AsyncBroker | ||
|
||
|
||
class ProxyFormatter(TaskiqFormatter): | ||
"""Default taskiq formatter.""" | ||
|
||
def __init__(self, broker: "AsyncBroker") -> None: | ||
self.broker = broker | ||
|
||
def dumps(self, message: TaskiqMessage) -> BrokerMessage: | ||
""" | ||
Dumps taskiq message to some broker message format. | ||
:param message: message to send. | ||
:return: Dumped message. | ||
""" | ||
return BrokerMessage( | ||
task_id=message.task_id, | ||
task_name=message.task_name, | ||
message=self.broker.serializer.dumpb(model_dump(message)), | ||
labels=message.labels, | ||
) | ||
|
||
def loads(self, message: bytes) -> TaskiqMessage: | ||
""" | ||
Loads json from message. | ||
:param message: broker's message. | ||
:return: parsed taskiq message. | ||
""" | ||
return model_validate(TaskiqMessage, self.broker.serializer.loadb(message)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Taskiq serializers.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from json import dumps, loads | ||
from typing import Any | ||
|
||
from taskiq.abc.serializer import TaskiqSerializer | ||
|
||
|
||
class JSONSerializer(TaskiqSerializer): | ||
"""Default taskiq serizalizer.""" | ||
|
||
def dumpb(self, value: Any) -> bytes: | ||
""" | ||
Dumps taskiq message to some broker message format. | ||
:param message: message to send. | ||
:return: Dumped message. | ||
""" | ||
return dumps(value).encode() | ||
|
||
def loadb(self, value: bytes) -> Any: | ||
""" | ||
Parse byte-encoded value received from the wire. | ||
:param message: value to parse. | ||
:return: decoded value. | ||
""" | ||
return loads(value.decode()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from taskiq.formatters.json_formatter import JSONFormatter | ||
from taskiq.message import BrokerMessage, TaskiqMessage | ||
|
||
|
||
def test_json_dumps() -> None: | ||
fmt = JSONFormatter() | ||
msg = TaskiqMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
labels={"label1": 1, "label2": "text"}, | ||
args=[1, "a"], | ||
kwargs={"p1": "v1"}, | ||
) | ||
expected = BrokerMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
message=( | ||
b'{"task_id":"task-id","task_name":"task.name",' | ||
b'"labels":{"label1":1,"label2":"text"},' | ||
b'"args":[1,"a"],"kwargs":{"p1":"v1"}}' | ||
), | ||
labels={"label1": 1, "label2": "text"}, | ||
) | ||
assert fmt.dumps(msg) == expected | ||
|
||
|
||
def test_json_loads() -> None: | ||
fmt = JSONFormatter() | ||
msg = ( | ||
b'{"task_id":"task-id","task_name":"task.name",' | ||
b'"labels":{"label1":1,"label2":"text"},' | ||
b'"args":[1,"a"],"kwargs":{"p1":"v1"}}' | ||
) | ||
expected = TaskiqMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
labels={"label1": 1, "label2": "text"}, | ||
args=[1, "a"], | ||
kwargs={"p1": "v1"}, | ||
) | ||
assert fmt.loads(msg) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from taskiq.brokers.inmemory_broker import InMemoryBroker | ||
from taskiq.message import BrokerMessage, TaskiqMessage | ||
|
||
|
||
def test_proxy_dumps() -> None: | ||
# uses json serializer by default | ||
broker = InMemoryBroker() | ||
msg = TaskiqMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
labels={"label1": 1, "label2": "text"}, | ||
args=[1, "a"], | ||
kwargs={"p1": "v1"}, | ||
) | ||
expected = BrokerMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
message=( | ||
b'{"task_id": "task-id", "task_name": "task.name", ' | ||
b'"labels": {"label1": 1, "label2": "text"}, ' | ||
b'"args": [1, "a"], "kwargs": {"p1": "v1"}}' | ||
), | ||
labels={"label1": 1, "label2": "text"}, | ||
) | ||
assert broker.formatter.dumps(msg) == expected | ||
|
||
|
||
def test_proxy_loads() -> None: | ||
# uses json serializer by default | ||
broker = InMemoryBroker() | ||
msg = ( | ||
b'{"task_id":"task-id","task_name":"task.name",' | ||
b'"labels":{"label1":1,"label2":"text"},' | ||
b'"args":[1,"a"],"kwargs":{"p1":"v1"}}' | ||
) | ||
expected = TaskiqMessage( | ||
task_id="task-id", | ||
task_name="task.name", | ||
labels={"label1": 1, "label2": "text"}, | ||
args=[1, "a"], | ||
kwargs={"p1": "v1"}, | ||
) | ||
assert broker.formatter.loads(msg) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from taskiq.serializers.json_serializer import JSONSerializer | ||
|
||
|
||
def test_json_dumpb() -> None: | ||
serizalizer = JSONSerializer() | ||
assert serizalizer.dumpb(None) == b"null" # noqa: PLR2004 | ||
assert serizalizer.dumpb(1) == b"1" # noqa: PLR2004 | ||
assert serizalizer.dumpb("a") == b'"a"' # noqa: PLR2004 | ||
assert serizalizer.dumpb(["a"]) == b'["a"]' # noqa: PLR2004 | ||
assert serizalizer.dumpb({"a": "b"}) == b'{"a": "b"}' # noqa: PLR2004 | ||
|
||
|
||
def test_json_loadb() -> None: | ||
serizalizer = JSONSerializer() | ||
assert serizalizer.loadb(b"null") is None | ||
assert serizalizer.loadb(b"1") == 1 | ||
assert serizalizer.loadb(b'"a"') == "a" | ||
assert serizalizer.loadb(b'["a"]') == ["a"] | ||
assert serizalizer.loadb(b'{"a": "b"}') == {"a": "b"} |