nozk.py 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nozk_py-0.1.0/.github/workflows/ci.yml +40 -0
- nozk_py-0.1.0/.github/workflows/publish.yml +69 -0
- nozk_py-0.1.0/.gitignore +8 -0
- nozk_py-0.1.0/AGENTS.md +34 -0
- nozk_py-0.1.0/LICENSE +21 -0
- nozk_py-0.1.0/PKG-INFO +181 -0
- nozk_py-0.1.0/README.md +157 -0
- nozk_py-0.1.0/docs/agent-guide.md +48 -0
- nozk_py-0.1.0/docs/reference.md +163 -0
- nozk_py-0.1.0/pyproject.toml +98 -0
- nozk_py-0.1.0/src/nozk/__init__.py +128 -0
- nozk_py-0.1.0/src/nozk/_codec.py +227 -0
- nozk_py-0.1.0/src/nozk/_endpoints.py +263 -0
- nozk_py-0.1.0/src/nozk/_resources.py +163 -0
- nozk_py-0.1.0/src/nozk/_transport.py +136 -0
- nozk_py-0.1.0/src/nozk/client.py +295 -0
- nozk_py-0.1.0/src/nozk/errors.py +95 -0
- nozk_py-0.1.0/src/nozk/models/__init__.py +100 -0
- nozk_py-0.1.0/src/nozk/models/_base.py +56 -0
- nozk_py-0.1.0/src/nozk/models/crypto.py +39 -0
- nozk_py-0.1.0/src/nozk/models/market.py +130 -0
- nozk_py-0.1.0/src/nozk/models/me.py +121 -0
- nozk_py-0.1.0/src/nozk/models/params.py +122 -0
- nozk_py-0.1.0/src/nozk/py.typed +0 -0
- nozk_py-0.1.0/tests/__init__.py +0 -0
- nozk_py-0.1.0/tests/fixtures.py +363 -0
- nozk_py-0.1.0/tests/test_architecture.py +163 -0
- nozk_py-0.1.0/tests/test_contract_openapi.py +50 -0
- nozk_py-0.1.0/tests/test_endpoints.py +178 -0
- nozk_py-0.1.0/tests/test_errors.py +125 -0
- nozk_py-0.1.0/tests/test_pagination.py +107 -0
- nozk_py-0.1.0/tests/test_protocol.py +151 -0
- nozk_py-0.1.0/uv.lock +404 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
check:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
strategy:
|
|
15
|
+
fail-fast: false
|
|
16
|
+
matrix:
|
|
17
|
+
python-version: ["3.11", "3.12", "3.13"]
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- uses: astral-sh/setup-uv@v5
|
|
23
|
+
with:
|
|
24
|
+
python-version: ${{ matrix.python-version }}
|
|
25
|
+
enable-cache: true
|
|
26
|
+
|
|
27
|
+
- name: Install dependencies
|
|
28
|
+
run: uv sync --locked
|
|
29
|
+
|
|
30
|
+
- name: Format check
|
|
31
|
+
run: uv run ruff format --check .
|
|
32
|
+
|
|
33
|
+
- name: Lint
|
|
34
|
+
run: uv run ruff check .
|
|
35
|
+
|
|
36
|
+
- name: Type check
|
|
37
|
+
run: uv run pyright
|
|
38
|
+
|
|
39
|
+
- name: Test
|
|
40
|
+
run: uv run pytest -v
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
build:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
|
|
16
|
+
- uses: astral-sh/setup-uv@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.13"
|
|
19
|
+
enable-cache: true
|
|
20
|
+
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: uv sync --locked
|
|
23
|
+
|
|
24
|
+
- name: Verify release tag matches pyproject.toml version
|
|
25
|
+
env:
|
|
26
|
+
RELEASE_TAG: ${{ github.ref_name }}
|
|
27
|
+
run: |
|
|
28
|
+
uv run python -c "
|
|
29
|
+
import os, tomllib
|
|
30
|
+
version = tomllib.load(open('pyproject.toml', 'rb'))['project']['version']
|
|
31
|
+
tag = os.environ['RELEASE_TAG']
|
|
32
|
+
assert tag == f'v{version}', f'Tag {tag!r} does not match pyproject.toml version v{version}'
|
|
33
|
+
print(f'Verified tag {tag} matches pyproject.toml')
|
|
34
|
+
"
|
|
35
|
+
|
|
36
|
+
- name: Run checks
|
|
37
|
+
run: |
|
|
38
|
+
uv run ruff format --check .
|
|
39
|
+
uv run ruff check .
|
|
40
|
+
uv run pyright
|
|
41
|
+
uv run pytest -v
|
|
42
|
+
|
|
43
|
+
- name: Build sdist and wheel
|
|
44
|
+
run: uv build
|
|
45
|
+
|
|
46
|
+
- name: Upload dist artifacts
|
|
47
|
+
uses: actions/upload-artifact@v4
|
|
48
|
+
with:
|
|
49
|
+
name: python-package-distributions
|
|
50
|
+
path: dist/
|
|
51
|
+
|
|
52
|
+
publish:
|
|
53
|
+
needs: build
|
|
54
|
+
runs-on: ubuntu-latest
|
|
55
|
+
environment:
|
|
56
|
+
name: pypi
|
|
57
|
+
url: https://pypi.org/p/nozk.py
|
|
58
|
+
permissions:
|
|
59
|
+
id-token: write
|
|
60
|
+
|
|
61
|
+
steps:
|
|
62
|
+
- name: Download dist artifacts
|
|
63
|
+
uses: actions/download-artifact@v4
|
|
64
|
+
with:
|
|
65
|
+
name: python-package-distributions
|
|
66
|
+
path: dist/
|
|
67
|
+
|
|
68
|
+
- name: Publish to PyPI
|
|
69
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
nozk_py-0.1.0/.gitignore
ADDED
nozk_py-0.1.0/AGENTS.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
Instructions for AI coding agents modifying this repository (`nozk.py`).
|
|
4
|
+
For agents *using* this library in external projects, see [`docs/agent-guide.md`](./docs/agent-guide.md) and [`docs/reference.md`](./docs/reference.md).
|
|
5
|
+
|
|
6
|
+
## Source of Truth for Architecture
|
|
7
|
+
|
|
8
|
+
Module layering, Sans-I/O boundaries, model immutability, and Sync/Async symmetry are enforced directly as executable tests in [`tests/test_architecture.py`](./tests/test_architecture.py). Read that file before adding modules or changing imports.
|
|
9
|
+
|
|
10
|
+
## Verification Commands
|
|
11
|
+
|
|
12
|
+
Run all checks before finishing any change:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
uv run ruff format --check .
|
|
16
|
+
uv run ruff check .
|
|
17
|
+
uv run pyright
|
|
18
|
+
uv run pytest
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Run live OpenAPI drift checks when changing endpoints, query params, or error codes:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uv run pytest -m contract
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Repository Rules
|
|
28
|
+
|
|
29
|
+
1. **Strict DAG & Sans-I/O**: Never import `httpx` in `models/*`, `errors.py`, `_codec.py`, or `_endpoints.py`. Register any new source module in `MODULE_LAYERS` in `tests/test_architecture.py`.
|
|
30
|
+
2. **Sync / Async Symmetry**: Any public method added or changed on `NozkClient`, `CryptoResource`, or `MeResource` must have an exact signature mirror on `AsyncNozkClient`, `AsyncCryptoResource`, or `AsyncMeResource`.
|
|
31
|
+
3. **No Implicit State Machines**: Do not add client-side caches, hidden retry loops, or mutable state on client/transport instances.
|
|
32
|
+
4. **No Type Ignores**: Keep `pyright` (`strict` mode) at 0 errors without `# type: ignore` or `# pyright: ignore`.
|
|
33
|
+
5. **Single Source of Truth for Docs**: Keep API signatures, models, and error codes updated in `docs/reference.md` without duplicating full schemas elsewhere.
|
|
34
|
+
6. **Never Commit Secrets**: Never store `NOZK_API_KEY` values in repository files, tests, or fixtures.
|
nozk_py-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mikuteto-dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
nozk_py-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: nozk.py
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Clean, typed, stateless Python client (sync & async) for the Nozk MC Public API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/mikuteto-dev/nozk.py
|
|
6
|
+
Project-URL: Repository, https://github.com/mikuteto-dev/nozk.py
|
|
7
|
+
Project-URL: Documentation, https://github.com/mikuteto-dev/nozk.py/blob/main/docs/reference.md
|
|
8
|
+
Project-URL: Issues, https://github.com/mikuteto-dev/nozk.py/issues
|
|
9
|
+
Author: mikuteto-dev
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: api,economy,httpx,minecraft,nozk,pydantic,sdk
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: httpx<1.0,>=0.27
|
|
22
|
+
Requires-Dist: pydantic<3.0,>=2.7
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# nozk.py
|
|
26
|
+
|
|
27
|
+
Minecraft 経済サーバー **Nozk MC** 公開 API(`https://api.nozk.dev`)の Python クライアントライブラリです。
|
|
28
|
+
|
|
29
|
+
- **対応 API**: 読み取り専用 REST API(全 17 エンドポイント)
|
|
30
|
+
- **実行モデル**: 同期 (`NozkClient`) / 非同期 (`AsyncNozkClient`) 両対応
|
|
31
|
+
- **型・モデル**: Pydantic v2 による不変モデル(`py.typed` 同梱)
|
|
32
|
+
- **動作特性**: ステートレス(自動リトライ・クライアント側キャッシュなし)
|
|
33
|
+
- **対応環境**: Python `>= 3.11` / 依存パッケージ `httpx`, `pydantic`
|
|
34
|
+
|
|
35
|
+
## インストール
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
uv add nozk.py
|
|
39
|
+
# または
|
|
40
|
+
pip install nozk.py
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
インポート名は **`nozk`** です。
|
|
44
|
+
|
|
45
|
+
## 認証
|
|
46
|
+
|
|
47
|
+
API キーは環境変数 `NOZK_API_KEY` に設定するか、クライアント初期化時に渡します。
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
export NOZK_API_KEY="nozk_..."
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from nozk import NozkClient
|
|
55
|
+
|
|
56
|
+
client = NozkClient() # 環境変数 NOZK_API_KEY を参照
|
|
57
|
+
client = NozkClient("nozk_...") # 明示指定(環境変数より優先)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## クイックスタート
|
|
61
|
+
|
|
62
|
+
### 同期クライアント
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from nozk import NozkClient
|
|
66
|
+
|
|
67
|
+
with NozkClient() as client:
|
|
68
|
+
# 相場・銘柄
|
|
69
|
+
items = client.market()
|
|
70
|
+
bread = client.item("BREAD")
|
|
71
|
+
print(f"{bread.name_ja}: {bread.price}G (24h: {bread.change_24h_pct}%)")
|
|
72
|
+
|
|
73
|
+
# 仮想通貨
|
|
74
|
+
vivo = client.crypto.get("VIVO")
|
|
75
|
+
candles = client.crypto.candles("VIVO", limit=24)
|
|
76
|
+
|
|
77
|
+
# 自分のデータ(キーに該当スコープが必要)
|
|
78
|
+
me = client.me.get()
|
|
79
|
+
wallet = client.me.wallet()
|
|
80
|
+
print(f"{me.name}: 残高 {wallet.balance}G / 合計 {wallet.total}G")
|
|
81
|
+
|
|
82
|
+
# ゲーム設定値の ID 引き
|
|
83
|
+
params = client.params(category="money")
|
|
84
|
+
fee = params.row("money.transfer_fee")
|
|
85
|
+
if fee is not None:
|
|
86
|
+
print(fee.label, fee.text)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 非同期クライアント
|
|
90
|
+
|
|
91
|
+
`AsyncNozkClient` は `NozkClient` と同一のメソッド名・引数シグネチャを持ちます。
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
import asyncio
|
|
95
|
+
from nozk import AsyncNozkClient
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def main() -> None:
|
|
99
|
+
async with AsyncNozkClient() as client:
|
|
100
|
+
summary, coins = await asyncio.gather(
|
|
101
|
+
client.summary(),
|
|
102
|
+
client.crypto.list(),
|
|
103
|
+
)
|
|
104
|
+
print(summary.cpi, [c.code for c in coins])
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
asyncio.run(main())
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### 約定のページ送り
|
|
111
|
+
|
|
112
|
+
`trades()` / `me.trades()` は常に 1 リクエストのみ送信します。過去へ遡る場合は遅延イテレータ `iter_trades()` / `me.iter_trades()` を使用します。
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from itertools import islice
|
|
116
|
+
from nozk import NozkClient
|
|
117
|
+
|
|
118
|
+
with NozkClient() as client:
|
|
119
|
+
recent_200 = list(islice(client.iter_trades(item="BREAD", page_size=100), 200))
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### エラーハンドリング
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from nozk import (
|
|
126
|
+
AuthenticationError,
|
|
127
|
+
ErrorCode,
|
|
128
|
+
NotFoundError,
|
|
129
|
+
NozkClient,
|
|
130
|
+
RateLimitError,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
with NozkClient() as client:
|
|
134
|
+
try:
|
|
135
|
+
client.item("UNKNOWN")
|
|
136
|
+
except NotFoundError as err:
|
|
137
|
+
print(err.code, err.message) # ErrorCode.ITEM_NOT_FOUND
|
|
138
|
+
except RateLimitError as err:
|
|
139
|
+
print(f"retry_after={err.retry_after}s")
|
|
140
|
+
except AuthenticationError as err:
|
|
141
|
+
if err.code == ErrorCode.INVALID_API_KEY:
|
|
142
|
+
print("API キーが無効または失効済みです")
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### レート制限残回数と `X-Cache: stale` の監視
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
from nozk import NozkClient, ResponseMeta
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def on_response(meta: ResponseMeta) -> None:
|
|
152
|
+
if meta.is_stale:
|
|
153
|
+
print(f"[stale] {meta.path}")
|
|
154
|
+
if meta.rate_limit is not None:
|
|
155
|
+
rl = meta.rate_limit
|
|
156
|
+
print(f"quota: {rl.remaining}/{rl.limit} (day: {rl.remaining_day}/{rl.limit_day})")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
with NozkClient(on_response=on_response) as client:
|
|
160
|
+
client.market()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## ドキュメント
|
|
164
|
+
|
|
165
|
+
- **API リファレンス**: [docs/reference.md](https://github.com/mikuteto-dev/nozk.py/blob/main/docs/reference.md) — 全メソッド・引数・レスポンスモデル・例外の詳細
|
|
166
|
+
- **AI エージェント向け利用ガイド**: [docs/agent-guide.md](https://github.com/mikuteto-dev/nozk.py/blob/main/docs/agent-guide.md) — エージェント利用時の制約・注意点・JSON 出力パターン
|
|
167
|
+
- **Nozk 公式 API ドキュメント**: https://developers.nozk.dev/
|
|
168
|
+
|
|
169
|
+
## 開発
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
uv sync
|
|
173
|
+
uv run ruff check .
|
|
174
|
+
uv run pyright
|
|
175
|
+
uv run pytest
|
|
176
|
+
uv run pytest -m contract # 本番 OpenAPI 仕様との整合テスト
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## ライセンス
|
|
180
|
+
|
|
181
|
+
MIT License
|
nozk_py-0.1.0/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# nozk.py
|
|
2
|
+
|
|
3
|
+
Minecraft 経済サーバー **Nozk MC** 公開 API(`https://api.nozk.dev`)の Python クライアントライブラリです。
|
|
4
|
+
|
|
5
|
+
- **対応 API**: 読み取り専用 REST API(全 17 エンドポイント)
|
|
6
|
+
- **実行モデル**: 同期 (`NozkClient`) / 非同期 (`AsyncNozkClient`) 両対応
|
|
7
|
+
- **型・モデル**: Pydantic v2 による不変モデル(`py.typed` 同梱)
|
|
8
|
+
- **動作特性**: ステートレス(自動リトライ・クライアント側キャッシュなし)
|
|
9
|
+
- **対応環境**: Python `>= 3.11` / 依存パッケージ `httpx`, `pydantic`
|
|
10
|
+
|
|
11
|
+
## インストール
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
uv add nozk.py
|
|
15
|
+
# または
|
|
16
|
+
pip install nozk.py
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
インポート名は **`nozk`** です。
|
|
20
|
+
|
|
21
|
+
## 認証
|
|
22
|
+
|
|
23
|
+
API キーは環境変数 `NOZK_API_KEY` に設定するか、クライアント初期化時に渡します。
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
export NOZK_API_KEY="nozk_..."
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from nozk import NozkClient
|
|
31
|
+
|
|
32
|
+
client = NozkClient() # 環境変数 NOZK_API_KEY を参照
|
|
33
|
+
client = NozkClient("nozk_...") # 明示指定(環境変数より優先)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## クイックスタート
|
|
37
|
+
|
|
38
|
+
### 同期クライアント
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from nozk import NozkClient
|
|
42
|
+
|
|
43
|
+
with NozkClient() as client:
|
|
44
|
+
# 相場・銘柄
|
|
45
|
+
items = client.market()
|
|
46
|
+
bread = client.item("BREAD")
|
|
47
|
+
print(f"{bread.name_ja}: {bread.price}G (24h: {bread.change_24h_pct}%)")
|
|
48
|
+
|
|
49
|
+
# 仮想通貨
|
|
50
|
+
vivo = client.crypto.get("VIVO")
|
|
51
|
+
candles = client.crypto.candles("VIVO", limit=24)
|
|
52
|
+
|
|
53
|
+
# 自分のデータ(キーに該当スコープが必要)
|
|
54
|
+
me = client.me.get()
|
|
55
|
+
wallet = client.me.wallet()
|
|
56
|
+
print(f"{me.name}: 残高 {wallet.balance}G / 合計 {wallet.total}G")
|
|
57
|
+
|
|
58
|
+
# ゲーム設定値の ID 引き
|
|
59
|
+
params = client.params(category="money")
|
|
60
|
+
fee = params.row("money.transfer_fee")
|
|
61
|
+
if fee is not None:
|
|
62
|
+
print(fee.label, fee.text)
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 非同期クライアント
|
|
66
|
+
|
|
67
|
+
`AsyncNozkClient` は `NozkClient` と同一のメソッド名・引数シグネチャを持ちます。
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
import asyncio
|
|
71
|
+
from nozk import AsyncNozkClient
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
async def main() -> None:
|
|
75
|
+
async with AsyncNozkClient() as client:
|
|
76
|
+
summary, coins = await asyncio.gather(
|
|
77
|
+
client.summary(),
|
|
78
|
+
client.crypto.list(),
|
|
79
|
+
)
|
|
80
|
+
print(summary.cpi, [c.code for c in coins])
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
asyncio.run(main())
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 約定のページ送り
|
|
87
|
+
|
|
88
|
+
`trades()` / `me.trades()` は常に 1 リクエストのみ送信します。過去へ遡る場合は遅延イテレータ `iter_trades()` / `me.iter_trades()` を使用します。
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from itertools import islice
|
|
92
|
+
from nozk import NozkClient
|
|
93
|
+
|
|
94
|
+
with NozkClient() as client:
|
|
95
|
+
recent_200 = list(islice(client.iter_trades(item="BREAD", page_size=100), 200))
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### エラーハンドリング
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from nozk import (
|
|
102
|
+
AuthenticationError,
|
|
103
|
+
ErrorCode,
|
|
104
|
+
NotFoundError,
|
|
105
|
+
NozkClient,
|
|
106
|
+
RateLimitError,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
with NozkClient() as client:
|
|
110
|
+
try:
|
|
111
|
+
client.item("UNKNOWN")
|
|
112
|
+
except NotFoundError as err:
|
|
113
|
+
print(err.code, err.message) # ErrorCode.ITEM_NOT_FOUND
|
|
114
|
+
except RateLimitError as err:
|
|
115
|
+
print(f"retry_after={err.retry_after}s")
|
|
116
|
+
except AuthenticationError as err:
|
|
117
|
+
if err.code == ErrorCode.INVALID_API_KEY:
|
|
118
|
+
print("API キーが無効または失効済みです")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### レート制限残回数と `X-Cache: stale` の監視
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from nozk import NozkClient, ResponseMeta
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def on_response(meta: ResponseMeta) -> None:
|
|
128
|
+
if meta.is_stale:
|
|
129
|
+
print(f"[stale] {meta.path}")
|
|
130
|
+
if meta.rate_limit is not None:
|
|
131
|
+
rl = meta.rate_limit
|
|
132
|
+
print(f"quota: {rl.remaining}/{rl.limit} (day: {rl.remaining_day}/{rl.limit_day})")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
with NozkClient(on_response=on_response) as client:
|
|
136
|
+
client.market()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## ドキュメント
|
|
140
|
+
|
|
141
|
+
- **API リファレンス**: [docs/reference.md](https://github.com/mikuteto-dev/nozk.py/blob/main/docs/reference.md) — 全メソッド・引数・レスポンスモデル・例外の詳細
|
|
142
|
+
- **AI エージェント向け利用ガイド**: [docs/agent-guide.md](https://github.com/mikuteto-dev/nozk.py/blob/main/docs/agent-guide.md) — エージェント利用時の制約・注意点・JSON 出力パターン
|
|
143
|
+
- **Nozk 公式 API ドキュメント**: https://developers.nozk.dev/
|
|
144
|
+
|
|
145
|
+
## 開発
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
uv sync
|
|
149
|
+
uv run ruff check .
|
|
150
|
+
uv run pyright
|
|
151
|
+
uv run pytest
|
|
152
|
+
uv run pytest -m contract # 本番 OpenAPI 仕様との整合テスト
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## ライセンス
|
|
156
|
+
|
|
157
|
+
MIT License
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Agent Usage Guide
|
|
2
|
+
|
|
3
|
+
Operational rules and pitfalls for AI agents using `nozk.py` (import name `nozk`).
|
|
4
|
+
For the complete API surface (methods, parameters, models, and exceptions), see [`docs/reference.md`](./reference.md).
|
|
5
|
+
|
|
6
|
+
## Core Contracts
|
|
7
|
+
|
|
8
|
+
- **Read-only GET API**: Nozk MC Public API supports `GET` only. Trading, transfers, and writes are not supported.
|
|
9
|
+
- **Package vs. Import Name**: Install `nozk.py`; import `nozk` (`from nozk import NozkClient, AsyncNozkClient`).
|
|
10
|
+
- **Authentication**: Passing no `api_key` reads `NOZK_API_KEY` from the environment. Empty or missing keys raise `ValueError` at initialization.
|
|
11
|
+
- **Stateless Execution (1 call = 1 HTTP request)**:
|
|
12
|
+
- No implicit retries and no client-side caching.
|
|
13
|
+
- Only `iter_trades()` and `me.iter_trades()` perform multiple requests (lazy pagination). Always bound iteration with `itertools.islice` or an explicit `break`.
|
|
14
|
+
|
|
15
|
+
## Rate Limits & Retry Decisions
|
|
16
|
+
|
|
17
|
+
- Free plan limits: **30 req/min** and **2,000 req/day** (daily reset at 00:00 JST). Server caches responses for 30s; polling 1–2 times per minute is sufficient.
|
|
18
|
+
- On HTTP 429, `RateLimitError` is raised immediately with `err.retry_after` (seconds).
|
|
19
|
+
- **Warning**: When the daily limit is exhausted, `err.retry_after` is the number of seconds until 00:00 JST (potentially hours). Never sleep blindly without capping the wait time.
|
|
20
|
+
- Prefer `client.market()` (1 request for all listed items) over calling `client.item()` in a loop.
|
|
21
|
+
|
|
22
|
+
## Common Pitfalls
|
|
23
|
+
|
|
24
|
+
1. **Reserved keyword `from`**: Use `from_` in `item_history(key, from_=..., to=...)`, `TreasuryPeriod.from_`, and `ParamValue.from_`.
|
|
25
|
+
2. **7-day history cap & candle width**: `item_history()` allows at most 7 days per request (earliest `2026-09-01T00:00:00Z`). The server picks candle width from the window length (`<=60m`: 1m, `<=6h`: 5m, `<=24h`: 15m, `<=7d`: 2h). If splitting ranges longer than 7 days, a shorter trailing chunk will have finer candle resolution.
|
|
26
|
+
3. **Arrears units**: `tax_arrears` and `rent_arrears` in `Property` are **days overdue**, not currency amounts.
|
|
27
|
+
4. **Timestamps vs. dates**: Datetime fields (`time`, `matures_at`, `reset`) are UTC `datetime`; date-only fields (`date`, `from_`, `to`, `updated`, `moved_in_on`) are JST `datetime.date`.
|
|
28
|
+
5. **Stale cache during outages**: Market endpoints may serve stale cached data (`X-Cache: stale`) when the upstream economy service is unavailable. When freshness matters, inspect `meta.is_stale` via `on_response`.
|
|
29
|
+
|
|
30
|
+
## Structured JSON & Error Branching
|
|
31
|
+
|
|
32
|
+
All response models are frozen Pydantic v2 models (`model.model_dump()`, `model.model_dump_json()`). Branch on `err.code` (`ErrorCode` StrEnum):
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from nozk import ErrorCode, NozkAPIError, NozkClient
|
|
36
|
+
|
|
37
|
+
with NozkClient() as client:
|
|
38
|
+
try:
|
|
39
|
+
wallet = client.me.wallet()
|
|
40
|
+
print(wallet.model_dump_json())
|
|
41
|
+
except NozkAPIError as err:
|
|
42
|
+
if err.code == ErrorCode.INSUFFICIENT_SCOPE:
|
|
43
|
+
print("API key is missing the `wallet` scope.")
|
|
44
|
+
elif err.code == ErrorCode.RATE_LIMITED:
|
|
45
|
+
print(f"Rate limited; retry_after={err.retry_after}s")
|
|
46
|
+
else:
|
|
47
|
+
raise
|
|
48
|
+
```
|