Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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.
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
:
Table of Contents:
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:
Router
manages API endpoints and routing.Path
specifies and validates route parameters.APIModel
defines models for structuring API responses (similar to pydantic.BaseModel
).The Router
is initialized with the base URL of the PokéAPI. All subsequent requests will use this as the base path.
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.
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).
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.
Sensei 👍: It provides a high level of abstraction. Sensei simplifies creating API wrappers, offering decorators for easy routing, data validation, and automatic mapping of API responses to models. This reduces boilerplate and improves code readability and maintainability.
Bare HTTP Client 👎: A bare HTTP client like requests
or httpx
requires manually managing requests,
handling response parsing, data validation, and error handling. You have to write repetitive code for each endpoint.
There is a wonderful OOP approach proposed by Sensei:
class User(APIModel):
email: EmailStr
id: PositiveInt
first_name: str
last_name: str
avatar: AnyHttpUrl
@classmethod
@router.get('/users')
def query(
cls,
page: Annotated[int, Query()] = 1,
per_page: Annotated[int, Query(le=7)] = 3
) -> list[Self]:
pass
@classmethod
@router.get('/users/{id_}')
def get(cls, id_: Annotated[int, Path(alias='id')]) -> Self:
pass
@router.post('/token')
def login(self) -> str:
pass
@login.prepare
def _login_in(self, args: Args) -> Args:
args.json_['email'] = self.email
return args
@login.finalize
def _login_out(self, response: Response) -> str:
return response.json()['token']
user = User.get(1)
user.login() # User(id=1, email="john@example.com", first_name="John", ...)
When Sensei doesn't know how to handle a request, you can do it yourself, using preprocessing as prepare
and
postprocessing as finalize
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
FAQs
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.
We found that sensei demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.