Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Any, NamedTuple

from wsrpc_aiohttp import WSRPCClient, serializer
from wsrpc_aiohttp.websocket.tools import json_dumps


async def test_call_error(client: WSRPCClient, handler):

handler.configure(dumps=json_dumps)

class CustomType(NamedTuple):
name: str
value: Any

@serializer.register(CustomType)
def _serizlize(value: CustomType):
return {
"custom_type": {
"name": value.name,
"value": serializer(value.value),
},
}

async def handle_request(_):
return CustomType(name="the answer", value=42)

handler.add_route("send_custom", handle_request)

async with client:
result = await client.call("send_custom")

assert result
2 changes: 1 addition & 1 deletion wsrpc_aiohttp/websocket/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def _send(self, **kwargs):

async with self.send_lock:
return await self.socket.send_json(
kwargs, dumps=self._dumps,
kwargs, dumps=self._json_dumps,
)
except aiohttp.WebSocketError:
self._loop.create_task(self.close())
Expand Down
7 changes: 2 additions & 5 deletions wsrpc_aiohttp/websocket/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
LocksCollectionType, Proxy, RouteCollectionType, RouteType, TimeoutType,
)
from .route import Route
from .tools import Singleton, awaitable, serializer
from .tools import Singleton, awaitable, json_dumps, serializer


class WSRPCError(Exception):
Expand Down Expand Up @@ -92,14 +92,11 @@ class WSRPCBase(AbstractWSRPC):
_pending_tasks: t.Set[t.Union[asyncio.Task, asyncio.Handle]]
_handlers: t.Dict[str, RouteType]

def _dumps(self, value: t.Any) -> t.Any:
return self._json_dumps(value, default=serializer)

def __init__(
self, loop: asyncio.AbstractEventLoop = None,
timeout: t.Optional[TimeoutType] = None,
loads: LoadsType = json.loads,
dumps: DumpsType = json.dumps,
dumps: DumpsType = json_dumps,
):
self._json_dumps = dumps
self._json_loads = loads
Expand Down
4 changes: 2 additions & 2 deletions wsrpc_aiohttp/websocket/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .abc import TimeoutType
from .common import ClientException, WSRPCBase
from .tools import Lazy, awaitable
from .tools import Lazy, awaitable, json_dumps


global_log = logging.getLogger("wsrpc")
Expand Down Expand Up @@ -71,7 +71,7 @@ def configure(
client_timeout=CLIENT_TIMEOUT,
max_concurrent_requests=MAX_CONCURRENT_REQUESTS,
loads=json.loads,
dumps=json.dumps,
dumps=json_dumps,
):
""" Configures the handler class

Expand Down
6 changes: 6 additions & 0 deletions wsrpc_aiohttp/websocket/tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import base64
import json
from functools import singledispatch, wraps


Expand Down Expand Up @@ -36,6 +37,10 @@ def _(value: MyObject) -> dict:
raise ValueError("Can not serialize %r" % type(value))


def json_dumps(*args, **kwargs) -> str:
return json.dumps(*args, **kwargs, default=serializer)


@serializer.register(bytes) # noqa: W0404
def _(value):
return base64.b64encode(value).decode()
Expand Down Expand Up @@ -80,4 +85,5 @@ async def wrap(*args, **kwargs):
"Singleton",
"awaitable",
"serializer",
"json_dumps",
)