Skip to main content

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

CI

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.local on the instance
  • Retry defaults: total=3, backoff_factor=0.5 (delays: 0.5s → 1s → 2s)
  • Retry statuses: 500, 502, 503, 504 by default
  • POST not retried by default — pass allowed_methods explicitly to enable it
  • Sessions are reused across calls on the same thread

Used by

  • tha-edfi-runner — uses ThaReq as its HTTP transport layer

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tha_req_runner-0.1.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tha_req_runner-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

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

Hashes for tha_req_runner-0.1.1.tar.gz
Algorithm Hash digest
SHA256 353ef1ed6d1a3a7d26a82247efff97f50f25ca5ebfdb9a2beb69ae720e97bc94
MD5 74b8b295da68fd7d39fa3dbea2b17c36
BLAKE2b-256 0d9aaef7c90b6496f1806bd7faff86b589a5998484b34a2f8308c6132451f023

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for tha_req_runner-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 903c86129720aee8d038d2b643d621b14f0386e31d0fb4626948de112ed912cb
MD5 909832babbd859294cc09afc065fef4a
BLAKE2b-256 351f2435dfe01fc0c575d81f9d414b8c28b1225c49e7032853eae1633df8ee5f

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page