Skip to main content

Module for simple RPC service implementation

Project description

drakaina

image image image Ruff Code style: black OpenRPC libera manifesto

Framework for simple RPC service implementation.

Features

  • Serializers layer.
    • json, orjson, ujson and msgpack serializers.
  • Generates schemas for documentation in OpenRPC format.
  • WSGI protocol implementation
    • CORS middleware
    • JWT Authorization middleware.
    • Compatible with middlewares for others wsgi-frameworks, like as Werkzeug, Flask
  • login_required and check_permissions decorators.

Installation and Dependencies

Drakaina may be installed via pip and requires Python 3.8 or higher :

pip install drakaina

Usage Examples

A minimal Drakaina example is:

from drakaina import remote_procedure
from drakaina.wsgi import WSGIHandler

@remote_procedure("hello")
def hello_method(name):
    return f"Hello, {name}!"

"""
>>> from drakaina.rpc_protocols import JsonRPCv2
>>> JsonRPCv2().handle({"jsonrpc": "2.0", "method": "hello", "params": ["🐍 Python"] "id": 1})
{"jsonrpc": "2.0", "result": "Hello, 🐍 Python!", "id": 1}
"""

# Or define WSGI application
app = WSGIHandler(route="/jrpc")

Documentation

Optional requirements

pip install drakaina[jwt, orjson, ujson]

Middlewares

CORS

JWT

Drakaina may be installed via pip and requires Python 3.7 or higher :

pip install drakaina[jwt]

Example of using Drakaina:

from functools import partial
from drakaina import check_permissions
from drakaina import ENV_IS_AUTHENTICATED
from drakaina import ENV_USER_ID
from drakaina import login_required
from drakaina import match_any
from drakaina import remote_procedure
from drakaina.contrib.jwt.middleware import JWTAuthenticationMiddleware
from drakaina.wsgi import WSGIHandler

import user_store


@login_required
@remote_procedure(provide_request=True)
def my_method(request):
    assert request[ENV_IS_AUTHENTICATED]
    return f"Hello Bro ✋! Your ID={request[ENV_USER_ID]}"


@check_permissions(["user_read", "user:admin", "username:johndoe"], match_any)
@remote_procedure
def my_method():
    return "Hello Bro! ✋️"


def get_user(request, payload):
    user_id = request[ENV_USER_ID] or payload["user_id"]
    return user_store.get(id=user_id)


def get_jwt_scopes(request, payload):
    # here `scp` is the key for the scopes value in the token payload
    return payload.get("scp")


app = WSGIHandler(
    middlewares=[
        partial(
            JWTAuthenticationMiddleware,
            secret_phrase="_secret_",
            credentials_required=True,
            auth_scheme="Bearer",
            # token_getter=custom_implementation_get_token,
            user_getter=get_user,
            scopes_getter=get_jwt_scopes,
            # revoke_checker=is_revoked,
        )
    ]
)

Drakaina may be ran with any WSGI-compliant server, such as Gunicorn.

gunicorn main:app

or ran with any ASGI-compliant server

uvicorn main:app2

Using with Django

Create file rpc_views.py in your django application. Define function and wrap it remote_procedure decorator:

from drakaina import remote_procedure

@remote_procedure
def my_method():
    return "Hello, Django Bro! ✋"

Add RPCView class to urlpatterns. The as_view method must accept the autodiscover argument as the name of the remote procedure files.

from django.urls import path
from drakaina.contrib.django.views import RPCView

urlpatterns = [
    ...,
    path("api/", RPCView.as_view(autodiscover="rpc_views")),
]

JWT Authentication in your Django project

Wrap an instance of RPCView with the JWTAuthenticationMiddleware.

from django.urls import path
from drakaina.contrib.django import RPCView, JWTAuthenticationMiddleware

urlpatterns = [
    ...,
    path("api/", JWTAuthenticationMiddleware(
        RPCView.as_view(autodiscover="rpc_views")
    )),
]

Define the parameters in the settings.py file.

...

DRAKAINA_JWT_SECRET_KEY = "__SECRET_KEY__"

...

License

Apache License 2.0

Artwork

"drakaina.png" by Korolko Anastasia is licensed under License Creative Commons (CC BY-SA 4.0).

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

drakaina-0.7.6.tar.gz (38.9 kB view details)

Uploaded Source

Built Distribution

drakaina-0.7.6-py3-none-any.whl (47.8 kB view details)

Uploaded Python 3

File details

Details for the file drakaina-0.7.6.tar.gz.

File metadata

  • Download URL: drakaina-0.7.6.tar.gz
  • Upload date:
  • Size: 38.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Windows/10

File hashes

Hashes for drakaina-0.7.6.tar.gz
Algorithm Hash digest
SHA256 0758ca3b1d8a4342ef9ffd0ca25aa7915b03852684a4e766752aaba18a8c102b
MD5 3b890289a9a8fb952920fa10ba57fc78
BLAKE2b-256 8c023c3385575ceaba91b92a3320a056810cc718abf0de46e217796729e71cf1

See more details on using hashes here.

File details

Details for the file drakaina-0.7.6-py3-none-any.whl.

File metadata

  • Download URL: drakaina-0.7.6-py3-none-any.whl
  • Upload date:
  • Size: 47.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.0 Windows/10

File hashes

Hashes for drakaina-0.7.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4f809c133da40a0e6383b4ee935bb3cdcaa64840fe9d52780e3f59e11776d53d
MD5 6b67832ee2a56a68f98241f740dad766
BLAKE2b-256 e780891b0ae01bf81a6a0b9903ea0dc05a756fb4d0dae1c68e27d146d553601c

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