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.2.tar.gz (5.2 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.2-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tha_req_runner-0.1.2.tar.gz
  • Upload date:
  • Size: 5.2 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.2.tar.gz
Algorithm Hash digest
SHA256 434aaec9c266c15878a27b445cc4b6d0a2ea3af7f361f5356bb5043780712408
MD5 b613b6acc54e18f9fe9fe65ab3b8597c
BLAKE2b-256 c86b227e192f85e7e423f35078fa8736f8e7aac04144ccce39f3cf78124795d9

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tha_req_runner-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.5 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1bad072edac244291468863f62fc1651f435b1f6f8a7671ab0d05a1d94dadaf3
MD5 23ef236097d6a6b266c46cdd41f0bd7b
BLAKE2b-256 de9ab06b43e9b7a3a2a4172476e898408ce4da08c7c5b0b84f169c7137d2dc28

See more details on using hashes here.

Provenance

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