-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from CrocoFactory/dev
Updating README.md
- Loading branch information
Showing
3 changed files
with
76 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,28 +21,25 @@ little or no implementation. | |
**Source code:** [https://github.com/CrocoFactory/sensei](https://github.com/CrocoFactory/sensei) | ||
|
||
--- | ||
|
||
<a href="https://pypi.org/project/sensei/"> | ||
<h1 align="center"> | ||
<img alt="Mindmap" src="https://raw.githubusercontent.com/CrocoFactory/sensei/main/assets/mindmap.svg" height="350px"> | ||
</h1><br> | ||
</a> | ||
|
||
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. | ||
- **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 🛡️️ | ||
|
||
**Sensei is a tool to avoid these issues.** | ||
Table of Contents: | ||
1. [First Request](#first-request) | ||
2. [Comparison](#comparison) | ||
3. [OOP Style](#oop-style) | ||
4. [Installing](#installing) | ||
|
||
## First Request | ||
|
||
|
@@ -106,7 +103,62 @@ By calling `get_pokemon(name="pikachu")`, Sensei automatically handles validatio | |
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 | ||
## Comparison | ||
|
||
**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. | ||
|
||
## OOP Style | ||
|
||
There is a wonderful OOP approach proposed by Sensei: | ||
|
||
```python | ||
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="[email protected]", 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` | ||
|
||
## Installing | ||
To install `sensei` from PyPi, you can use that: | ||
|
||
```shell | ||
|
Oops, something went wrong.