Origin Core Master SDK (Python port of @originaiagent/core-master-sdk)
Project description
originaiagent-core-master-sdk-python
全 Python ツール共通の origin-core マスタ参照 SDK。TS 版 @originaiagent/core-master-sdk の Python ポート (v0.2.0-alpha.1 と機能同等 / read-only + write 21 メソッド)。
- 設計書: core-master-sdk-python-design.md
- TS-Python 対応表: core-master-sdk-python-parity.md
- Python 要件: 3.11 以降
インストール
Private repo のため GITHUB_TOKEN を環境変数に設定した上で pip から git URL で取得する。
# 最新 alpha をインストール
pip install "git+https://${GITHUB_TOKEN}@github.com/originaiagent/origin-core.git@core-master-sdk-python-v0.2.0-alpha.1#subdirectory=packages/core-master-sdk-python"
Streamlit Cloud では .streamlit/secrets.toml に GITHUB_TOKEN を置き、requirements.txt に上記 URL を 1 行追加するだけで導入できる。
環境変数
| 変数 | 必須 | 用途 |
|---|---|---|
CORE_MASTER_BASE_URL |
必須 | Core API のベース URL (例: https://your-core-api.example.com) |
CORE_MASTER_INTERNAL_API_KEY |
必須 | INTERNAL API キー。INTERNAL_API_KEY も後方互換フォールバックとして読むが、両方設定時は CORE_MASTER_* を優先し warnings.warn を出す |
使用例
1. sync で products を取得して DataFrame 表示
from core_master_sdk import create_core_master
from core_master_sdk.pandas_helpers import products_to_df
# 引数を省略すると環境変数から自動読み込み
with create_core_master() as client:
products = client.products.search("配線カバー", limit=50)
df = products_to_df(products)
print(df[["id", "product_name", "jan_code"]].head())
2. async で ASIN を一括逆引き
import asyncio
from core_master_sdk import create_async_core_master
async def map_asins() -> None:
async with create_async_core_master() as client:
result = await client.mall_identifiers.amazon_asin_map(
["B000000001", "B000000002", "B000000003"]
)
print(result) # {"B000000001": 422, "B000000002": 423, "B000000003": None}
asyncio.run(map_asins())
3. 書き込み (PATCH)
from core_master_sdk import create_core_master
# 書き込み時は default_tool_name を指定するか、メソッド呼出時に tool_name を渡すのが推奨
with create_core_master(default_tool_name="my-tool") as client:
# 1. Product 基本情報の更新
updated = client.products.patch(123, {"product_name": "新名称"}, if_match="2026-04-22T10:00:00Z")
# 2. Product Cost の更新 (change_reason 必須)
client.product_costs.patch(456, {"cost": 1000, "change_reason": "原材料高騰"})
# 3. Product Specs / SKUs の更新 (Read なし、Write のみ提供)
client.product_specs.patch(789, {"spec_value": "New Value"})
client.product_skus.patch("SKU-001", {"variation_name": "Red-M"})
4. エラーハンドリング
from core_master_sdk import (
create_core_master,
CoreMasterNotFound,
CoreMasterAuthError,
)
with create_core_master() as client:
try:
product = client.products.get_by_id(999999)
except CoreMasterNotFound:
product = None # 静かに握りつぶして OK
except CoreMasterAuthError:
raise # env 設定ミスなので上位で止める
Streamlit アプリに組み込む
examples/streamlit_demo.py 参照。streamlit run examples/streamlit_demo.py で動作する。
提供リソース (21 メソッド × sync+async)
| リソース | メソッド |
|---|---|
products |
list / get_by_id / by_ids / search / get_by_logi_id / patch |
product_groups |
list / get_by_id |
product_costs |
list_by_product_id / get_latest_by_product_id / patch |
product_specs |
patch |
product_skus |
patch |
mall_identifiers |
list_by_product_id / lookup / lookup_bulk / amazon_asin_map |
mall_code_definitions |
list / get_by_mall_id |
malls |
list / get_by_id |
詳細は TS-Python 対応表 (parity.md) を参照。
例外階層
CoreMasterError (Exception)
├── CoreMasterAuthError (401/403, retryable=False)
├── CoreMasterNotFound (404, retryable=False)
├── CoreMasterPreconditionFailedError (412, retryable=False)
├── CoreMasterValidationError (400/422, retryable=False)
├── CoreMasterRateLimitError (429, retryable=True)
└── CoreMasterUpstreamError (5xx/network/timeout, retryable=True)
GET および PATCH は retries=2 で retry (指数バックオフ + jitter)。PATCH は Idempotency-Key を retry 間で固定して安全性を確保しています。
429 / 5xx / タイムアウト / ネットワーク断が retry 対象。
Caveats / 既知の制約
product_skus.patch は last-write-wins
product_skus テーブルには updated_at カラムが物理的に存在しないため、If-Match ヘッダでの楽観ロックが server 側で適用されません (v0.3 server 実装 server/routes/masterV1Write.ts 参照)。
SDK 側で if_match=... を渡しても、server はそのヘッダを無視し、直近の書き込みで上書きする last-write-wins 挙動になります。
- 同時編集が発生しうる業務フローで
product_skus.patchを使う場合は、SDK 呼び出し側で楽観ロックを別途実装する (例: 事前 SELECT → アプリ側で diff 判定 → 書込) か、単一ワーカーからのみ呼び出す運用を推奨します。 products.patch/product_specs.patch/product_costs.patchはupdated_atカラムがあるため、if_match=<ISO 8601>を渡せば 412 (CoreMasterPreconditionFailedError) が返り楽観ロックとして機能します。
PATCH の retry と副作用
PATCH は retries=2 で retry されます。Idempotency-Key は retry 間で固定 され、server 側 audit_log_core_writes の UNIQUE 制約で二重実行を防止します。未指定時は SDK が uuid.uuid4().hex を自動付与します。
開発
cd packages/core-master-sdk-python
python3.11 -m venv .venv
.venv/bin/pip install -e '.[dev]'
.venv/bin/pytest --cov
バージョニング
0.2.0a1: Write support (PATCH 4 resource) 追加, retry 緩和0.1.0a1: Initial read-only release (14 メソッド)
TS 版と機能マイルストーンで一致させるが、patch レベルでは独立インクリメントされることがある (設計書 §10.1)。
関連
- 設計書: docs/core-master-sdk-design.md (TS 版共通設計)
- Core API:
server/routes/masterV1.ts(13 GET エンドポイント)
License
Apache License 2.0. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file originaiagent_core_master_sdk_python-0.2.0a1.tar.gz.
File metadata
- Download URL: originaiagent_core_master_sdk_python-0.2.0a1.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73894f2e47bcaea00835b1f0b95ac03b5a1a2f55bddf1959404bf4bc44dd3f92
|
|
| MD5 |
d3f5753e142a9e3b4665edc80a5a99f6
|
|
| BLAKE2b-256 |
d693e1d068da01b86a999f825f2f25f493f1544e26687617917f49f413837aa9
|
Provenance
The following attestation bundles were made for originaiagent_core_master_sdk_python-0.2.0a1.tar.gz:
Publisher:
publish-python-sdk.yml on originaiagent/origin-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
originaiagent_core_master_sdk_python-0.2.0a1.tar.gz -
Subject digest:
73894f2e47bcaea00835b1f0b95ac03b5a1a2f55bddf1959404bf4bc44dd3f92 - Sigstore transparency entry: 1362598620
- Sigstore integration time:
-
Permalink:
originaiagent/origin-core@924d51053e7dcbdc40f361eb491a30bb87c142c1 -
Branch / Tag:
refs/tags/core-master-sdk-python-v0.2.0-alpha.1 - Owner: https://github.com/originaiagent
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-sdk.yml@924d51053e7dcbdc40f361eb491a30bb87c142c1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file originaiagent_core_master_sdk_python-0.2.0a1-py3-none-any.whl.
File metadata
- Download URL: originaiagent_core_master_sdk_python-0.2.0a1-py3-none-any.whl
- Upload date:
- Size: 39.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a400685b0224ef884a7ee144f4d4fc41e3e5edc86f099766d32c96aad53f97bf
|
|
| MD5 |
f6577022ffa053d474d81a7b1738d7f2
|
|
| BLAKE2b-256 |
fd8603a7ffe940cf0a51e98994065b291e76fad9a2543d161f9d311727ee227d
|
Provenance
The following attestation bundles were made for originaiagent_core_master_sdk_python-0.2.0a1-py3-none-any.whl:
Publisher:
publish-python-sdk.yml on originaiagent/origin-core
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
originaiagent_core_master_sdk_python-0.2.0a1-py3-none-any.whl -
Subject digest:
a400685b0224ef884a7ee144f4d4fc41e3e5edc86f099766d32c96aad53f97bf - Sigstore transparency entry: 1362598636
- Sigstore integration time:
-
Permalink:
originaiagent/origin-core@924d51053e7dcbdc40f361eb491a30bb87c142c1 -
Branch / Tag:
refs/tags/core-master-sdk-python-v0.2.0-alpha.1 - Owner: https://github.com/originaiagent
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python-sdk.yml@924d51053e7dcbdc40f361eb491a30bb87c142c1 -
Trigger Event:
push
-
Statement type: