Skip to content

controls

controls

Per-call request controls (cache and rate-limit overrides).

These use :mod:contextvars, so they work identically for sync code and asyncio tasks, and never leak across concurrent tasks.

Example::

from cocapi import cache_control, without_rate_limit

with cache_control(lookup=False):          # force a fresh fetch
    clan = api.clan_tag("#ABC")

with cache_control(update=False):          # don't store this response
    clan = api.clan_tag("#ABC")

with without_rate_limit():                 # skip the async rate limiter
    data = await api.clan_tag("#ABC")

cache_control

cache_control(
    lookup: bool = True, update: bool = True
) -> Iterator[None]

Override response caching for requests made inside the with block.

PARAMETER DESCRIPTION
lookup

If False, ignore any cached entry and hit the API.

TYPE: bool DEFAULT: True

update

If False, don't store the fresh response in the cache.

TYPE: bool DEFAULT: True

Source code in cocapi/controls.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@contextmanager
def cache_control(lookup: bool = True, update: bool = True) -> Iterator[None]:
    """Override response caching for requests made inside the ``with`` block.

    Args:
        lookup: If False, ignore any cached entry and hit the API.
        update: If False, don't store the fresh response in the cache.
    """
    t1 = _cache_lookup.set(lookup)
    t2 = _cache_update.set(update)
    try:
        yield
    finally:
        _cache_lookup.reset(t1)
        _cache_update.reset(t2)

without_rate_limit

without_rate_limit() -> Iterator[None]

Bypass the async rate limiter for requests made inside the block.

Source code in cocapi/controls.py
47
48
49
50
51
52
53
54
@contextmanager
def without_rate_limit() -> Iterator[None]:
    """Bypass the async rate limiter for requests made inside the block."""
    token = _skip_rate_limit.set(True)
    try:
        yield
    finally:
        _skip_rate_limit.reset(token)