cs2sh 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.
- cs2sh-0.1.0/.gitignore +39 -0
- cs2sh-0.1.0/LICENSE +21 -0
- cs2sh-0.1.0/PKG-INFO +79 -0
- cs2sh-0.1.0/README.md +42 -0
- cs2sh-0.1.0/pyproject.toml +93 -0
- cs2sh-0.1.0/src/cs2sh/__init__.py +108 -0
- cs2sh-0.1.0/src/cs2sh/_async_engine.py +134 -0
- cs2sh-0.1.0/src/cs2sh/_async_namespaces.py +4153 -0
- cs2sh-0.1.0/src/cs2sh/_base.py +168 -0
- cs2sh-0.1.0/src/cs2sh/_chunk.py +140 -0
- cs2sh-0.1.0/src/cs2sh/_engine.py +142 -0
- cs2sh-0.1.0/src/cs2sh/_namespaces.py +4151 -0
- cs2sh-0.1.0/src/cs2sh/_operation.py +233 -0
- cs2sh-0.1.0/src/cs2sh/_spec.py +2205 -0
- cs2sh-0.1.0/src/cs2sh/_transport.py +464 -0
- cs2sh-0.1.0/src/cs2sh/_version.py +3 -0
- cs2sh-0.1.0/src/cs2sh/client.py +301 -0
- cs2sh-0.1.0/src/cs2sh/config.py +73 -0
- cs2sh-0.1.0/src/cs2sh/enums.py +211 -0
- cs2sh-0.1.0/src/cs2sh/errors.py +139 -0
- cs2sh-0.1.0/src/cs2sh/helpers.py +301 -0
- cs2sh-0.1.0/src/cs2sh/models.py +2527 -0
- cs2sh-0.1.0/src/cs2sh/orderbook.py +126 -0
- cs2sh-0.1.0/src/cs2sh/pandas.py +47 -0
- cs2sh-0.1.0/src/cs2sh/params.py +853 -0
- cs2sh-0.1.0/src/cs2sh/py.typed +0 -0
cs2sh-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Python bytecode
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Virtual environments
|
|
7
|
+
.venv/
|
|
8
|
+
venv/
|
|
9
|
+
env/
|
|
10
|
+
ENV/
|
|
11
|
+
|
|
12
|
+
# Build / packaging artifacts
|
|
13
|
+
build/
|
|
14
|
+
dist/
|
|
15
|
+
*.egg
|
|
16
|
+
*.egg-info/
|
|
17
|
+
.eggs/
|
|
18
|
+
wheels/
|
|
19
|
+
|
|
20
|
+
# Tooling caches
|
|
21
|
+
.mypy_cache/
|
|
22
|
+
.pytest_cache/
|
|
23
|
+
.ruff_cache/
|
|
24
|
+
.tox/
|
|
25
|
+
|
|
26
|
+
# Coverage
|
|
27
|
+
.coverage
|
|
28
|
+
.coverage.*
|
|
29
|
+
htmlcov/
|
|
30
|
+
coverage.xml
|
|
31
|
+
|
|
32
|
+
# Editor / OS noise
|
|
33
|
+
.idea/
|
|
34
|
+
.vscode/
|
|
35
|
+
.DS_Store
|
|
36
|
+
|
|
37
|
+
# CSV/parquet data written by example programs into the working directory.
|
|
38
|
+
*.csv
|
|
39
|
+
*.parquet
|
cs2sh-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 cs2.sh
|
|
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.
|
cs2sh-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: cs2sh
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python client for the cs2.sh CS2 skin-pricing data API (https://api.cs2.sh).
|
|
5
|
+
Project-URL: Homepage, https://cs2.sh
|
|
6
|
+
Project-URL: Documentation, https://cs2.sh/docs
|
|
7
|
+
Project-URL: Repository, https://github.com/open-mesa/cs2sh-sdks
|
|
8
|
+
Project-URL: Issues, https://github.com/open-mesa/cs2sh-sdks/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/open-mesa/cs2sh-sdks/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: API, https://api.cs2.sh
|
|
11
|
+
Author: cs2.sh
|
|
12
|
+
License-Expression: MIT
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Keywords: api-client,buff,c5game,counter-strike,cs2,csfloat,market-data,skinport,steam,youpin
|
|
15
|
+
Classifier: Development Status :: 4 - Beta
|
|
16
|
+
Classifier: Intended Audience :: Developers
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: httpx>=0.27
|
|
26
|
+
Requires-Dist: pydantic>=2
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
29
|
+
Requires-Dist: pandas>=1.5; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
32
|
+
Requires-Dist: respx>=0.21; extra == 'dev'
|
|
33
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
34
|
+
Provides-Extra: pandas
|
|
35
|
+
Requires-Dist: pandas>=1.5; extra == 'pandas'
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# cs2sh
|
|
39
|
+
|
|
40
|
+
Python package `cs2sh`, a client for the cs2.sh API at `https://api.cs2.sh`.
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install cs2sh
|
|
46
|
+
pip install "cs2sh[pandas]" # with the pandas helper
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quickstart
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from cs2sh import Client, best_ask
|
|
53
|
+
|
|
54
|
+
ITEM = "AK-47 | Redline (Field-Tested)"
|
|
55
|
+
|
|
56
|
+
with Client() as client: # api_key is read from CS2SH_API_KEY when omitted
|
|
57
|
+
resp = client.prices.latest([ITEM])
|
|
58
|
+
item = resp.items.get(ITEM)
|
|
59
|
+
if item is None:
|
|
60
|
+
print(f"no latest data for {ITEM!r}")
|
|
61
|
+
else:
|
|
62
|
+
print(item.market_hash_name)
|
|
63
|
+
print(f" csfloat ask = {best_ask(item, 'csfloat')}")
|
|
64
|
+
print(f" youpin ask = {best_ask(item, 'youpin')}")
|
|
65
|
+
print(f" buff ask = {best_ask(item, 'buff')}")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Links
|
|
69
|
+
|
|
70
|
+
- Documentation: https://cs2.sh/docs
|
|
71
|
+
- Full docs as text: https://cs2.sh/llms-full.txt
|
|
72
|
+
- OpenAPI spec: https://cs2.sh/openapi.yaml
|
|
73
|
+
- Changelog: https://github.com/open-mesa/cs2sh-sdks/blob/main/CHANGELOG.md
|
|
74
|
+
|
|
75
|
+
The live, sales and account routes are not yet in the public API docs.
|
|
76
|
+
|
|
77
|
+
## License
|
|
78
|
+
|
|
79
|
+
MIT. See `LICENSE`.
|
cs2sh-0.1.0/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# cs2sh
|
|
2
|
+
|
|
3
|
+
Python package `cs2sh`, a client for the cs2.sh API at `https://api.cs2.sh`.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install cs2sh
|
|
9
|
+
pip install "cs2sh[pandas]" # with the pandas helper
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Quickstart
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from cs2sh import Client, best_ask
|
|
16
|
+
|
|
17
|
+
ITEM = "AK-47 | Redline (Field-Tested)"
|
|
18
|
+
|
|
19
|
+
with Client() as client: # api_key is read from CS2SH_API_KEY when omitted
|
|
20
|
+
resp = client.prices.latest([ITEM])
|
|
21
|
+
item = resp.items.get(ITEM)
|
|
22
|
+
if item is None:
|
|
23
|
+
print(f"no latest data for {ITEM!r}")
|
|
24
|
+
else:
|
|
25
|
+
print(item.market_hash_name)
|
|
26
|
+
print(f" csfloat ask = {best_ask(item, 'csfloat')}")
|
|
27
|
+
print(f" youpin ask = {best_ask(item, 'youpin')}")
|
|
28
|
+
print(f" buff ask = {best_ask(item, 'buff')}")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Links
|
|
32
|
+
|
|
33
|
+
- Documentation: https://cs2.sh/docs
|
|
34
|
+
- Full docs as text: https://cs2.sh/llms-full.txt
|
|
35
|
+
- OpenAPI spec: https://cs2.sh/openapi.yaml
|
|
36
|
+
- Changelog: https://github.com/open-mesa/cs2sh-sdks/blob/main/CHANGELOG.md
|
|
37
|
+
|
|
38
|
+
The live, sales and account routes are not yet in the public API docs.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT. See `LICENSE`.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cs2sh"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official Python client for the cs2.sh CS2 skin-pricing data API (https://api.cs2.sh)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "cs2.sh" }]
|
|
14
|
+
keywords = [
|
|
15
|
+
"cs2",
|
|
16
|
+
"counter-strike",
|
|
17
|
+
"steam",
|
|
18
|
+
"csfloat",
|
|
19
|
+
"buff",
|
|
20
|
+
"youpin",
|
|
21
|
+
"skinport",
|
|
22
|
+
"c5game",
|
|
23
|
+
"market-data",
|
|
24
|
+
"api-client",
|
|
25
|
+
]
|
|
26
|
+
classifiers = [
|
|
27
|
+
"Development Status :: 4 - Beta",
|
|
28
|
+
"Intended Audience :: Developers",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.9",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Programming Language :: Python :: 3.13",
|
|
35
|
+
"Typing :: Typed",
|
|
36
|
+
]
|
|
37
|
+
dependencies = [
|
|
38
|
+
"httpx>=0.27",
|
|
39
|
+
"pydantic>=2",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = "https://cs2.sh"
|
|
44
|
+
Documentation = "https://cs2.sh/docs"
|
|
45
|
+
Repository = "https://github.com/open-mesa/cs2sh-sdks"
|
|
46
|
+
Issues = "https://github.com/open-mesa/cs2sh-sdks/issues"
|
|
47
|
+
Changelog = "https://github.com/open-mesa/cs2sh-sdks/blob/main/CHANGELOG.md"
|
|
48
|
+
API = "https://api.cs2.sh"
|
|
49
|
+
|
|
50
|
+
[project.optional-dependencies]
|
|
51
|
+
pandas = ["pandas>=1.5"]
|
|
52
|
+
dev = [
|
|
53
|
+
"pytest>=8",
|
|
54
|
+
"pytest-asyncio>=0.23",
|
|
55
|
+
"respx>=0.21",
|
|
56
|
+
"ruff>=0.5",
|
|
57
|
+
"mypy>=1.10",
|
|
58
|
+
"pandas>=1.5",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[tool.hatch.build.targets.wheel]
|
|
62
|
+
packages = ["src/cs2sh"]
|
|
63
|
+
|
|
64
|
+
[tool.hatch.build.targets.sdist]
|
|
65
|
+
include = ["src/cs2sh", "README.md", "LICENSE"]
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
line-length = 100
|
|
69
|
+
target-version = "py39"
|
|
70
|
+
src = ["src", "tests"]
|
|
71
|
+
|
|
72
|
+
[tool.ruff.lint]
|
|
73
|
+
select = ["E", "F", "I", "UP", "B", "W"]
|
|
74
|
+
# UP045 would rewrite the contract-mandated ``Optional[...]`` model fields to
|
|
75
|
+
# ``X | None``; we keep ``Optional`` deliberately (see models.py docstring), so
|
|
76
|
+
# silence only that one pyupgrade rule. B008 allows ``Field(...)`` defaults.
|
|
77
|
+
ignore = ["B008", "UP045"]
|
|
78
|
+
|
|
79
|
+
[tool.mypy]
|
|
80
|
+
# Runtime supports 3.9+ (``requires-python``); the type-checker analyses against
|
|
81
|
+
# 3.10 because the installed mypy no longer accepts a 3.9 target. The package
|
|
82
|
+
# stays 3.9-safe via ``from __future__ import annotations`` everywhere.
|
|
83
|
+
python_version = "3.10"
|
|
84
|
+
strict = true
|
|
85
|
+
warn_unused_ignores = true
|
|
86
|
+
disallow_untyped_defs = true
|
|
87
|
+
ignore_missing_imports = true
|
|
88
|
+
files = ["src/cs2sh"]
|
|
89
|
+
|
|
90
|
+
[tool.pytest.ini_options]
|
|
91
|
+
asyncio_mode = "auto"
|
|
92
|
+
testpaths = ["tests"]
|
|
93
|
+
addopts = "-q"
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"""cs2sh: official Python client for the cs2.sh CS2 skin-pricing data API.
|
|
2
|
+
|
|
3
|
+
The public API lives at ``https://api.cs2.sh``; get a key from the dashboard at
|
|
4
|
+
``https://cs2.sh`` (paid ``cs2_...``) or a Discord trial (``demo_...``).
|
|
5
|
+
|
|
6
|
+
Quickstart::
|
|
7
|
+
|
|
8
|
+
from cs2sh import Client
|
|
9
|
+
|
|
10
|
+
with Client(api_key="cs2_...") as client:
|
|
11
|
+
prices = client.prices.latest(["AK-47 | Redline (Field-Tested)"])
|
|
12
|
+
print(prices.items)
|
|
13
|
+
|
|
14
|
+
Every model (``cs2sh.models``), request TypedDict (``cs2sh.params``) and enum
|
|
15
|
+
alias (``cs2sh.enums``) is generated from the SDK spec and re-exported here.
|
|
16
|
+
The optional pandas helper lives in :mod:`cs2sh.pandas`
|
|
17
|
+
(``pip install 'cs2sh[pandas]'``) and is imported separately so the base
|
|
18
|
+
install carries no pandas dependency.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
from . import enums, models, params
|
|
24
|
+
from ._base import ResponseMeta
|
|
25
|
+
from ._spec import LiveFollowResponse
|
|
26
|
+
from ._version import __version__
|
|
27
|
+
from .client import AsyncClient, Client
|
|
28
|
+
from .config import DEFAULT_BASE_URL, ClientConfig
|
|
29
|
+
from .enums import * # noqa: F403
|
|
30
|
+
from .errors import (
|
|
31
|
+
ApiError,
|
|
32
|
+
AuthenticationError,
|
|
33
|
+
ConfigError,
|
|
34
|
+
NotFoundError,
|
|
35
|
+
PermissionDeniedError,
|
|
36
|
+
RateLimitError,
|
|
37
|
+
ServerError,
|
|
38
|
+
TransportError,
|
|
39
|
+
UsageError,
|
|
40
|
+
ValidationError,
|
|
41
|
+
)
|
|
42
|
+
from .helpers import (
|
|
43
|
+
PriceEntry,
|
|
44
|
+
best_ask,
|
|
45
|
+
best_ask_entry,
|
|
46
|
+
best_bid,
|
|
47
|
+
best_bid_entry,
|
|
48
|
+
get_variant,
|
|
49
|
+
rows_to_csv,
|
|
50
|
+
to_csv,
|
|
51
|
+
to_rows,
|
|
52
|
+
)
|
|
53
|
+
from .models import * # noqa: F403
|
|
54
|
+
from .orderbook import (
|
|
55
|
+
DepthWithin,
|
|
56
|
+
LadderFill,
|
|
57
|
+
cost_to_buy,
|
|
58
|
+
depth_within,
|
|
59
|
+
mid,
|
|
60
|
+
proceeds_to_sell,
|
|
61
|
+
spread,
|
|
62
|
+
spread_pct,
|
|
63
|
+
)
|
|
64
|
+
from .params import * # noqa: F403
|
|
65
|
+
|
|
66
|
+
__all__ = [
|
|
67
|
+
"__version__",
|
|
68
|
+
# Clients and configuration
|
|
69
|
+
"Client",
|
|
70
|
+
"AsyncClient",
|
|
71
|
+
"ClientConfig",
|
|
72
|
+
"DEFAULT_BASE_URL",
|
|
73
|
+
"ResponseMeta",
|
|
74
|
+
"LiveFollowResponse",
|
|
75
|
+
# Errors
|
|
76
|
+
"ApiError",
|
|
77
|
+
"ValidationError",
|
|
78
|
+
"AuthenticationError",
|
|
79
|
+
"PermissionDeniedError",
|
|
80
|
+
"NotFoundError",
|
|
81
|
+
"RateLimitError",
|
|
82
|
+
"ServerError",
|
|
83
|
+
"TransportError",
|
|
84
|
+
"UsageError",
|
|
85
|
+
"ConfigError",
|
|
86
|
+
# Helpers
|
|
87
|
+
"PriceEntry",
|
|
88
|
+
"best_ask",
|
|
89
|
+
"best_bid",
|
|
90
|
+
"best_ask_entry",
|
|
91
|
+
"best_bid_entry",
|
|
92
|
+
"get_variant",
|
|
93
|
+
"to_rows",
|
|
94
|
+
"to_csv",
|
|
95
|
+
"rows_to_csv",
|
|
96
|
+
# Orderbook math
|
|
97
|
+
"DepthWithin",
|
|
98
|
+
"LadderFill",
|
|
99
|
+
"mid",
|
|
100
|
+
"spread",
|
|
101
|
+
"spread_pct",
|
|
102
|
+
"depth_within",
|
|
103
|
+
"cost_to_buy",
|
|
104
|
+
"proceeds_to_sell",
|
|
105
|
+
]
|
|
106
|
+
__all__ += enums.__all__
|
|
107
|
+
__all__ += models.__all__
|
|
108
|
+
__all__ += params.__all__
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# GENERATED FILE. DO NOT EDIT.
|
|
2
|
+
# Source: src/cs2sh/_engine.py
|
|
3
|
+
# Regenerate: uv run python tools/gen_async.py
|
|
4
|
+
#
|
|
5
|
+
# Async twin of the runtime in _engine.py, for AsyncClient.
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import AsyncIterator, Mapping
|
|
9
|
+
from typing import TYPE_CHECKING, Any, Optional, TypeVar
|
|
10
|
+
|
|
11
|
+
from ._base import ResponseMeta, _Response, parse_response
|
|
12
|
+
from ._chunk import folded_item_errors, iter_chunks, merge_chunks, normalize_items
|
|
13
|
+
from ._operation import (
|
|
14
|
+
Chunking,
|
|
15
|
+
Operation,
|
|
16
|
+
build_payload,
|
|
17
|
+
encode_query,
|
|
18
|
+
follow_target,
|
|
19
|
+
next_cursor,
|
|
20
|
+
page_rows,
|
|
21
|
+
)
|
|
22
|
+
from ._transport import RawResponse
|
|
23
|
+
from .errors import ApiError, TransportError, UsageError
|
|
24
|
+
|
|
25
|
+
if TYPE_CHECKING:
|
|
26
|
+
from .client import AsyncClient
|
|
27
|
+
|
|
28
|
+
_R = TypeVar("_R", bound=_Response)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class _AsyncNamespace:
|
|
32
|
+
"""Base of every generated namespace: executes ``Operation`` values."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, client: AsyncClient) -> None:
|
|
35
|
+
self._client = client
|
|
36
|
+
|
|
37
|
+
async def _send(self, op: Operation[Any], payload: dict[str, Any]) -> RawResponse:
|
|
38
|
+
"""One request (with the transport's retries) for an already-built payload."""
|
|
39
|
+
if op.params_in == "query":
|
|
40
|
+
query = encode_query(op, payload)
|
|
41
|
+
return await self._client._request(op.method, op.path, params=query, auth=op.auth)
|
|
42
|
+
if op.method == "GET":
|
|
43
|
+
return await self._client._request(op.method, op.path, auth=op.auth)
|
|
44
|
+
return await self._client._request(op.method, op.path, json_body=payload, auth=op.auth)
|
|
45
|
+
|
|
46
|
+
async def _call(self, op: Operation[_R], params: Mapping[str, Any]) -> _R:
|
|
47
|
+
"""One call: chunked when the operation chunks, else one request."""
|
|
48
|
+
if op.chunking is not None:
|
|
49
|
+
return await self._chunked(op, op.chunking, params)
|
|
50
|
+
raw = await self._send(op, build_payload(op, params))
|
|
51
|
+
return parse_response(op.response, raw.body, raw.meta)
|
|
52
|
+
|
|
53
|
+
async def _chunked(
|
|
54
|
+
self, op: Operation[_R], chunking: Chunking, params: Mapping[str, Any]
|
|
55
|
+
) -> _R:
|
|
56
|
+
"""Normalize the items, POST them in chunks one after another, fold, merge.
|
|
57
|
+
|
|
58
|
+
The merged response and any ``err.partial`` carry the meta of the last
|
|
59
|
+
chunk that returned 2xx. When no chunk succeeds and one folded, the
|
|
60
|
+
first folded chunk's error is raised with no partial.
|
|
61
|
+
"""
|
|
62
|
+
items = normalize_items(params.get(chunking.items_field))
|
|
63
|
+
rest = {key: value for key, value in params.items() if key != chunking.items_field}
|
|
64
|
+
base = build_payload(op, rest)
|
|
65
|
+
parts: list[dict[str, Any]] = []
|
|
66
|
+
errors: list[Any] = []
|
|
67
|
+
meta = ResponseMeta()
|
|
68
|
+
first_folded: Optional[ApiError] = None
|
|
69
|
+
for chunk in iter_chunks(items, chunking.size):
|
|
70
|
+
try:
|
|
71
|
+
raw = await self._send(op, {**base, chunking.items_field: chunk})
|
|
72
|
+
# A 2xx body that does not fit the model is this chunk's
|
|
73
|
+
# failure (R12): later chunks are not sent and the earlier
|
|
74
|
+
# ones survive as the partial.
|
|
75
|
+
parse_response(op.response, raw.body, raw.meta)
|
|
76
|
+
except ApiError as err:
|
|
77
|
+
folded = folded_item_errors(err, chunk, chunking)
|
|
78
|
+
if folded is not None:
|
|
79
|
+
if first_folded is None:
|
|
80
|
+
first_folded = err
|
|
81
|
+
errors.extend(folded)
|
|
82
|
+
continue
|
|
83
|
+
if parts:
|
|
84
|
+
try:
|
|
85
|
+
merged = merge_chunks(parts, errors, chunking)
|
|
86
|
+
err.partial = parse_response(op.response, merged, meta)
|
|
87
|
+
except TransportError:
|
|
88
|
+
err.partial = None
|
|
89
|
+
raise
|
|
90
|
+
parts.append(raw.body)
|
|
91
|
+
meta = raw.meta
|
|
92
|
+
chunk_errors = raw.body.get("errors")
|
|
93
|
+
if isinstance(chunk_errors, list):
|
|
94
|
+
errors.extend(chunk_errors)
|
|
95
|
+
if not parts:
|
|
96
|
+
assert first_folded is not None # items is non-empty, so a chunk ran and folded
|
|
97
|
+
raise first_folded
|
|
98
|
+
return parse_response(op.response, merge_chunks(parts, errors, chunking), meta)
|
|
99
|
+
|
|
100
|
+
async def _pages(self, op: Operation[_R], params: Mapping[str, Any]) -> AsyncIterator[_R]:
|
|
101
|
+
"""The first page, then each ``next_cursor`` sent alone, lazily, with no sleep."""
|
|
102
|
+
assert op.pagination is not None
|
|
103
|
+
page = await self._call(op, params)
|
|
104
|
+
while True:
|
|
105
|
+
yield page
|
|
106
|
+
cursor = next_cursor(op, page)
|
|
107
|
+
if cursor is None:
|
|
108
|
+
return
|
|
109
|
+
page = await self._call(op, {op.pagination.cursor_field: cursor})
|
|
110
|
+
|
|
111
|
+
async def _iter(self, op: Operation[Any], params: Mapping[str, Any]) -> AsyncIterator[Any]:
|
|
112
|
+
"""The rows at the operation's ``rows_path`` of every page, lazily."""
|
|
113
|
+
async for page in self._pages(op, params):
|
|
114
|
+
for row in page_rows(op, page): # noqa: UP028 - async generators cannot `yield from`
|
|
115
|
+
yield row
|
|
116
|
+
|
|
117
|
+
async def _follow(self, table: Mapping[str, Operation[Any]], query: Any) -> Any:
|
|
118
|
+
"""``live.follow``: send a ready-made ``{path, body}`` to a live path.
|
|
119
|
+
|
|
120
|
+
On a data path the body is sent as given plus the operation's body
|
|
121
|
+
constants, or as ``{cursor}`` alone when it holds ``cursor`` (R25); on
|
|
122
|
+
an ``/options`` path it is sent unchanged (R27). A path not in ``table``
|
|
123
|
+
raises ``UsageError`` before any request.
|
|
124
|
+
"""
|
|
125
|
+
path, body = follow_target(query)
|
|
126
|
+
op = table.get(path)
|
|
127
|
+
if op is None:
|
|
128
|
+
raise UsageError(f"live.follow: {path!r} is not a live data or options path")
|
|
129
|
+
if path.endswith("/options"):
|
|
130
|
+
payload = dict(body)
|
|
131
|
+
else:
|
|
132
|
+
payload = build_payload(op, body, drop_unset=False)
|
|
133
|
+
raw = await self._send(op, payload)
|
|
134
|
+
return parse_response(op.response, raw.body, raw.meta)
|