Skip to main content

Python client for Bank of Japan timeseries statistics API

Project description

boj-api-client

日本銀行「時系列統計データ検索サイト API」(https://www.stat-search.boj.or.jp/api/v1)向けの Python クライアントです。

主な特徴

  • 同期/非同期クライアント
    • BojClient
    • AsyncBojClient
  • 通信フォーマットは JSON 固定(format=json
  • 型付き Query/Response モデル
  • getDataCode の 250 件超を自動分割して統合
  • NEXTPOSITION を使った自動ページング
  • リトライ/スロットリング
  • 途中失敗時の partial result + checkpoint 再開

対応 API

  • getDataCode
  • getDataLayer
  • getMetadata

要件

  • Python 3.11+

インストール

通常利用(PyPI):

pip install boj-api-client

PyPI: https://pypi.org/project/boj-api-client/

ローカル開発環境で使う場合:

pip install -e .

または

uv pip install -e .

クイックスタート(同期)

from boj_api_client import BojClient
from boj_api_client.timeseries import DataCodeQuery

with BojClient() as client:
    result = client.timeseries.get_data_code(
        DataCodeQuery(
            db="CO",
            code=["TK99F1000601GCQ01000"],
            lang="JP",
        )
    )

for series in result.series:
    print(series.series_code, len(series.points))

クイックスタート(非同期)

import asyncio

from boj_api_client import AsyncBojClient
from boj_api_client.timeseries import MetadataQuery


async def main() -> None:
    async with AsyncBojClient() as client:
        result = await client.timeseries.get_metadata(
            MetadataQuery(db="FM08", lang="JP")
        )
    print(result.envelope.status, len(result.entries))


asyncio.run(main())

ページ単位で取得する(iter_*)

from boj_api_client import BojClient
from boj_api_client.timeseries import DataLayerQuery

with BojClient() as client:
    for page in client.timeseries.iter_data_layer(
        DataLayerQuery(db="MD10", frequency="Q", layer1="*")
    ):
        print(page.envelope.status, len(page.series))

getDataLayer の auto-partition を有効化する

getDataLayer が 1,250 系列上限に達したとき、metadata 経由の fallback を使う設定です。

from boj_api_client import BojClient, BojClientConfig
from boj_api_client.config import TimeSeriesConfig
from boj_api_client.timeseries import DataLayerQuery

config = BojClientConfig(
    timeseries=TimeSeriesConfig(enable_layer_auto_partition=True),
)

with BojClient(config=config) as client:
    result = client.timeseries.get_data_layer(
        DataLayerQuery(db="MD10", frequency="Q", layer1="*", lang="JP")
    )

partial result から再開する

from boj_api_client import BojClient
from boj_api_client.core.errors import BojPartialResultError
from boj_api_client.timeseries import DataCodeQuery

query = DataCodeQuery(db="CO", code=["TK99F1000601GCQ01000"], lang="JP")

with BojClient() as client:
    try:
        result = client.timeseries.get_data_code(query)
    except BojPartialResultError as exc:
        if exc.checkpoint_id is None:
            raise
        result = client.timeseries.get_data_code(
            query,
            checkpoint_id=exc.checkpoint_id,
        )

設定

from boj_api_client import BojClient, BojClientConfig
from boj_api_client.config import (
    CheckpointConfig,
    RetryConfig,
    ThrottlingConfig,
    TimeSeriesConfig,
    TransportConfig,
)

config = BojClientConfig(
    transport=TransportConfig(timeout_read_seconds=20.0),
    retry=RetryConfig(max_attempts=3, max_backoff_seconds=5.0),
    throttling=ThrottlingConfig(min_wait_interval_seconds=1.0),
    checkpoint=CheckpointConfig(enabled=True, ttl_seconds=86400.0),
    timeseries=TimeSeriesConfig(enable_layer_auto_partition=False),
)

with BojClient(config=config) as client:
    ...

主な例外

  • BojValidationError
  • BojServerError
  • BojUnavailableError
  • BojTransportError
  • BojProtocolError
  • BojPartialResultError
  • BojClientClosedError

live contract test

BOJ_RUN_LIVE=1 pytest -m live -q tests/contract_live

getDataLayer live テストを実行する場合:

  • BOJ_LIVE_LAYER1(必須)
  • BOJ_LIVE_LAYER_DB(任意、既定 MD10
  • BOJ_LIVE_LAYER_FREQUENCY(任意、既定 Q

ドキュメント

  • パッケージ構成と責務: docs/architecture.md
  • API 仕様の実装対応: docs/api_overview.md

開発者向け

  • sync orchestrator は async source から生成:
    • uv run --extra dev python scripts/generate_sync_orchestrator.py
  • PyPI 公開:
    • 初回のみ、PyPI 側で Trusted Publisher に GitHub Actions を登録
    • pyproject.tomlproject.version を更新後、v<version> タグを push
    • GitHub Actions (.github/workflows/ci.yml) がテスト成功後に自動 publish

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

boj_api_client-0.1.2.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boj_api_client-0.1.2-py3-none-any.whl (47.6 kB view details)

Uploaded Python 3

File details

Details for the file boj_api_client-0.1.2.tar.gz.

File metadata

  • Download URL: boj_api_client-0.1.2.tar.gz
  • Upload date:
  • Size: 33.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for boj_api_client-0.1.2.tar.gz
Algorithm Hash digest
SHA256 efbece72770e9bc6313d61789e998f71057598df0549f54f711ef4bd32355ea7
MD5 c84ad8e94b98d09128b9d0187484cbb5
BLAKE2b-256 d93cb610abd65016ecb6bb6e56a2cffe1e87c357ed632a801f2e119aa0a4f0a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for boj_api_client-0.1.2.tar.gz:

Publisher: ci.yml on delihiros/boj-api-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file boj_api_client-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: boj_api_client-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 47.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for boj_api_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1935c20777b8b08066e0f7cebde4f5f6c5fdc790474d2d801e7e6e157524493
MD5 833fcbb4c1777301b47502bff99744ab
BLAKE2b-256 6acf77ec8128f1afa4be9b9842b28d6deff728935090714d9c0169cf49bdcdc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for boj_api_client-0.1.2-py3-none-any.whl:

Publisher: ci.yml on delihiros/boj-api-client

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