Modern python http client
Project description
Retejo
A modern and simple way to create clients for REST like APIs
Advantages
- Fully typable library.
- Supports async and sync.
- 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:
- Install aiohttp (instead of requests)
- Change RequestsClient to AiohttpClient
- 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
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
retejo-0.0.1-py3-none-any.whl
(16.6 kB
view details)
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5366e0337fd9edb6f07ddd24c8948efc839741526f2536cb477d902a330d422e
|
|
| MD5 |
76bc09c54b6c96ec0b3779054116a3ca
|
|
| BLAKE2b-256 |
82e9f482af04f7af9fa3e0524bb2e172809b45f584d76b55cd61572ca6fbe220
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80537c2c3b1e790e3633fed1bfa04ffdfafe4efde9ccfcae830e543072f32c24
|
|
| MD5 |
f5a40b0b5f8934b11d0890d49236050c
|
|
| BLAKE2b-256 |
4ad73007026d4b25498b350bba04116e0b88930edb309d78c5276ccec95b04b6
|