sensei
Build robust HTTP Requests and best API clients with minimal implementation
The Python framework that provides a quick way to build robust HTTP requests and best API clients. Use type hints, to build requests, with little or no implementation.
Documentation: https://sensei.crocofactory.dev
Source code: https://github.com/CrocoFactory/sensei
There are key features provided by sensei:
- Fast: Do not write any request-handling code, dedicate responsibility to the function's interface(signature)
- Short: Avoid code duplication.
- Sync/Async: Implement sync and async quickly, without headaches
- Robust: Auto validation data before and after request
Quick Overview
API Client (or API Wrapper) should provide these features for users:
- Provide sync and async code versions
- Validate data before accessing the API.
- Handle RPS (Requests per second) limits.
- Return relevant response
And as a developer, you want to avoid code duplication and make routine things faster. To follow all these principles, you either violate DRY or have to maintain bad code architecture.
Sensei is a tool to avoid these issues.
First Request
Do you want to see the simplest and most robust HTTP Request? He's already here!
from typing import Annotated
from sensei import Router, Path, APIModel
router = Router('https://pokeapi.co/api/v2/')
class Pokemon(APIModel):
name: str
id: int
height: int
weight: int
@router.get('/pokemon/{name}')
def get_pokemon(name: Annotated[str, Path(max_length=300)]) -> Pokemon:
pass
pokemon = get_pokemon(name="pikachu")
print(pokemon) # Pokemon(name='pikachu' id=25 height=4 weight=60)
Didn't it seem to you that the function doesn't contain the code? Sensei writes it instead of you!
Moreover, Sensei abstracts away much of the manual work, letting developers focus on function signatures while the framework handles the API logic and data validation. This enables a declarative style for your apps.
The example of First Request demonstrates a simple and robust HTTP request using the Sensei framework. Here's the key breakdown of the process:
1. Importing Dependencies:
Routermanages API endpoints and routing.Pathspecifies and validates route parameters.APIModeldefines models for structuring API responses (similar topydantic.BaseModel).
2. Creating the Router:
The Router is initialized with the base URL of the PokéAPI. All subsequent requests will use this as the base path.
3. Defining the Model:
The Pokemon class represents the data structure for a Pokémon, with fields like name, id, height, and weight.
It inherits from APIModel, which provides validation and serialization.
4. Creating the Endpoint:
The get_pokemon function is a routed function decorated with @router.get, defining a GET request for
/pokemon/{name}.
This uses Annotated to ensure that name is a string and adheres to the validation rule (max length of 300).
5. Making the Request:
By calling get_pokemon(name="pikachu"), Sensei automatically handles validation, makes the HTTP request,
and maps the API response into the Pokemon model. The code omits the function body since Sensei handles calls through
the function's signature.
Installing sensei
To install sensei from PyPi, you can use that:
pip install sensei
To install sensei from GitHub, use that:
pip install git+https://github.com/CrocoFactory/sensei.git
Release files for sensei 0.1.1.post2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sensei-0.1.1.post2.tar.gz | 75.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sensei-0.1.1.post2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 114.6 kB
Release files / sensei-0.1.1.post2.tar.gz
| Download URL | sensei-0.1.1.post2.tar.gz |
|---|---|
| Size | 75.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e33be8d92e2f23a71e29d5f0a849176be69727d3b73f9d7b2b04d1c9610f8a0a
|
|
BLAKE2b-256 checksum How to use checksums |
cb3b86b068c47af0f22232204fc8491cd1adb2786ef6fd8b47064914da62ae47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.6 Darwin/24.0.0
|
Release files / sensei-0.1.1.post2-py3-none-any.whl
| Download URL | sensei-0.1.1.post2-py3-none-any.whl |
|---|---|
| Size | 39.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e28efec4a82360bef518aba33e8e26584755ea03e5807e305c9f8b1fe9d569ac
|
|
BLAKE2b-256 checksum How to use checksums |
5132e7e49ae9a42341a2b24ef9fb921498dd9fc25789b2a5ff6059258869ef29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.12.6 Darwin/24.0.0
|