# Clients and caching (https://nolantw.github.io/cwa/en/advanced/clients/)

Module functions such as `cwa.weather.get()` are the easiest start, but each call opens a new connection and shares no cache. For many queries, server use or async code, use a client.

| Use | Best for |
| --- | --- |
| Module functions, such as `cwa.weather.get()` | Scripts, occasional queries |
| `CWA` | Many queries in one program, caching or custom settings |
| `AsyncCWA` | asyncio code, such as FastAPI and other async web frameworks |

## Client

`CWA` has the same resources as the module functions, such as `client.weather` and `client.forecast`, used the same way:

```python
from cwa import CWA

with CWA() as client:
    weather = client.weather.get("臺北市信義區")
    quake = client.earthquakes.latest()
```

Use it with `with` so the connection closes when you are done. Without `with`, call `client.close()` yourself. Calls after closing raise `ClientClosedError`.

| Parameter | Default | Meaning |
| --- | --- | --- |
| `api_key` | The `CWA_API_KEY` environment variable | API key |
| `timeout` | 30 seconds | Timeout for one request |
| `retries` | 3 | Retries after network or server errors |
| `cache` | `True` | In-memory caching; see [Caching](#caching) |
| `max_concurrency` | 8 | Maximum simultaneous requests in a batch |
| `http_client` | None | Your own HTTP client; see [Custom HTTP client](#custom-http-client) |

## Async

`AsyncCWA` has the same methods as `CWA`, awaited:

```python
import asyncio
from cwa import AsyncCWA

async def main() -> None:
    async with AsyncCWA() as client:
        weather = await client.weather.get("臺北市信義區")
        print(weather.current)

asyncio.run(main())
```

## Caching

Each client keeps up to 64 MiB of responses in memory, so repeating a query for the same dataset shortly afterwards makes no new request. The cache is never written to disk, is freed when the client closes, and is separate for each API key.

CWA responses do not say how long they may be cached, so the SDK sets a limit by data type:

| Datasets | Reused for at most |
| --- | --- |
| Forecasts (`F-`) | 10 minutes |
| Observations (`O-`) | 5 minutes |
| Earthquakes (`E-`) | 1 minute |
| Warnings and typhoons (`W-`) | Not cached |
| Other datasets | 1 minute |

To always get fresh data, disable it with `CWA(cache=False)`.

## Custom HTTP client

For a proxy, custom certificates or other network settings, pass an `httpx2.Client` you created (`httpx2.AsyncClient` for async):

```python
import httpx2
from cwa import CWA

with httpx2.Client(proxy="http://proxy.example:8080", timeout=60) as http:
    with CWA(http_client=http) as client:
        weather = client.weather.get("臺北市信義區")
```

You manage that HTTP client:

| Concern | Behavior |
| --- | --- |
| Closing | The SDK does not close it; close it yourself |
| Timeout | Set it on the HTTP client; also passing `timeout=` raises `ConfigurationError` |
| Caching | The SDK does not add caching; configure a `hishel.httpx2` cache transport on it if needed. Also passing `cache=True` raises `ConfigurationError` |