requests.Session to work with JSON APIs
Project description
api-session
api-session is a small module providing an extended requests.Session class to work with JSON APIs.
It aims at factoring the common parts of these clients while staying very lightweight (<100 SLOC). It only augments
the requests.Session class, so the base methods are still available.
We use it at Bixoto as a basis for JSON API clients such as PyMagento.
Features
- 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
POSTand similar calls - Offline flag: if given, prevents the API from doing any call. This is useful for tests.
requests.Sessioninheritance: 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.
Install
pip install api-session
With uv:
uv add api-session
With Poetry:
poetry add api-session
Dependency: Python 3.10+.
- Versions 1.6.x and above require Python 3.10+
- Versions 1.5.x require Python 3.9+
- Versions 1.4.x and below require Python 3.8+
Usage
from api_session import APISession
# The only requirement is to pass the base URL of the API you want to use.
# This does not prevent you from calling other URLs.
#
# Remember this is just an augmented `requests.Session()` object.
client = APISession("https://httpbin.org")
# All requests methods are available:
client.get("https://example.com")
client.head("https://example.com")
client.post("https://example.com")
client.put("https://example.com")
client.delete("https://example.com")
client.options("https://example.com")
# In addition, *_api methods call the base URL:
client.get_api("/foo") # GET https://httpbin.org/foo
client.head_api("/foo") # HEAD https://httpbin.org/foo
client.post_api("/foo") # POST https://httpbin.org/foo
client.put_api("/foo") # PUT https://httpbin.org/foo
client.delete_api("/foo") # DELETE https://httpbin.org/foo
client.options_api("/foo") # OPTIONS https://httpbin.org/foo
# For JSON API, use the _*_json_api methods. They call `.json()` on the response:
client.get_json_api("/foo") # equivalent of client.get("https://httpbin.org/foo").json()
client.post_json_api("/bar") # equivalent of client.post("https://httpbin.org/bar").json()
client.put_json_api("/baz") # equivalent of client.put("https://httpbin.org/baz").json()
client.delete_json_api("/qux") # equivalent of client.delete("https://httpbin.org/qux").json()
A typical usage is to inherit from the session to implement an API client class:
from typing import Any
from api_session import APISession
class FooApiClient(APISession):
def __init__(self, token: str, **kwargs):
# Set the base URL used by all API calls
super().__init__(base_url="https://foo-api.example.com/v1", **kwargs)
# Add your own setup, like headers
self.headers["Authorization"] = f"Bearer {token}"
def get_stuff(self) -> list[dict]:
return self.get_json_api("/get-stuff")
def create_stuff(self, stuff: dict[str, Any]) -> bool:
return self.post_json_api("/create-stuff", json=stuff)
# Then use it:
my_client = FooApiClient("my-token")
my_client.create_stuff({"foo": "bar"}) # => True
for thing in my_client.get_stuff():
print(thing)
FAQ
What about cookies?
Cookies are persisted in the session object, just like with requests.Session.
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 api_session-1.7.0.tar.gz.
File metadata
- Download URL: api_session-1.7.0.tar.gz
- Upload date:
- Size: 52.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
473f09393c9772bf4a5b0bdad9ac1e40d85d22a9801679114e4c26a4c60c83ad
|
|
| MD5 |
f8aaeb4f692677f0145958081f36e153
|
|
| BLAKE2b-256 |
21495f82c3b11c3a005f77afb02b3bbb9ccfb4deb587f4977edd83e181870427
|
File details
Details for the file api_session-1.7.0-py3-none-any.whl.
File metadata
- Download URL: api_session-1.7.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3458de51e37fb5324d396554d2f9eb0b6de7323da6ba89f5d6705b4b45e322ba
|
|
| MD5 |
485f52c9fa0f3d16ac577b88a1a28ece
|
|
| BLAKE2b-256 |
1115833c1092323ed9a1bf6616218fcf62223701cdd9b1421ee9de682528216d
|