Skip to content

Commit c8e1e9d

Browse files
committed
Modernize the caching docs
1 parent 56cbc21 commit c8e1e9d

5 files changed

Lines changed: 48 additions & 34 deletions

File tree

docs/advanced.rst

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,37 @@ Advanced Section
55
Caching
66
**********
77

8-
By default, caching is **disabled**. You can enable the built-in in-memory cache by passing :code:`use_cache=True`:
8+
The packaged :py:class:`Client` and :py:class:`AsyncClient` do not come with any built-in caching.
9+
A convenient option is to use the :py:class:`niquests_cache.session.CachedSession` or :py:class:`niquests_cache.session.AsyncCachedSession` classes from the `niquests_cache` library.
10+
911

1012
.. code-block:: python
1113
1214
from homeassistant_api import Client
1315
14-
client = Client("<API_URL>", "<TOKEN>", use_cache=True)
16+
from niquests_cache.session import CachedSession
17+
from niquests_cache.backend import MemoryBackend
18+
19+
client = Client("<API_URL>", "<TOKEN>", session=CachedSession(backend=MemoryBackend(), expire_after=300))
20+
21+
.. code-block:: python
22+
from homeassistant_api import AsyncClient
23+
24+
from niquests_cache.session import AsyncCachedSession
25+
from niquests_cache.backend import MemoryBackend
26+
27+
client = AsyncClient("<API_URL>", "<TOKEN>", session=AsyncCachedSession(backend=MemoryBackend(), expire_after=300))
28+
29+
30+
This creates an in-memory cache that expires after 300 seconds. You can adjust the `expire_after` value to fit your needs or set it to `-1` to disable expiration.
31+
For more information on the available caching options, see the `niquests_cache <https://niquests-cache.readthedocs.io/en/stable/>` documentation.
1532

16-
This creates an in-memory cache that expires after 300 seconds.
1733

1834
Persistent Caching
1935
********************
2036

2137
If you want your cache to persist between runs (e.g. to a filesystem), you can pass your own custom cached session via the :code:`session` parameter.
2238

