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

@ -47,7 +47,9 @@ def is_collection_admin(collection, user):
def msgpack_encode(content): 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): def msgpack_decode(content):

Loading…
Cancel
Save