Skip to main content

Filesystem-cached niquests sessions.

Project description

niquests-cache

Python versions PyPI - Version GitHub tag (with filter) License GitHub commits since latest release (by SemVer including pre-releases) CodeQL QA Tests Coverage Status Dependabot Documentation Status mypy uv pytest Ruff Downloads Stargazers pre-commit Prettier

@Tatsh Buy Me A Coffee Libera.Chat Mastodon Follow Patreon

Filesystem-cached niquests sessions.

Installation

pip install niquests-cache

Example usage

The cached_session() helper returns a CachedSession or CachedAsyncSession whose cache root is platformdirs.user_cache_path(app_name, appauthor=False) / 'http'. If you omit app_name, niquests-cache is used. Only successful GET and HEAD responses are written to disk; the default time-to-live is 10 minutes (expire_after= on the helper, or per-request—see below).

Sync helper (default app name and TTL):

from niquests_cache import cached_session

session = cached_session()
response = session.get('https://httpbin.org/get')
response.raise_for_status()

Custom application name for user_cache_path (same http subdirectory):

from niquests_cache import cached_session

session = cached_session(app_name='my-application')
response = session.get('https://httpbin.org/get')
response.raise_for_status()

Plain niquests session with no filesystem cache:

from niquests_cache import cached_session

session = cached_session(no_cache=True)

Construct CachedSession when you need an explicit directory or TTL:

from datetime import timedelta
from pathlib import Path

from niquests_cache import CachedSession

cache = Path('.cache') / 'http'
with CachedSession(cache_dir=cache, expire_after=timedelta(hours=1)) as session:
    response = session.get('https://httpbin.org/get')
    response.raise_for_status()

Async helper (use an async context manager):

import asyncio
from datetime import timedelta

from niquests_cache import cached_session

async def main() -> None:
    session = cached_session(aio=True, expire_after=timedelta(minutes=30))
    async with session:
        response = await session.get('https://httpbin.org/get')
        response.raise_for_status()

asyncio.run(main())

Or construct CachedAsyncSession directly:

import asyncio
from datetime import timedelta
from pathlib import Path

from niquests_cache import CachedAsyncSession

async def main() -> None:
    cache = Path('.cache') / 'http'
    async with CachedAsyncSession(cache_dir=cache, expire_after=timedelta(hours=1)) as session:
        response = await session.get('https://httpbin.org/get')
        response.raise_for_status()

asyncio.run(main())

To bypass the cache for a single request, pass expire_after=0 to request (that GET or HEAD is not served from or written to the cache).

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

niquests_cache-0.0.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

niquests_cache-0.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file niquests_cache-0.0.1.tar.gz.

File metadata

  • Download URL: niquests_cache-0.0.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for niquests_cache-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ff88e02fec36df9d9dd1157f1a7873e493cee6064308fb02685bd74ff775664e
MD5 441b20a902389fee4c96909f49de6269
BLAKE2b-256 0b32815efa319ccadd5f7473f57d87b0f90157e5138656581653b0b28bc98b38

See more details on using hashes here.

Provenance

The following attestation bundles were made for niquests_cache-0.0.1.tar.gz:

Publisher: publish.yml on Tatsh/niquests-cache

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

File details

Details for the file niquests_cache-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: niquests_cache-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for niquests_cache-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c75281eabf70940a480c434c2d3d2c13bf8fa43a8ba7565813b9ee838b669ab9
MD5 6738327be411e83b496e9e4e9640f060
BLAKE2b-256 af643324241b0caf7db99a1b01c43ebb575f9a4fd252f87a3cb35f61600a4396

See more details on using hashes here.

Provenance

The following attestation bundles were made for niquests_cache-0.0.1-py3-none-any.whl:

Publisher: publish.yml on Tatsh/niquests-cache

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