Files
turbosloth/src/turbosloth/interfaces/serialized/msgpack.py

29 lines
942 B
Python

import msgpack
from case_insensitive_dict import CaseInsensitiveDict
from turbosloth.interfaces.base import BasicRequest, BasicResponse
from .base import SerializedRequest, SerializedResponse
class MessagePackSerializedRequest(SerializedRequest):
@classmethod
async def deserialize(cls, basic: BasicRequest, charset: str) -> SerializedRequest:
body = await basic.fetch_full_body()
if len(body) == 0:
b = None
else:
b = msgpack.unpackb(body)
return cls(b, basic, charset)
class MessagePackSerializedResponse(SerializedResponse):
def into_basic(self, charset: str) -> BasicResponse:
headers = CaseInsensitiveDict(self.headers).copy()
headers['content-type'] = 'application/vnd.msgpack'
b = msgpack.packb(self.body)
return BasicResponse(self.code, headers, b)
def into_ws(self) -> bytes | str:
return msgpack.packb(self.body)