A Tabular Helper API library that wraps requests with thread-safe session reuse, automatic retries, and a normalized response dict.
Project description
tha-req-runner
A small Python library that provides a thread-safe requests.Session with automatic retries and a normalized response parser. Intended as the HTTP transport layer for other tha-* runners.
Install
pip install tha-req-runner
Quick start
from tha_req_runner import ThaReq
req = ThaReq()
session = req.get_session()
# safe_call wraps the try/except for you
result = req.safe_call(session.get, "https://api.example.com/students", params={"limit": 100})
# {"status": 200, "data": [...], "message": None, "raw_response": <Response>}
# network errors return the same shape — no try/except needed
result = req.safe_call(session.get, "https://unreachable.example.com")
# {"status": None, "data": None, "message": "Connection refused", "raw_response": None}
Response dict
Every method returns the same shape whether the call succeeded or raised:
| Key | Type | Description |
|---|---|---|
status |
int | None |
HTTP status code, or None on network error |
data |
object |
Parsed JSON body, or None if not JSON |
message |
str | None |
Exception message on error, otherwise None |
raw_response |
Response | None |
The raw requests.Response object |
API
ThaReq
ThaReq()
req.get_session()
req.get_session(
*,
status_forcelist: tuple[int, ...] = (500, 502, 503, 504),
allowed_methods: Collection[str] | None = None,
) -> requests.Session
Returns a requests.Session configured with automatic retries (total=3, backoff_factor=0.5). Config is applied only on the first call per thread — subsequent calls on the same thread return the cached session regardless of args. Two ThaReq instances never share a session.
allowed_methods=None uses urllib3's safe-method default, which excludes POST. To retry POST (e.g. token endpoints):
session = req.get_session(
status_forcelist=(429, 500, 502, 503, 504),
allowed_methods=frozenset(["GET", "POST"]),
)
ThaReq.parse_response()
ThaReq.parse_response(result: requests.Response | Exception) -> dict[str, Any]
Normalizes a requests.Response or a caught Exception into a consistent dict. Also callable as req.parse_response(result) without instantiation.
req.safe_call()
req.safe_call(fn, *args, **kwargs) -> dict[str, Any]
Calls fn(*args, **kwargs), catches any exception, and returns a normalized response dict. Equivalent to:
try:
result = req.parse_response(fn(*args, **kwargs))
except Exception as exc:
result = req.parse_response(exc)
result = req.safe_call(session.get, url, params={"limit": 100})
result = req.safe_call(session.post, token_url, data={"grant_type": "client_credentials"})
Session and retries
- Thread-safe: each thread gets its own session via
threading.localon the instance - Retry defaults:
total=3,backoff_factor=0.5(delays: 0.5s → 1s → 2s) - Retry statuses:
500,502,503,504by default - POST not retried by default — pass
allowed_methodsexplicitly to enable it - Sessions are reused across calls on the same thread
Used by
tha-edfi-runner— usesThaReqas its HTTP transport layer
License
MIT
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 tha_req_runner-0.1.1.tar.gz.
File metadata
- Download URL: tha_req_runner-0.1.1.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
353ef1ed6d1a3a7d26a82247efff97f50f25ca5ebfdb9a2beb69ae720e97bc94
|
|
| MD5 |
74b8b295da68fd7d39fa3dbea2b17c36
|
|
| BLAKE2b-256 |
0d9aaef7c90b6496f1806bd7faff86b589a5998484b34a2f8308c6132451f023
|
Provenance
The following attestation bundles were made for tha_req_runner-0.1.1.tar.gz:
Publisher:
publish.yml on tha-guy-nate/tha-req-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tha_req_runner-0.1.1.tar.gz -
Subject digest:
353ef1ed6d1a3a7d26a82247efff97f50f25ca5ebfdb9a2beb69ae720e97bc94 - Sigstore transparency entry: 1554591934
- Sigstore integration time:
-
Permalink:
tha-guy-nate/tha-req-runner@104b089559cb0d8ee82313b17196cd5cb35e2f1b -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/tha-guy-nate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@104b089559cb0d8ee82313b17196cd5cb35e2f1b -
Trigger Event:
push
-
Statement type:
File details
Details for the file tha_req_runner-0.1.1-py3-none-any.whl.
File metadata
- Download URL: tha_req_runner-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
903c86129720aee8d038d2b643d621b14f0386e31d0fb4626948de112ed912cb
|
|
| MD5 |
909832babbd859294cc09afc065fef4a
|
|
| BLAKE2b-256 |
351f2435dfe01fc0c575d81f9d414b8c28b1225c49e7032853eae1633df8ee5f
|
Provenance
The following attestation bundles were made for tha_req_runner-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on tha-guy-nate/tha-req-runner
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tha_req_runner-0.1.1-py3-none-any.whl -
Subject digest:
903c86129720aee8d038d2b643d621b14f0386e31d0fb4626948de112ed912cb - Sigstore transparency entry: 1554591946
- Sigstore integration time:
-
Permalink:
tha-guy-nate/tha-req-runner@104b089559cb0d8ee82313b17196cd5cb35e2f1b -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/tha-guy-nate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@104b089559cb0d8ee82313b17196cd5cb35e2f1b -
Trigger Event:
push
-
Statement type: