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 alias 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 alias 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

Make sure that return type annotation does not turn into the type you do not expect. Example:

>>> Union[Any]
typing.Any
>>> Any | Any
typing.Any

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.1.1.tar.gz (6.2 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.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for RESTModels-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0542a156475d4fc5b8b0fb605aad16ec4f6d105d9f11c70f654026cf2b522c28
MD5 64744c313b34fbeb77bbe3d8a1acbc5e
BLAKE2b-256 06347b3f741697c76bd6b4898fd6198df145afdbf503dfe84c5bbbf5c4093146

See more details on using hashes here.

File details

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

File metadata

  • Download URL: RESTModels-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 195685a059a27fe2ef30598b8b557f67162724917cc7543b63cf95f0af0c4608
MD5 5c76f558391c95b54f5c5ec4981edb9d
BLAKE2b-256 db1353b6810ef5100ff1ec33e932c3c69e8f472313a57352b50be91db127bf9f

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