Skip to main content

API client by resource models for RESTful API over JSON

Project description

About

Simple sync/async client for REST API over JSON

PyPI: https://pypi.org/project/RESTModels/

Usage

  • install package
pip install RESTModels
  • Create models
from RESTModels import ResourceModel, SyncClient, get, post


class Model(ResourceModel):
    @get("/todos/{count}")
    def get_todos(self, count: int) -> list[str]:
        ...

    @post("/todo", body=("todo",))
    def make_todo(self, todo: str) -> None:
        ...
  • Create instance and use it
client = SyncClient("http://localhost:8000")
model = Model(client)
print(model.make_todo("Info"))
print(model.get_todos(1))

More usage

Custom type parsers

Create any object that implement protocol:

class TypeParserProtocol(Protocol):
    def __call__(
            self,
            value: Any,
            alias: GenericAlias,
            alias_parser: "TypeAliasParser",
    ) -> Any:

        raise NotImplementedError

And register it as type parser for TypeAliasParser:

from RESTModels import register_general_type_parser


@register_general_type_parser  # For all TypeAliasParser objects
def str_alias_parser(value: Any, alias: GenericAlias, alias_parser: TypeAliasParser) -> str:
    return str(value)
  • value: response body as json.loads(request.body)
  • alias: GenericAlias that describes expected result type
  • alias_parser: TypeAliasParser object that called your type parser. Use it for parse nested types as showed lower.

You can parse generic types like that:

@TypeAliasParser.register_general_type_parser
def list_alias_parser(value: Any, alias: GenericAlias, alias_parser: TypeAliasParser) -> list[Any]:
    if hasattr(alias, "__args__"):
        origin_type_alias = alias.__args__[0]
        return [alias_parser(elem, origin_type_alias) for elem in value]

    return list(value)

If your return type is generic, annotate it like Generic[Any] for static type checkers, as showed above

Type of value that type parser returns determined by return type annotation

If you register more than one parser for one type only last will be used

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

RESTModels-1.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

RESTModels-1.0.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: RESTModels-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for RESTModels-1.0.1.tar.gz
Algorithm Hash digest
SHA256 81ed8a46b0392e1fc1a9d85439a7fc5a4fe77e79bf1f6258e0224c84716e2c9c
MD5 20240713c987efc934210ae586292156
BLAKE2b-256 5e72d8469e6377a039420e2ab64d5bb050b6f129a1602038cdda5533539ff885

See more details on using hashes here.

File details

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

File metadata

  • Download URL: RESTModels-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for RESTModels-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1daebb53acc4ef2692a66f8625ada96ae5c6c73f43ffc9c07cf054dc4a532e6a
MD5 2e7dc47c300bd96a0adeecf93739dcfb
BLAKE2b-256 929f497b5000ca6220ccf83ad888a159208f1aa52345b03919b201d738170b60

See more details on using hashes here.

Supported by

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