# 用戶端 (https://nolantw.github.io/tdx/reference/clients/)

## `Client`

```python
class Client:
    ...
```

```python
def __init__(
    client_id: str | None = None,
    client_secret: str | None = None,
    *,
    timeout: float | None = None,
    retries: int = 2,
    cache_dir: str | PathLike[str] | None = None,
    http_client: httpx2.Client | None = None,
) -> None
```

以 TDX 會員中心取得的 Client ID 與 Client Secret 查詢，兩者皆未傳入時讀取環境變數 `TDX_CLIENT_ID` 與 `TDX_CLIENT_SECRET`。

暫時性錯誤最多重試 `retries` 次，每次嘗試都計入用量。遇到流量限制時，只有 TDX 指定等待時間才會重試。

指定 `cache_dir` 時會保存回應，TDX 更新資料後才重新下載。確認資料是否更新仍計入用量。

### `api`

```python
def api(service: ServiceName) -> API
```

取得直接呼叫指定服務端點的物件。

### `bus_route`

```python
def bus_route(city: City | str | None, name: str) -> BusRouteResource
```

選擇一條公車路線。

### `rail_station`

```python
def rail_station(system: RailSystem, station_id: str) -> RailStationResource
```

選擇一個鐵路或捷運車站。

### `bike_station`

```python
def bike_station(city: City | str, station_uid: str) -> BikeStationStatus | None
```

取得自行車站與即時數量。

### `car_park`

```python
def car_park(
    area: City | str | ParkingArea,
    car_park_id: str,
) -> CarParkStatus | None
```

取得路外停車場與即時空位。

### `nearby`

```python
def nearby(
    position: Position,
    *,
    radius: float = 500,
    include: tuple[Mode, ...] = ('bus', 'rail', 'bike', 'parking'),
    cache_ttl: float | None = None,
) -> Nearby
```

取得 `radius` 公尺內（上限 1000）的站牌、車站與停車場。

每種資料需要一次以上的請求，可用 `include` 只選擇需要的種類。即時數量不使用 `cache_ttl`。

### `close`

```python
def close() -> None
```

關閉 SDK 建立的 HTTP 用戶端。

## `API`

```python
class API:
    ...
```

直接呼叫一類 TDX 服務的端點。

### `get_json`

```python
def get_json(
    version: Version,
    path: str,
    *,
    params: QueryParams | None = None,
    format: Literal['JSON', 'GEOJSON'] = 'JSON',
    cache_ttl: float | None = None,
) -> JsonValue
```

取得 JSON，可快取 `cache_ttl` 秒。

### `stream_lines`

```python
def stream_lines(
    version: Version,
    path: str,
    *,
    params: QueryParams | None = None,
) -> Generator[bytes]
```

逐行讀取 JSON Lines 回應，不會載入整份內容。

### `post_json`

```python
def post_json(
    version: Version,
    path: str,
    body: JsonValue,
    *,
    params: QueryParams | None = None,
) -> JsonValue
```

將 JSON 送至查詢端點。

### `post_bytes`

```python
def post_bytes(
    version: Version,
    path: str,
    body: JsonValue,
    *,
    params: QueryParams | None = None,
) -> bytes
```

將 JSON 送至查詢端點，取得原始回應內容。

### `get_xml`

```python
def get_xml(
    version: Version,
    path: str,
    *,
    params: QueryParams | None = None,
    cache_ttl: float | None = None,
) -> bytes
```

取得 XML，可快取 `cache_ttl` 秒。

### `get_bytes`

```python
def get_bytes(
    version: Version,
    path: str,
    *,
    params: QueryParams | None = None,
) -> bytes
```

取得原始內容，不指定格式參數。