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.0.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.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: RESTModels-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 07c713837caafc71b802eb81b18916f1505ed6ea25b937cc028b87a6f2da20f3
MD5 eb99c0c3fbd553d72c84a5833a93825e
BLAKE2b-256 3d49195476cdb08fbba09ac19a345c7263cb388fe1d59ba9f0a7f5f537a1b58e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: RESTModels-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00537a7a077344cfe4014c76e2836372b72e0f1945d36baa95a6ea594edfac9b
MD5 01f7189a83d954edf6a9a2ef2b5ba6e1
BLAKE2b-256 07996f07d98ed6fe5d7a96dae53aa5eeb319d0f86a4f02d79e6b8293647f2243

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