drf_msgpack: add code to parse/serialise msgpack
It's not actually used by clients but it's there and can be used. It works for receiving msgpack messages, but doesn't yet work for sending because some of the types will be converted to base64.
This commit is contained in:
0
django_etebase/drf_msgpack/__init__.py
Normal file
0
django_etebase/drf_msgpack/__init__.py
Normal file
5
django_etebase/drf_msgpack/apps.py
Normal file
5
django_etebase/drf_msgpack/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DrfMsgpackConfig(AppConfig):
|
||||
name = 'drf_msgpack'
|
||||
0
django_etebase/drf_msgpack/migrations/__init__.py
Normal file
0
django_etebase/drf_msgpack/migrations/__init__.py
Normal file
14
django_etebase/drf_msgpack/parsers.py
Normal file
14
django_etebase/drf_msgpack/parsers.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import msgpack
|
||||
|
||||
from rest_framework.parsers import BaseParser
|
||||
from rest_framework.exceptions import ParseError
|
||||
|
||||
|
||||
class MessagePackParser(BaseParser):
|
||||
media_type = 'application/msgpack'
|
||||
|
||||
def parse(self, stream, media_type=None, parser_context=None):
|
||||
try:
|
||||
return msgpack.unpackb(stream.read(), raw=False)
|
||||
except Exception as exc:
|
||||
raise ParseError('MessagePack parse error - %s' % str(exc))
|
||||
15
django_etebase/drf_msgpack/renderers.py
Normal file
15
django_etebase/drf_msgpack/renderers.py
Normal file
@@ -0,0 +1,15 @@
|
||||
import msgpack
|
||||
|
||||
from rest_framework.renderers import BaseRenderer
|
||||
|
||||
|
||||
class MessagePackRenderer(BaseRenderer):
|
||||
media_type = 'application/msgpack'
|
||||
format = 'msgpack'
|
||||
render_style = 'binary'
|
||||
charset = None
|
||||
|
||||
def render(self, data, media_type=None, renderer_context=None):
|
||||
if data is None:
|
||||
return b''
|
||||
return msgpack.packb(data, use_bin_type=True)
|
||||
3
django_etebase/drf_msgpack/views.py
Normal file
3
django_etebase/drf_msgpack/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user