Skip to main content

Modern python http client

Project description

Retejo

A modern and simple way to create clients for REST like APIs

Advantages

  1. Fully typable library.
  2. Supports async and sync.
  3. Simple and intuitive user API.

Quickstart

Step 1. Install library.

pip install retejo requests

Step 2. Declare model.

@dataclass
class Post:
    id: int
    title: str
    body: str
    user_id: int

Step 3. Declare methods.

class GetPost(Method[Post]):
    __url__ = "posts/{id}"
    __method__ = "get"

    id: UrlVar[int]


class CreatePost(Method[PostId]):
    __url__ = "posts"
    __method__ = "post"

    user_id: Body[int]
    title: Body[str]
    body: Body[str]

Step 4. Declare client.

class Client(RequestsClient):
    def __init__(self) -> None:
        super().__init__(base_url="https://jsonplaceholder.typicode.com/")

    @override
    def _init_response_factory(self) -> Retort:
        retort = super()._init_response_factory()
        return retort.extend(
            recipe=[
                name_mapping(name_style=NameStyle.CAMEL),
            ]
        )

Step 5. Bind methods to clients.

class Client(RequestsClient):
    def __init__(self) -> None:
        super().__init__(base_url="https://jsonplaceholder.typicode.com/")

    @override
    def _init_response_factory(self) -> Retort:
        retort = super()._init_response_factory()
        return retort.extend(
            recipe=[
                name_mapping(name_style=NameStyle.CAMEL),
            ]
        )

    get_post = bind_method(GetPost)
    create_post = bind_method(CreatePost)

Step 6. Use client.

client = Client()
created_post = client.create_post(
    user_id=1,
    title="Title",
    body="Body"
)
got_post = client.get_post(created_post.id)

Step 7. Close client.

client.close()

Full code.

@dataclass
class Post:
    id: int
    title: str
    body: str
    user_id: int


class GetPost(Method[Post]):
    __url__ = "posts/{id}"
    __method__ = "get"

    id: UrlVar[int]


class CreatePost(Method[PostId]):
    __url__ = "posts"
    __method__ = "post"

    user_id: Body[int]
    title: Body[str]
    body: Body[str]


class Client(RequestsClient):
    def __init__(self) -> None:
        super().__init__(base_url="https://jsonplaceholder.typicode.com/")

    @override
    def _init_response_factory(self) -> Retort:
        retort = super()._init_response_factory()
        return retort.extend(
            recipe=[
                name_mapping(name_style=NameStyle.CAMEL),
            ]
        )

    get_post = bind_method(GetPost)
    create_post = bind_method(CreatePost)


client = Client()
created_post = client.create_post(
    user_id=1,
    title="Title",
    body="Body"
)
got_post = client.get_post(created_post.id)
client.close()

Asyncio

To use async client insted of sync:

  1. Install aiohttp (instead of requests)
  2. Change RequestsClient to AiohttpClient
  3. Add the await keyword before the method call
class Client(AiohttpClient):
    def __init__(self) -> None:
        super().__init__(base_url="https://jsonplaceholder.typicode.com/")

    @override
    def _init_response_factory(self) -> Retort:
        retort = super()._init_response_factory()
        return retort.extend(
            recipe=[
                name_mapping(name_style=NameStyle.CAMEL),
            ]
        )

    get_post = bind_method(GetPost)
    create_post = bind_method(CreatePost)


client = Client()
created_post = await client.create_post(
    user_id=1,
    title="Title",
    body="Body"
)
got_post = await client.get_post(created_post.id)
client.close()

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

retejo-0.0.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

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

retejo-0.0.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file retejo-0.0.1.tar.gz.

File metadata

  • Download URL: retejo-0.0.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for retejo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5366e0337fd9edb6f07ddd24c8948efc839741526f2536cb477d902a330d422e
MD5 76bc09c54b6c96ec0b3779054116a3ca
BLAKE2b-256 82e9f482af04f7af9fa3e0524bb2e172809b45f584d76b55cd61572ca6fbe220

See more details on using hashes here.

File details

Details for the file retejo-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: retejo-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for retejo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 80537c2c3b1e790e3633fed1bfa04ffdfafe4efde9ccfcae830e543072f32c24
MD5 f5a40b0b5f8934b11d0890d49236050c
BLAKE2b-256 4ad73007026d4b25498b350bba04116e0b88930edb309d78c5276ccec95b04b6

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