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.2.0.tar.gz (5.7 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.2.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file tha_req_runner-0.2.0.tar.gz.

File metadata

  • Download URL: tha_req_runner-0.2.0.tar.gz
  • Upload date:
  • Size: 5.7 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.2.0.tar.gz
Algorithm Hash digest
SHA256 6a2c90d090fa0b51921da6d8c2363b5c816ad85d3f1ef256dad1585291ef1e7d
MD5 125fcebfb0f535e4e30608493e184c2b
BLAKE2b-256 d6e39d8395e9634dbb468265251ae00b3a22696d7c6f4d09a9a4396160874af5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_req_runner-0.2.0.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: tha_req_runner-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bff1dedd41834f086c00c63bfe28fad0ab6468326fa7c5eea381299c32c093f
MD5 2e365fb9d44b128bde0938bc36d12b63
BLAKE2b-256 4c7a7768a7c351309887876ba6dbadd3503091fda7dfd2ffeb5000ff74ec871a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tha_req_runner-0.2.0-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