daffalib
A modern and ultra-simple Python wrapper for the requests library, designed to simplify REST API interactions with flexible configuration and built-in REPL support.
Features
- Simple Interface: Clean and intuitive methods for
GET,POST,PUT, andDELETE. - Automatic JSON Parsing: Responses are automatically converted to Python dictionaries.
- Graceful Fallback: If a response is not valid JSON, the raw text content is returned.
- Automatic Error Handling: Raises
requests.HTTPErrorfor unsuccessful responses (4xx or 5xx status codes). - Flexible Configuration: Easily configure base URLs, default headers, and authentication for your API session.
- Interactive REPL: A command-line tool for interacting with APIs directly from your terminal.
Installation
Install daffalib using pip:
pip install daffalib
Basic Usage
Instantiate the API class with a base URL and start making requests. The methods return a dictionary or a string directly.
from daffalib import API
from requests.exceptions import HTTPError
# Use a public test API
api = API("https://jsonplaceholder.typicode.com")
try:
# GET request
posts = api.get("posts")
print(f"Found {len(posts)} posts.")
# GET a single item
post = api.get("posts/1")
print(f"Title of post #1: {post['title']}")
# POST request
new_post_data = {
"title": "My New Post",
"body": "This is the content.",
"userId": 1
}
created_post = api.post(new_post_data, endpoint="posts")
print(f"Created new post with ID: {created_post['id']}")
# PUT request
updated_data = {"title": "Updated Title"}
updated_post = api.put("posts/1", data=updated_data)
print(f"Updated post #1's title to: {updated_post['title']}")
# DELETE request
response = api.delete("posts/1")
print("Delete response:", response) # Often empty on success
except HTTPError as e:
print(f"An HTTP error occurred: {e.response.status_code} {e.response.reason}")
except Exception as e:
print(f"An error occurred: {e}")
Custom Headers and Authentication
You can configure default headers and authentication when initializing the API object.
from daffalib import API
from requests.auth import HTTPBasicAuth
# Custom headers
headers = {
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"X-Custom-Header": "MyValue"
}
# Basic Authentication
auth = HTTPBasicAuth('your_username', 'your_password')
# Initialize with headers and auth
secure_api = API(
base_url="https://api.yourapi.com/v1/",
headers=headers,
auth=auth
)
# All requests made with `secure_api` will now include the configured headers and auth
data = secure_api.get("user/profile")
print(data)
Interactive REPL Mode
daffalib includes a command-line tool for quick API exploration. Just run daffalib-cli in your terminal.
-
Start the tool:
daffalib-cli
-
Enter the base URL when prompted:
Welcome to the daffalib Interactive Console! ... Enter Base URL: https://jsonplaceholder.typicode.com API client initialized for https://jsonplaceholder.typicode.com daffalib> -
Use direct commands to interact with the API:
daffalib> get /users/1 {'address': {'city': 'Gwenborough', 'geo': {'lat': '-37.3159', 'lng': '81.1496'}, 'street': 'Kulas Light', 'suite': 'Apt. 556', 'zipcode': '92998-3874'}, 'company': {'bs': 'harness real-time e-markets', 'catchPhrase': 'Multi-layered client-server neural-net', 'name': 'Romaguera-Crona'}, 'email': 'Sincere@april.biz', 'id': 1, 'name': 'Leanne Graham', 'phone': '1-770-736-8031 x56442', 'username': 'Bret', 'website': 'hildegard.org'} daffalib> post /todos '{"userId": 1, "title": "Learn daffalib", "completed": true}' {'userId': 1, 'title': 'Learn daffalib', 'completed': True, 'id': 201} daffalib> exit
License
This project is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for daffalib 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| daffalib-0.2.5.tar.gz | 6.1 kB | Without provenance | Details for daffalib-0.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| daffalib-0.2.5-py3-none-any.whl | Python 3 | none | any | Details for daffalib-0.2.5-py3-none-any.whl |
Total release size: 13.2 kB
Release files / daffalib-0.2.5.tar.gz
| Download URL | daffalib-0.2.5.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd639db7a7a0a5e1f9ad4dc836312cef4bc194ceb0a9602c670a46efda2ebfaa
|
|
BLAKE2b-256 checksum How to use checksums |
8454c03aab9a1f02283068a46416ed990f21e86bec99764b2d820d9e90809b43
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Release files / daffalib-0.2.5-py3-none-any.whl
| Download URL | daffalib-0.2.5-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c24d6458943efaf2f280e7a184babd6c8e40874306a67759e61b00e371d68b86
|
|
BLAKE2b-256 checksum How to use checksums |
ca88aebee09cf90284a1749b42223ecc49020632bbf75aa97238fcbc17f7a1a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.12
|
Security for daffalib 0.2.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.