msgpack route: use the encode/decode functions from the utils module.

master
Tom Hacohen 4 years ago
parent dbdff06e68
commit 62eb46ec4e

@ -1,10 +1,11 @@
import typing as t
import msgpack
from fastapi.routing import APIRoute, get_request_handler
from pydantic import BaseModel
from starlette.requests import Request
from starlette.responses import Response
from .utils import msgpack_encode, msgpack_decode
class MsgpackRequest(Request):
media_type = "application/msgpack"
@ -12,7 +13,7 @@ class MsgpackRequest(Request):
async def json(self) -> bytes:
if not hasattr(self, "_json"):
body = await super().body()
self._json = msgpack.unpackb(body, raw=False)
self._json = msgpack_decode(body)
return self._json
@ -25,9 +26,7 @@ class MsgpackResponse(Response):
if isinstance(content, BaseModel):
content = content.dict()
ret = msgpack.packb(content, use_bin_type=True)
assert ret is not None
return ret
return msgpack_encode(content)
class MsgpackRoute(APIRoute):

@ -47,7 +47,9 @@ def is_collection_admin(collection, user):
def msgpack_encode(content):
return msgpack.packb(content, use_bin_type=True)
ret = msgpack.packb(content, use_bin_type=True)
assert ret is not None
return ret
def msgpack_decode(content):

Loading…
Cancel
Save