Skip to main content

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 by default, and provides a modern alternative to REST.

Project description

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 by default, and provides a modern alternative to REST. It includes examples for Django, FastAPI, and Flask.

To learn more about BLEST, please visit the website: https://blest.jhunt.dev

For a front-end implementation in React, please visit https://github.com/jhuntdev/blest-react

Features

  • Built on JSON - Reduce parsing time and overhead
  • Request Batching - Save bandwidth and reduce load times
  • Compact Payloads - Save even more bandwidth
  • Single Endpoint - Reduce complexity and facilitate introspection
  • Fully Encrypted - Improve data privacy

Installation

Install BLEST Python from PyPI.

python3 -m pip install blest

Usage

Router

The following example uses Flask, but you can find examples with other frameworks here.

from flask import Flask, make_response, request
from blest import Router

# Instantiate the Router
router = Router({ 'timeout': 1000 })

# Create some middleware (optional)
@router.before_request
async def auth_middleware(body, context):
  if context['headers']['auth'] == 'myToken':
    context['user'] = {
      # user info for example
    }
    return
  else:
    raise Exception('Unauthorized')

# Create a route controller
@router.route('greet')
async def greet_controller(body, context):
  return {
    'greeting': f"Hi, {body['name']}!"
  }

# Instantiate a Flask application
app = Flask(__name__)

# Handle BLEST requests
@app.post('/')
async def index():
  result, error = await router.handle(request.json, { 'headers': request.headers })
  if error:
    resp = make_response(error, error.status or 500)
    resp.headers['Content-Type'] = 'application/json'
  else:
    resp = make_response(result, 200)
    resp.headers['Content-Type'] = 'application/json'
    return resp

HttpClient

from blest import HttpClient

async def main():
  # Create a client
  client = HttpClient('http://localhost:8080', {
    'max_batch_size': 25,
    'buffer_delay': 10,
    'http_headers': {
      'Authorization': 'Bearer token'
    }
  })

  # Send a request
  try:
    result = await client.request('greet', { 'name': 'Steve' }, { 'auth': 'myToken' })
    # Do something with the result
  except Exception as error:
    # Do something in case of error

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blest-1.0.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

blest-1.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file blest-1.0.1.tar.gz.

File metadata

  • Download URL: blest-1.0.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for blest-1.0.1.tar.gz
Algorithm Hash digest
SHA256 fa198b0adfd9e98bdbb6584a235c9d4d9296dabc63a752dcea0d21f7bcd052c3
MD5 e7d9a7a47d4879dd0b70ff475381d8ec
BLAKE2b-256 bb2b81bdcac6c08907259812dc0d8be6c0d203cc0f523c2c0bc3b85d807c9804

See more details on using hashes here.

File details

Details for the file blest-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: blest-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for blest-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d86e3d6eb95ceb359f8e6e7685136ccb974d974ea2287ea2d56f116c66100bb5
MD5 f578e6b861635be808692b3dac47b577
BLAKE2b-256 2135a561adf057c95c5cedd10b226ff4d1cebc88b9060611e1c6c6245539b686

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page