Skip to content

Commit

Permalink
Merge pull request #25 from CrocoFactory/dev
Browse files Browse the repository at this point in the history
Updating README.md
  • Loading branch information
blnkoff authored Nov 21, 2024
2 parents 2ffd033 + 74d4b36 commit 4e199dd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 21 deletions.
90 changes: 71 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 4e199dd

Please sign in to comment.