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

Uploaded Python 3

File details

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

File metadata

  • Download URL: niquests_cache-0.0.2.tar.gz
  • Upload date:
  • Size: 10.3 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.2.tar.gz
Algorithm Hash digest
SHA256 c36b0ccffb6ed6cbf5be4ac6bf2d777a443a55e5c19635e0d941867a9a4e3a59
MD5 ee9d67394def57f4ecd0602540d6b9e1
BLAKE2b-256 d6f538d21b3eec6ad557adde46efbae3b31aaf879bb4c27867ccec8d9b57644f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: niquests_cache-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 53d96a66e86c20cf67c6bd1e446670a2aa1d182b43bb69eaa963f02ed1c83859
MD5 f2cd5b489a5626bfb3241d662af424fc
BLAKE2b-256 7e32b4a6ac8c670767026a1edeeaf61411e52d6c7fc6ecbf3259c398cb7992cd

See more details on using hashes here.

Provenance

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