api-session is a small module providing an extended requests.Session
class to work with JSON APIs.
We use it at Bixoto as a basis for JSON API clients such as PyMagento or PyBigBuy.
It aims at factoring the common parts of these clients while staying very lightweight (<100 SLOC).
- Base URL: the base API URL is given only once on object creation; subsequent calls use
.get("/path")
- Read-only flag: if given, prevents the API from doing
POST
and similar calls - Offline flag: if given, prevents the API from doing any call. This is useful for tests.
requests.Session
inheritance: the class inherits fromrequests.Session
, so it stays 100% compatible with it- Response bodies are included in exception messages for 4xx errors. This behavior can be customized.
pip install api-session
Or with Poetry:
poetry add api-session
Dependency: Python 3.9+.
- Versions 1.5.x require Python 3.9+
- Versions 1.4.x and before require Python 3.8+
from api_session import APISession
client = APISession("https://httpbin.org")
client.get_json_api("/get")
# => {...}