Drop-in MessagePack support for ASGI applications and frameworks
Project description
msgpack-asgi
msgpack-asgi
allows you to add automatic MessagePack content negotiation to ASGI applications (Starlette, FastAPI, Quart, etc.), with a single line of code:
app.add_middleware(MessagePackMiddleware)
(You may want to adapt this snippet to your framework-specific middleware API.)
This gives you the bandwitdth usage reduction benefits of MessagePack without having to change existing code.
Note: this comes at a CPU usage cost, since MessagePackMiddleware
will perform MsgPack decoding while your application continues to decode and encode JSON data (see also How it works). If your use case is CPU-sensitive, rather than strictly focused on reducing network bandwidth, this package may not be for you.
Installation
Install with pip:
pip install "msgpack-asgi==1.*"
Quickstart
First, you'll need an ASGI application. Let's use this sample application, which exposes an endpoint that returns JSON data:
# For convenience, we use some ASGI components from Starlette.
# Install with: `$ pip install starlette`.
from starlette.requests import Request
from starlette.responses import JSONResponse
async def get_response(request):
if request.method == "POST":
data = await request.json()
return JSONResponse({"data": data}, status_code=201)
else:
return JSONResponse({"message": "Hello, msgpack!"})
async def app(scope, receive, send):
assert scope["type"] == "http"
request = Request(scope=scope, receive=receive)
response = await get_response(request)
await response(scope, receive, send)
Then, wrap your application around MessagePackMiddleware
:
from msgpack_asgi import MessagePackMiddleware
app = MessagePackMiddleware(app)
Serve your application using an ASGI server, for example with Uvicorn:
uvicorn app:app
Now, let's make a request that accepts MessagePack data in response:
curl -i http://localhost:8000 -H "Accept: application/x-msgpack"
You should get the following output:
HTTP/1.1 200 OK
date: Fri, 01 Nov 2019 17:40:14 GMT
server: uvicorn
content-length: 25
content-type: application/x-msgpack
��message�Hello, msgpack!
What happened? Since we told the application that we accepted MessagePack-encoded responses, msgpack-asgi
automatically converted the JSON data returned by the Starlette application to MessagePack.
We can make sure the response contains valid MessagePack data by making the request again in Python, and decoding the response content:
>>> import requests
>>> import msgpack
>>> url = "http://localhost:8000"
>>> headers = {"accept": "application/x-msgpack"}
>>> r = requests.get(url, headers=headers)
>>> r.content
b'\x81\xa7message\xafHello, msgpack!'
>>> msgpack.unpackb(r.content, raw=False)
{'message': 'Hello, msgpack!'}
msgpack-asgi
also works in reverse: it will automatically decode MessagePack-encoded data sent by the client to JSON. We can try this out by making a POST
request to our sample application with a MessagePack-encoded body:
>>> import requests
>>> import msgpack
>>> url = "http://localhost:8000"
>>> data = msgpack.packb({"message": "Hi, there!"})
>>> headers = {"content-type": "application/x-msgpack"}
>>> r = requests.post(url, data=data, headers=headers)
>>> r.json()
{'data': {'message': 'Hi, there!'}}
That's all there is to it! You can now go reduce the size of your payloads.
Advanced usage
Custom implementations
msgpack-asgi
supports customizing the default encoding/decoding implementation. This is useful for fine-tuning application performance via an alternative msgpack implementation for encoding, decoding, or both.
To do so, use the following arguments:
packb
- (Optional, type:(obj: Any) -> bytes
, default:msgpack.packb
) - Used to encode outgoing data.unpackb
- (Optional, type:(data: bytes) -> Any
, default:msgpack.unpackb
) - Used to decode incoming data.
For example, to use the ormsgpack
library for encoding:
import ormsgpack # Installed separately.
from msgpack_asgi import MessagePackMiddleware
def packb(obj):
option = ... # See `ormsgpack` options.
return ormsgpack.packb(obj, option=option)
app = MessagePackMiddleware(..., packb=packb)
Limitations
msgpack-asgi
does not support request or response streaming. This is because the full request and response body content has to be loaded in memory before it can be re-encoded.
How it works
An ASGI application wrapped around MessagePackMiddleware
will perform automatic content negotiation based on the client's capabilities. More precisely:
- If the client sends MessagePack-encoded data with the
application/x-msgpack
content type,msgpack-asgi
will automatically re-encode the body to JSON and re-write the requestContent-Type
toapplication/json
for your application to consume. (Note: this means applications will not be able to distinguish between MessagePack and JSON client requests.) - If the client sent the
Accept: application/x-msgpack
header,msgpack-asgi
will automatically re-encode any JSON response data to MessagePack for the client to consume.
(In other cases, msgpack-asgi
won't intervene at all.)
License
MIT
Changelog
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog.
1.1.0 - 2021-10-26
Added
- Support custom encoding/decoding implementation via the
packb=...
andunpackb=...
optional parameters, allowing the use of alternative msgpack libraries. (Pull #20)
Fixed
- Properly re-write request
Content-Type
toapplication/json
. (Pull #24)
1.0.0 - 2020-03-26
First production/stable release.
Changed
- Switch to private module naming. Components should now be imported from the root package, e.g.
from msgpack_asgi import MessagePackMiddleware
. (Pull #5)
Fixed
- Add missing
MANIFEST.in
. (Pull #4)
0.1.0 - 2019-11-04
Initial release.
Added
- Add the
MessagePackMiddleware
ASGI middleware. - Add the
MessagePackResponse
ASGI response class.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file msgpack-asgi-1.1.0.tar.gz
.
File metadata
- Download URL: msgpack-asgi-1.1.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ed5a2f4050f28dd9388d38555f059ce05498f176642053ba9f08bb7a1e02221 |
|
MD5 | a5126b7f6d41f0d4feb6642300047918 |
|
BLAKE2b-256 | 4840c1f3ede818355d95f87666fb245a8538203081c6a0c9e39c3ca9ebd01da8 |
File details
Details for the file msgpack_asgi-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: msgpack_asgi-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47fe50fb58b310c1f73721406638a790bdf565ae555e09587c3ddfbe7eb9da8d |
|
MD5 | f723aaa3b5a70f0c9e32dfef7b3a74f7 |
|
BLAKE2b-256 | 44ff132685705f94f17afbeb471f3930ca1b67234aefa8e10c44adac8d9fb733 |