-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement customizable serializer (#214)
- Loading branch information
Showing
13 changed files
with
306 additions
and
57 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,45 @@ | ||
import pytest | ||
|
||
from taskiq.formatters.json_formatter import JSONFormatter | ||
from taskiq.message import BrokerMessage, TaskiqMessage | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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 | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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,47 @@ | ||
import pytest | ||
|
||
from taskiq.brokers.inmemory_broker import InMemoryBroker | ||
from taskiq.message import BrokerMessage, TaskiqMessage | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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 | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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,23 @@ | ||
import pytest | ||
|
||
from taskiq.serializers.json_serializer import JSONSerializer | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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 | ||
|
||
|
||
@pytest.mark.anyio | ||
async 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"} |