https://github.com/mango-philosophy/tealeaf
A simple interface for readable Python HTTP clients
https://github.com/mango-philosophy/tealeaf
api http json pydantic python
Last synced: 27 days ago
JSON representation
A simple interface for readable Python HTTP clients
- Host: GitHub
- URL: https://github.com/mango-philosophy/tealeaf
- Owner: mango-philosophy
- License: mit
- Created: 2023-08-18T21:21:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-05T18:05:08.000Z (over 2 years ago)
- Last Synced: 2025-12-16T19:13:16.791Z (4 months ago)
- Topics: api, http, json, pydantic, python
- Language: Python
- Homepage:
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tealeaf
[](https://github.com/jackmuskopf/softy/blob/main/LICENSE)
A simple interface for readable Python HTTP clients
- No dependencies
- Simple and readable
- Lightweight
## Installation
```
pip install tealeaf
```
## Usage
A simple example
```python
import tealeaf
# simple POST
api = tealeaf.Api('https://www.postman-echo.com')
api.post('/post', {"message": "hello tealeaf"}).json()
# Using bearer tokens
api = tealeaf.Api(
'https://www.postman-echo.com',
credentials=tealeaf.BearerToken('my-jwt')
)
api.post('/post', {"message": "hello authorized tealeaf"}).json()
```
Integrates easily with `pydantic` for data validation
```python
import tealeaf
from pydantic import BaseModel
class Message(BaseModel):
message: str
class EchoResponse(BaseModel):
data: Message
url: str
# simple POST
api = tealeaf.Api('https://www.postman-echo.com')
echo = api \
.post('/post', {"message": "hello tealeaf"}) \
.astype(EchoResponse)
print(repr(echo))
```
```
>>> EchoResponse(data=Message(message='hello tealeaf'), url='https://www.postman-echo.com/post')
```
An example with custom auth algorithm:
```python
# define a custom auth handler with a `preprocess_request` method
class CustomCredentialHandler(tealeaf.ApiCredential):
def __init__(self, secret: str):
super().__init__()
self.__secret = secret
def preprocess_request(self, request: tealeaf.Request):
# your algorithm goes here and modifies the request object
request.headers['secret-key'] = f'{request.data}{self.__secret}'
return request
api = tealeaf.Api(
'https://www.postman-echo.com',
credentials=CustomCredentialHandler('my-super-secret')
)
api.post('/post', {"message": "hello custom tealeaf auth"}).json()
```