Building blocks for easy REST API consumption
Project description
eazyrest
eazyrest is a small library for consuming REST APIs with less boilerplate. It combines a session-based API client with a lightweight, type-directed JSON object mapping layer that models REST resources as typed Python classes.
Installation
pip install eazyrest
Requires Python 3.10+.
Example
The example uses the public REST demo API available at https://jsonplaceholder.typicode.com/.
from eazyrest import API, JSONObject, json_object
demo_api = API("https://jsonplaceholder.typicode.com/")
class JSONPlaceholderObject(JSONObject):
"""A JSON object from the JSONPlaceholder API"""
pass
# Set class variable to point to JSONPlaceholder API instance
JSONPlaceholderObject.api = demo_api
@json_object
class User(JSONPlaceholderObject):
"""Represents one user object from JSONPlaceholder API."""
class_url = "/users/"
id: int
name: str
username: str
email: str
address: dict
phone: str
website: str
company: dict
def __str__(self):
return self.name
@json_object(field_map={'user': 'userId'})
class Todo(JSONPlaceholderObject):
"""Represents one Todo object from JSONPlaceholder API."""
class_url = "/todos/"
id: int
user: User
title: str
completed: bool
# Load one object by primary key (GET /todos/1/)
todo = Todo(id=1)
print(todo.title, todo.completed)
# Demonstrate loading of related object
print(todo.user)
# Query a collection (GET /todos/?userId=1)
todos_for_user_1 = Todo.filter(userId=1)
print(len(todos_for_user_1))
# Access typed fields
first = todos_for_user_1[0]
print(first.id, first.user.id, first.user, first.title)
License
MIT. See LICENSE.
Development
python3 -m venv .venv
. .venv/bin/activate
python -m pip install -U build twine
python -m build
python -m twine check dist/*
Publishing
To publish to PyPI:
python -m twine upload dist/*
To publish to Test PyPI:
python -m twine upload --repository testpypi dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file eazyrest-0.9.0.tar.gz.
File metadata
- Download URL: eazyrest-0.9.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
999b6a7eb5473cb5b110d53d6bb383441c1f1ea17bdf22c4b136c5b1ccb672d7
|
|
| MD5 |
38513f85ae00e52b35f3bdf2a9fc175b
|
|
| BLAKE2b-256 |
742baed869c38716495977c7723eeed83dee0b398a9ee3f795d0e13853660a1a
|
File details
Details for the file eazyrest-0.9.0-py3-none-any.whl.
File metadata
- Download URL: eazyrest-0.9.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d428c9b3d279ba4c2c13ccadd5e3746d277cb0063bd8c95a9164fa635521cd0
|
|
| MD5 |
cd772834731571e2ef8acaadccfabf3a
|
|
| BLAKE2b-256 |
2903cf4d88b25fa03bbd46b8d9b11d6987594ebffde6e6dd162066bf3dcfab0b
|