drakaina


Namedrakaina JSON
Version 0.7.6 PyPI version JSON
download
home_pagehttps://gitlab.com/tau_lex/drakaina
SummaryModule for simple RPC service implementation
upload_time2024-06-19 00:30:59
maintainerNone
docs_urlNone
authorAleksey Terentyev
requires_python<4.0,>=3.9
licenseApache-2.0
keywords rpc jsonrpc openrpc
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p style="text-align: center;"><img src="https://gitlab.com/tau_lex/drakaina/-/raw/main/content/drakaina300.png" style="" /></p>
<h2 style="text-align: center;">drakaina</h2>

[![image](https://img.shields.io/pypi/v/drakaina.svg)](https://pypi.python.org/pypi/drakaina)
[![image](https://img.shields.io/pypi/l/drakaina.svg)](https://pypi.python.org/pypi/drakaina)
[![image](https://img.shields.io/pypi/pyversions/drakaina.svg)](https://pypi.python.org/pypi/drakaina)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)
[![Code style: black](https://img.shields.io/badge/code%20style-black-black.svg)](https://github.com/psf/black)
[![OpenRPC](https://img.shields.io/endpoint?url=https%3A%2F%2Fgitlab.com%2Ftau_lex%2Fdrakaina%2F-%2Fraw%2Fmain%2Fcontent%2Fopenrpc-badge.json)](https://open-rpc.org)
[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)

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](https://palletsprojects.com/p/werkzeug/),
    [Flask](https://palletsprojects.com/p/flask/)
- `login_required` and `check_permissions` decorators.


## Installation and Dependencies

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

```shell
pip install drakaina
```

## Usage Examples

A minimal Drakaina example is:

```python
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

```shell
pip install drakaina[jwt, orjson, ujson]
```


## Middlewares


### CORS


### JWT

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

```shell
pip install drakaina[jwt]
```

Example of using Drakaina:

```python
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](http://gunicorn.org).

```shell
gunicorn main:app
```

or ran with any ASGI-compliant server

```shell
uvicorn main:app2
```


### Using with Django

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

```python
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.

```python
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`.

```python
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.

```python
...

DRAKAINA_JWT_SECRET_KEY = "__SECRET_KEY__"

...
```


## License

Apache License 2.0

## Artwork

"[drakaina.png](content/drakaina.png)" by Korolko Anastasia is licensed under
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="License Creative Commons" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png" /></a> ([CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)).

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.com/tau_lex/drakaina",
    "name": "drakaina",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "rpc, jsonrpc, openrpc",
    "author": "Aleksey Terentyev",
    "author_email": "terentyev.a@pm.me",
    "download_url": "https://files.pythonhosted.org/packages/8c/02/3c3385575ceaba91b92a3320a056810cc718abf0de46e217796729e71cf1/drakaina-0.7.6.tar.gz",
    "platform": null,
    "description": "<p style=\"text-align: center;\"><img src=\"https://gitlab.com/tau_lex/drakaina/-/raw/main/content/drakaina300.png\" style=\"\" /></p>\n<h2 style=\"text-align: center;\">drakaina</h2>\n\n[![image](https://img.shields.io/pypi/v/drakaina.svg)](https://pypi.python.org/pypi/drakaina)\n[![image](https://img.shields.io/pypi/l/drakaina.svg)](https://pypi.python.org/pypi/drakaina)\n[![image](https://img.shields.io/pypi/pyversions/drakaina.svg)](https://pypi.python.org/pypi/drakaina)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v1.json)](https://github.com/charliermarsh/ruff)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-black.svg)](https://github.com/psf/black)\n[![OpenRPC](https://img.shields.io/endpoint?url=https%3A%2F%2Fgitlab.com%2Ftau_lex%2Fdrakaina%2F-%2Fraw%2Fmain%2Fcontent%2Fopenrpc-badge.json)](https://open-rpc.org)\n[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)\n\nFramework for simple RPC service implementation.\n\n\n## Features\n\n- Serializers layer.\n  - `json`, `orjson`, `ujson` and `msgpack` serializers.\n- Generates schemas for documentation in OpenRPC format.\n- WSGI protocol implementation\n  - CORS middleware\n  - JWT Authorization middleware.\n  - Compatible with middlewares for others wsgi-frameworks,\n    like as [Werkzeug](https://palletsprojects.com/p/werkzeug/),\n    [Flask](https://palletsprojects.com/p/flask/)\n- `login_required` and `check_permissions` decorators.\n\n\n## Installation and Dependencies\n\nDrakaina may be installed via `pip` and requires Python 3.8 or higher :\n\n```shell\npip install drakaina\n```\n\n## Usage Examples\n\nA minimal Drakaina example is:\n\n```python\nfrom drakaina import remote_procedure\nfrom drakaina.wsgi import WSGIHandler\n\n@remote_procedure(\"hello\")\ndef hello_method(name):\n    return f\"Hello, {name}!\"\n\n\"\"\"\n>>> from drakaina.rpc_protocols import JsonRPCv2\n>>> JsonRPCv2().handle({\"jsonrpc\": \"2.0\", \"method\": \"hello\", \"params\": [\"\ud83d\udc0d Python\"] \"id\": 1})\n{\"jsonrpc\": \"2.0\", \"result\": \"Hello, \ud83d\udc0d Python!\", \"id\": 1}\n\"\"\"\n\n# Or define WSGI application\napp = WSGIHandler(route=\"/jrpc\")\n\n```\n\n\n# Documentation\n\n\n### Optional requirements\n\n```shell\npip install drakaina[jwt, orjson, ujson]\n```\n\n\n## Middlewares\n\n\n### CORS\n\n\n### JWT\n\nDrakaina may be installed via `pip` and requires Python 3.7 or higher :\n\n```shell\npip install drakaina[jwt]\n```\n\nExample of using Drakaina:\n\n```python\nfrom functools import partial\nfrom drakaina import check_permissions\nfrom drakaina import ENV_IS_AUTHENTICATED\nfrom drakaina import ENV_USER_ID\nfrom drakaina import login_required\nfrom drakaina import match_any\nfrom drakaina import remote_procedure\nfrom drakaina.contrib.jwt.middleware import JWTAuthenticationMiddleware\nfrom drakaina.wsgi import WSGIHandler\n\nimport user_store\n\n\n@login_required\n@remote_procedure(provide_request=True)\ndef my_method(request):\n    assert request[ENV_IS_AUTHENTICATED]\n    return f\"Hello Bro \u270b! Your ID={request[ENV_USER_ID]}\"\n\n\n@check_permissions([\"user_read\", \"user:admin\", \"username:johndoe\"], match_any)\n@remote_procedure\ndef my_method():\n    return \"Hello Bro! \u270b\ufe0f\"\n\n\ndef get_user(request, payload):\n    user_id = request[ENV_USER_ID] or payload[\"user_id\"]\n    return user_store.get(id=user_id)\n\n\ndef get_jwt_scopes(request, payload):\n    # here `scp` is the key for the scopes value in the token payload\n    return payload.get(\"scp\")\n\n\napp = WSGIHandler(\n    middlewares=[\n        partial(\n            JWTAuthenticationMiddleware,\n            secret_phrase=\"_secret_\",\n            credentials_required=True,\n            auth_scheme=\"Bearer\",\n            # token_getter=custom_implementation_get_token,\n            user_getter=get_user,\n            scopes_getter=get_jwt_scopes,\n            # revoke_checker=is_revoked,\n        )\n    ]\n)\n```\n\nDrakaina may be ran with any WSGI-compliant server,\nsuch as [Gunicorn](http://gunicorn.org).\n\n```shell\ngunicorn main:app\n```\n\nor ran with any ASGI-compliant server\n\n```shell\nuvicorn main:app2\n```\n\n\n### Using with Django\n\nCreate file `rpc_views.py` in your django application.\nDefine function and wrap it `remote_procedure` decorator:\n\n```python\nfrom drakaina import remote_procedure\n\n@remote_procedure\ndef my_method():\n    return \"Hello, Django Bro! \u270b\"\n```\n\nAdd `RPCView` class to urlpatterns. The `as_view` method\nmust accept the `autodiscover` argument as the name of\nthe remote procedure files.\n\n```python\nfrom django.urls import path\nfrom drakaina.contrib.django.views import RPCView\n\nurlpatterns = [\n    ...,\n    path(\"api/\", RPCView.as_view(autodiscover=\"rpc_views\")),\n]\n```\n\n\n### JWT Authentication in your Django project\n\nWrap an instance of `RPCView` with the `JWTAuthenticationMiddleware`.\n\n```python\nfrom django.urls import path\nfrom drakaina.contrib.django import RPCView, JWTAuthenticationMiddleware\n\nurlpatterns = [\n    ...,\n    path(\"api/\", JWTAuthenticationMiddleware(\n        RPCView.as_view(autodiscover=\"rpc_views\")\n    )),\n]\n```\n\nDefine the parameters in the `settings.py` file.\n\n```python\n...\n\nDRAKAINA_JWT_SECRET_KEY = \"__SECRET_KEY__\"\n\n...\n```\n\n\n## License\n\nApache License 2.0\n\n## Artwork\n\n\"[drakaina.png](content/drakaina.png)\" by Korolko Anastasia is licensed under\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"License Creative Commons\" style=\"border-width:0\" src=\"https://i.creativecommons.org/l/by-sa/4.0/80x15.png\" /></a> ([CC BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Module for simple RPC service implementation",
    "version": "0.7.6",
    "project_urls": {
        "Homepage": "https://gitlab.com/tau_lex/drakaina",
        "Repository": "https://gitlab.com/tau_lex/drakaina"
    },
    "split_keywords": [
        "rpc",
        " jsonrpc",
        " openrpc"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e780891b0ae01bf81a6a0b9903ea0dc05a756fb4d0dae1c68e27d146d553601c",
                "md5": "6b67832ee2a56a68f98241f740dad766",
                "sha256": "4f809c133da40a0e6383b4ee935bb3cdcaa64840fe9d52780e3f59e11776d53d"
            },
            "downloads": -1,
            "filename": "drakaina-0.7.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6b67832ee2a56a68f98241f740dad766",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 47819,
            "upload_time": "2024-06-19T00:30:54",
            "upload_time_iso_8601": "2024-06-19T00:30:54.330722Z",
            "url": "https://files.pythonhosted.org/packages/e7/80/891b0ae01bf81a6a0b9903ea0dc05a756fb4d0dae1c68e27d146d553601c/drakaina-0.7.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c023c3385575ceaba91b92a3320a056810cc718abf0de46e217796729e71cf1",
                "md5": "3b890289a9a8fb952920fa10ba57fc78",
                "sha256": "0758ca3b1d8a4342ef9ffd0ca25aa7915b03852684a4e766752aaba18a8c102b"
            },
            "downloads": -1,
            "filename": "drakaina-0.7.6.tar.gz",
            "has_sig": false,
            "md5_digest": "3b890289a9a8fb952920fa10ba57fc78",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 38853,
            "upload_time": "2024-06-19T00:30:59",
            "upload_time_iso_8601": "2024-06-19T00:30:59.248731Z",
            "url": "https://files.pythonhosted.org/packages/8c/02/3c3385575ceaba91b92a3320a056810cc718abf0de46e217796729e71cf1/drakaina-0.7.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-19 00:30:59",
    "github": false,
    "gitlab": true,
    "bitbucket": false,
    "codeberg": false,
    "gitlab_user": "tau_lex",
    "gitlab_project": "drakaina",
    "lcname": "drakaina"
}
        
Elapsed time: 0.33466s