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.3.tar.gz (10.4 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.3-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: niquests_cache-0.0.3.tar.gz
  • Upload date:
  • Size: 10.4 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.3.tar.gz
Algorithm Hash digest
SHA256 292f0093e31d32cf37ed8127bf2a6892fbbcc6273458b51d25291592d7741a84
MD5 86c5035d7fc46ae7f584d6b23e5c369c
BLAKE2b-256 828328a81393abb1e41d5c3771b8c281c5abb6a7ee4eb3c0986d1c85ea0fb688

See more details on using hashes here.

Provenance

The following attestation bundles were made for niquests_cache-0.0.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: niquests_cache-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c4f62a320cd993c002951f0268d40f98b960759cf1d66f15452bae4baf1a47b3
MD5 e811e084bfe76bd34271bf6fb6d1995d
BLAKE2b-256 2ac99e08677b85e709f1a6ecf1c51687ea42c9a10bb816fe13d7c4425490536e

See more details on using hashes here.

Provenance

The following attestation bundles were made for niquests_cache-0.0.3-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