23-
Depending on whether you are using a sync or async client you will want to use either :py:class:`niquests_cache.session.CachedSession` or :py:class:`niquests_cache.session.AsyncCachedSession` respectively.
24-
See the docs for `niquests_cache <https://niquests-cache.readthedocs.io/en/stable/#example-usage>`__ for backend options and more.
25-
2639
.. code-block:: python
2740
2841
from datetime import timedelta
@@ -32,7 +45,7 @@ See the docs for `niquests_cache <https://niquests-cache.readthedocs.io/en/stabl
3245
client = Client(
3346
"<API_URL>",
3447
"<TOKEN>",
35-
session=CachedSession(cache_name=Path('.cache') / 'http'), # defaults to sqlite cache
48+
session=CachedSession(cache_name=Path('.cache') / 'http'), # by default uses sqlite backend
3649
)
3750
3851
with client:

docs/conf.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import re
1616
import sys
1717

18-
1918
sys.path.insert(0, os.path.abspath("../"))
2019
sys.path.append(os.path.abspath("extensions"))
2120

@@ -129,15 +128,11 @@
129128
"model_construct",
130129
"model_computed_fields",
131130
]
132-
)
131+
),
133132
}
134133
intersphinx_mapping = {
135134
"python": ("https://docs.python.org/3", None),
136135
"homeassistant_api": ("https://homeassistantapi.readthedocs.io/en/stable", None),
137136
"niquests": ("https://niquests.readthedocs.io/en/stable", None),
138-
"niquests-cache": ("https://niquests-cache.readthedocs.io/en/stable/", None)
137+
"niquests-cache": ("https://niquests-cache.readthedocs.io/en/stable/", None),
139138
}
140-
141-
autodoc_type_aliases = {
142-
"JsonValue": "typing.Any",
143-
}

docs/extensions/resourcelinks.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
from typing import Any
66

77
import sphinx
8-
from docutils import nodes
9-
from docutils import utils
10-
from docutils.nodes import Node
11-
from docutils.nodes import system_message
8+
from docutils import nodes, utils
9+
from docutils.nodes import Node, system_message
1210
from docutils.parsers.rst.states import Inliner
1311
from sphinx.application import Sphinx
1412
from sphinx.util.nodes import split_explicit_title

homeassistant_api/models/domains.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
__all__ = (
2828
"AsyncDomain",
29+
"AsyncService",
2930
"BaseDomain",
30-
"Domain",
3131
"BaseService",
32-
"AsyncService",
33-
"Service"
32+
"Domain",
33+
"Service",
3434
)
3535

3636

tests/test_client.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,55 @@
99
from homeassistant_api import WebsocketClient
1010
from homeassistant_api.baseclient import BaseClient
1111

12+
HA_URL = os.environ.get("HOMEASSISTANTAPI_URL", "http://localhost:8123/api")
13+
HA_WS_URL = os.environ.get(
14+
"HOMEASSISTANTAPI_WS_URL",
15+
"ws://localhost:8123/api/websocket",
16+
)
17+
HA_TOKEN = os.environ.get("HOMEASSISTANTAPI_TOKEN", "")
18+
1219

1320
def test_custom_session(nimax_session: niquests.Session) -> None:
1421
with Client(
15-
os.environ.get("HOMEASSISTANTAPI_URL", "http://localhost:8123/api"),
16-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
22+
HA_URL,
23+
HA_TOKEN,
1724
session=nimax_session,
1825
):
1926
pass
2027

2128

2229
def test_default_session(nimax_session: niquests.Session) -> None: # noqa: ARG001
2330
with Client(
24-
os.environ.get("HOMEASSISTANTAPI_URL", "http://localhost:8123/api"),
25-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
31+
HA_URL,
32+
HA_TOKEN,
2633
):
2734
pass
2835

2936

3037
async def test_custom_async_session(nimax_async_session: niquests.AsyncSession) -> None:
3138
async with AsyncClient(
32-
os.environ.get("HOMEASSISTANTAPI_URL", "http://localhost:8123/api"),
33-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
39+
HA_URL,
40+
HA_TOKEN,
3441
session=nimax_async_session,
3542
):
3643
pass
3744

3845

3946
async def test_default_async_session(
40-
nimax_async_session: niquests.AsyncSession, # noqa: ARG001
47+
nimax_async_session: niquests.AsyncSession,
4148
) -> None:
4249
async with AsyncClient(
43-
os.environ.get("HOMEASSISTANTAPI_URL", "http://localhost:8123/api"),
44-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
50+
HA_URL,
51+
HA_TOKEN,
52+
session=nimax_async_session,
4553
):
4654
pass
4755

4856

4957
def test_websocket_client_ping(nimax_session: niquests.Session) -> None:
5058
with WebsocketClient(
51-
os.environ.get("HOMEASSISTANTAPI_WS_URL", "ws://localhost:8123/api/websocket"),
52-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
59+
HA_WS_URL,
60+
HA_TOKEN,
5361
session=nimax_session,
5462
) as client:
5563
assert client.ping_latency() > 0
@@ -59,8 +67,8 @@ async def test_async_websocket_client_ping(
5967
nimax_async_session: niquests.AsyncSession,
6068
) -> None:
6169
async with AsyncWebsocketClient(
62-
os.environ.get("HOMEASSISTANTAPI_WS_URL", "ws://localhost:8123/api/websocket"),
63-
os.environ.get("HOMEASSISTANTAPI_TOKEN", ""),
70+
HA_WS_URL,
71+
HA_TOKEN,
6472
session=nimax_async_session,
6573
) as client:
6674
assert (await client.ping_latency()) > 0

0 commit comments

Comments
 (0)