cs2sh 0.1.0__py3-none-any.whl
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/__init__.py +108 -0
- cs2sh/_async_engine.py +134 -0
- cs2sh/_async_namespaces.py +4153 -0
- cs2sh/_base.py +168 -0
- cs2sh/_chunk.py +140 -0
- cs2sh/_engine.py +142 -0
- cs2sh/_namespaces.py +4151 -0
- cs2sh/_operation.py +233 -0
- cs2sh/_spec.py +2205 -0
- cs2sh/_transport.py +464 -0
- cs2sh/_version.py +3 -0
- cs2sh/client.py +301 -0
- cs2sh/config.py +73 -0
- cs2sh/enums.py +211 -0
- cs2sh/errors.py +139 -0
- cs2sh/helpers.py +301 -0
- cs2sh/models.py +2527 -0
- cs2sh/orderbook.py +126 -0
- cs2sh/pandas.py +47 -0
- cs2sh/params.py +853 -0
- cs2sh/py.typed +0 -0
- cs2sh-0.1.0.dist-info/METADATA +79 -0
- cs2sh-0.1.0.dist-info/RECORD +25 -0
- cs2sh-0.1.0.dist-info/WHEEL +4 -0
- cs2sh-0.1.0.dist-info/licenses/LICENSE +21 -0
cs2sh/__init__.py
ADDED
|
@@ -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__
|
cs2sh/_async_engine.py
ADDED
|
@@ -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)
|