https://github.com/songzhi/apix
Declarative HTTP client based on pydantic and typical.
https://github.com/songzhi/apix
httpx typical
Last synced: about 1 month ago
JSON representation
Declarative HTTP client based on pydantic and typical.
- Host: GitHub
- URL: https://github.com/songzhi/apix
- Owner: songzhi
- Created: 2020-04-17T14:23:41.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-23T12:18:38.000Z (almost 2 years ago)
- Last Synced: 2025-04-06T11:46:26.732Z (2 months ago)
- Topics: httpx, typical
- Language: Python
- Homepage:
- Size: 53.7 KB
- Stars: 14
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## [WIP] Typed HTTP Client for Humans
It's inspired by [FastAPI](https://fastapi.tiangolo.com/) and
based on [pydantic](https://pydantic-docs.helpmanual.io/) and
[httpx](https://www.python-httpx.org/).**It's currently a very rough demo for experiment.**
Pydantic will be replaced by [typical](https://github.com/seandstewart/typical).
this project is currently waiting for new type Union operator in Python3.10.
## Example
```python
from typing import Listfrom pydantic import BaseModel
from apix import get, Service, Query
class User(BaseModel):
id: str
email: str
first_name: str
last_name: str
avatar: strclass UsersResponse(BaseModel):
page: int
per_page: int
total: int
total_pages: int
data: List['User']class Paging(BaseModel):
page: intclass UserService(Service):
# `@staticmethod` or `@classmethod` for specified-level client
@get('/api/users')
def get_users(self, paging: Paging = Query(...)) -> UsersResponse:
...
# or@get('https://reqres.in/api/users')
def get_users(self, paging: Paging = Query(...)) -> UsersResponse:
...def main():
paging = Paging(page=1)
print(UserService(base_url='https://reqres.in').get_users(paging))
print(UserService.get_users(paging))
print(UserService.get_users(UserService(base_url='https://reqres.in'),paging))
print(get_users(paging))if __name__ == '__main__':
main()
```## Features
### Multi-level 'client'
if using single decorator like `get`, all endpoints sharing one client.
if defined in a class, there will be a class-level client and instance-level
client.