Note: This package is in the dangerous land of
0.x.yversions and may be subject to breaking changes with minor version increments.
This package is superseded by astra.http.
nr.databind.rest
This module can be used to define a REST API interface using Python 3
annotations and nr.databind object mapping features.
Todo
- Dynamic and static code generation for REST clients
Example
Resources are created by defining an Interface subclass and decorating its
methods with the @Route decorator. The subclass can then be used to
automatically generate client code and to implement a server based on the
same interface.
from my.service.api.types import MyRequest, MyResponse
from nr.databind.rest import Route, RouteParam
from nr.interface import Interface
class MyResource(Interface):
@Route('GET /my-endpoint/{parameter_a}')
def my_get_endpoint(self, parameter_a: str, parameter_b: RouteParam.Query(int)) -> MyResponse:
...
@Route('POST /my-endpoint/{parameter_a}')
def my_post_endpoint(self, parameter_a: str, body: MyRequest) -> MyResponse:
...
The resource is then implemented like this:
from my.service.api.resources import MyResource
from my.service.api.types import MyResponse
from nr.interface import implements
@implements(MyResource)
class MyResourceImpl:
def my_get_endpoint(self, parameter_a, parameter_b):
# ...
return MyResponse(...)
def my_post_endpoint(self, parameter_a, body):
# ...
return MyResponse(...)
Resources can then be bound to a server framework using one of the available bind mechanisms (currently only Flask is available).
from flask import Flask
from my.service.api.resources_impl import MyResourceImpl
from nr.databind.rest import MimeTypeMapper
from nr.databind.rest.flask import bind_resource
app = Flask(__name__)
mapper = MimeTypeMapper.json()
bind_resource(app, '/my-resource', MyResourceImpl(), mapper=mapper)
Copyright © 2020 Niklas Rosenstein
Metadata
Release files for nr.databind.rest 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nr.databind.rest-0.0.3.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nr.databind.rest-0.0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.6 kB
Release files / nr.databind.rest-0.0.3.tar.gz
| Download URL | nr.databind.rest-0.0.3.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a234596683c4f2d7708149e24f31b04cd272c35d4bff7872dca6d12bdc313bcc
|
|
BLAKE2b-256 checksum How to use checksums |
a43e64b6f3657767a0e79718c4d382bfea88a222fd3e4d44b99ff3f6489104b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3
|
Release files / nr.databind.rest-0.0.3-py3-none-any.whl
| Download URL | nr.databind.rest-0.0.3-py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3809b73d075ae15af281b10b15ec36a1ef8e745556ff7997841b78b1be0d8487
|
|
BLAKE2b-256 checksum How to use checksums |
d1ce81a5ad570336bbce9f84feb69adcf089c79ffb6c709d98eaee24cdd37590
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3
|