From 62eb46ec4ef036673b1864c871aa477cf6fde002 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 29 Dec 2020 15:44:52 +0200 Subject: [PATCH] msgpack route: use the encode/decode functions from the utils module. --- etebase_fastapi/msgpack.py | 9 ++++----- etebase_fastapi/utils.py | 4 +++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/etebase_fastapi/msgpack.py b/etebase_fastapi/msgpack.py index 915e783..8de8806 100644 --- a/etebase_fastapi/msgpack.py +++ b/etebase_fastapi/msgpack.py @@ -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): diff --git a/etebase_fastapi/utils.py b/etebase_fastapi/utils.py index c91c3ec..3a091c5 100644 --- a/etebase_fastapi/utils.py +++ b/etebase_fastapi/utils.py @@ -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):