BLEST Python
The Python reference implementation of BLEST (Batch-able, Lightweight, Encrypted State Transfer), an improved communication protocol for web APIs which leverages JSON, supports request batching and selective returns, and provides a modern alternative to REST. It includes examples for Django, FastAPI, and Flask.
To learn more about BLEST, please refer to the white paper: https://jhunt.dev/BLEST%20White%20Paper.pdf
For a front-end implementation in React, please visit https://github.com/jhunt/blest-react
Features
- Built on JSON - Reduce parsing time and overhead
- Request Batching - Save bandwidth and reduce load times
- Compact Payloads - Save more bandwidth
- Selective Returns - Save even more bandwidth
- Single Endpoint - Reduce complexity and improve data privacy
- Fully Encrypted - Improve data privacy
Installation
Install BLEST Python from PyPI.
python3 -m pip install blest
Usage
Use the create_request_handler function to create a request handler suitable for use in an existing Python application. Use the create_http_server function to create a standalone HTTP server for your request handler. Use the create_http_client to create BLEST HTTP clients to interact with other servers.
create_request_handler
The following example uses Flask, but you can find examples with other frameworks here.
from flask import Flask, make_response, request
from blest import create_request_handler
# Create some middleware (optional)
async def auth_middleware(params, context):
if params['name']:
context['user'] = {
'name': params['name']
}
else:
raise Exception('Unauthorized')
# Create a route controller
async def greet_controller(params, context):
return {
'greeting': f"Hi, {context['user']['name']}!"
}
# Create a request_handler
request_handler = create_request_handler({
'greet': [auth_middleware, greet_controller]
})
app = Flask(__name__)
# Use the request handler
@app.post('/')
async def index():
result, error = await request_handler(request.json)
if error:
resp = make_response(error, 500)
resp.headers['Content-Type'] = 'application/json'
else:
resp = make_response(result, 200)
resp.headers['Content-Type'] = 'application/json'
return resp
create_http_server
from blest import create_http_server, create_request_handler
# Create some middleware (optional)
async def auth_middleware(params, context):
if params['name']:
context['user'] = {
'name': params['name']
}
else:
raise Exception('Unauthorized')
# Create a route controller
async def greet_controller(params, context):
return {
'greeting': f"Hi, {context['user']['name']}!"
}
# Create a request_handler
request_handler = create_request_handler({
'greet': [auth_middleware, greet_controller]
})
run = create_http_server(request_handler)
if __name__ == '__main__':
run()
create_http_client
from blest import create_http_client
# Create a client
request = create_http_client('http://localhost:8080')
async def main():
# Send a request
try:
result = await request('greet', { 'name': 'Steve' }, ['greeting'])
# Do something with the result
except Exception as error:
# Do something in case of error
Contributing
We actively welcome pull requests. Learn how to contribute for more information.
License
This project is licensed under the MIT License.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file BLEST-0.0.6.tar.gz.
File metadata
- Download URL: BLEST-0.0.6.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d651fc075e6bbf31016177a188711a80143810a7a04f3940fc3f9b0f25a5d814
|
|
| MD5 |
4113d6bafdc1e74c85c7ad2b07d25aff
|
|
| BLAKE2b-256 |
aee3a53ae79628cdeafb6882c53714e440bdcc8dad37f3875effffbc44ae0b23
|
File details
Details for the file BLEST-0.0.6-py3-none-any.whl.
File metadata
- Download URL: BLEST-0.0.6-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49b0a63647ffc87f317fb5d4c5c7990bd91635b7f5187aedd736f74d22576f7c
|
|
| MD5 |
5d7fec6471a6917bfd56ac3a9cff145d
|
|
| BLAKE2b-256 |
347581a73a53768e5d82858bd8ab0a63cd5f99b2589aa59a68b7ebc089317ae7
|