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+

インストール

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

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

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.0.tar.gz (35.7 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.0-py3-none-any.whl (47.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: boj_api_client-0.1.0.tar.gz
  • Upload date:
  • Size: 35.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for boj_api_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dcf5c405f57d02ccb3ce798c844082a145eb8c37898d25ca9797a6b3a8aec76f
MD5 ed4d23731407957161f0bdce0c41bb84
BLAKE2b-256 1ae3c54416174fa767be8709434ed267e2116dc07d83508b7dc1f3179d07d034

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for boj_api_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3798cdf644c8e9ab8f037f7b94161fa4834136cae5ef05a9ad292d8eccd52ac
MD5 d62de35fa05e6ebaa9436f33cff14426
BLAKE2b-256 ac18efa4477e20db630b92ac44c9ded2bdf85128a722fbc4d94bf385a777081c

See more details on using hashes here.

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