Skip to main content

Cache-aware requests session with r/w/rw disk modes and condition validation

Project description

SmartRequests

A lightweight wrapper around requests.Session that adds transparent disk caching with simple r / w / rw mode control and response condition validation.


Install

pip install smart_requests_dvxb

Standard vs Smart

Standard requests — fetches every time:

import requests

session = requests.Session()
resp = session.get("https://example.com/data.json")
print(resp.text)  # hits network every single run

SmartRequests — fetches once, reads from disk after:

import requests
from SmartRequests import smart_session

session = smart_session(requests.Session())
resp = session.get(
    "https://example.com/data.json",
    mode="rw",
    filepath="/cache/data.json",
)
print(resp.text)  # network on first run, disk on every run after

That's the only change. Everything else (headers, cookies, params, data, json) works exactly as before.


Modes

Mode Behavior
r Read from cache only — raises error if file missing
w Always fetch from network, write result to disk
rw Return cache if exists, otherwise fetch and store
rb / wb / rwb Same as above but read/write raw bytes (images, PDFs)

Condition Validation

Conditions are lambdas evaluated against the response before writing to disk. If any condition fails, the file is never written and a ValueError is raised — so you never cache a bad response.

Without SmartRequests — bad response silently saved:

resp = session.post(url, data=payload)
with open("result.html", "w") as f:
    f.write(resp.text)  # saves error page, captcha page, anything

With SmartRequests — only valid responses are cached:

resp = session.post(
    url,
    data=payload,
    mode="w",
    filepath="/cache/result.html",
    conditions=[
        lambda r: r.status_code == 200,
        lambda r: len(r.content) > 500,
        lambda r: "error" not in r.text,
        lambda r: "captcha" not in r.text.lower(),
    ],
)

Full Parameter Reference

Parameter Type Description
mode str One of r, w, rw, rb, wb, rwb
filepath str Absolute path to cache file — required when mode is set
conditions list[callable] Lambdas evaluated against response before writing — raises ValueError on first failure

Contact Developer

Dharmik Vadherdharmik.vadher@xbyte.io

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

smart_requests_dvxb-0.17-cp312-cp312-win_amd64.whl (88.6 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file smart_requests_dvxb-0.17-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for smart_requests_dvxb-0.17-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bcb5233626d8336f68ce714e9ff25efdf0685c6aa25d81e24d4754d1197d3b0e
MD5 f7b83f863f0eb7a69a455fd2c3e8508e
BLAKE2b-256 99aec0fd42cef9fc21e372b42733da28f27aac9a8fda9bbeab905240e66a1145

See more details on using hashes here.

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