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.0.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.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tha_req_runner-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 d64b6155d0c81aacafeb070fa8bc717bd54df8be98ca71540be7be957cbfc528
MD5 72425df26a1526af1fa90cd714a93fb1
BLAKE2b-256 ed295c69d607f938beef9acf54022d638276d57305c5b4dace0d28fec1264ae5

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: tha_req_runner-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b951ce5d30a8f569a02e38056bbec7887a631d8d6f3e632e9c4c8d1b4636ca0c
MD5 a9250dc60219f3e58e1661bfddf55099
BLAKE2b-256 257b45946725c940daa8143e4317cff45eea7ad14bbd8cd037a7a41838e84f71

See more details on using hashes here.

Provenance

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