Metadata-Version: 2.4
Name: cardspine
Version: 0.1.0
Summary: Typed client for the Cardspine trading card catalog API.
Project-URL: Homepage, https://cardspine.com/docs
Project-URL: Documentation, https://cardspine.com/docs
Author: The Clever Fox LLC
License-Expression: MIT
License-File: LICENSE
Keywords: api,cardspine,sdk,tcg,tcgplayer,trading cards
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Typing :: Typed
Requires-Python: >=3.9
Requires-Dist: httpx<1,>=0.27
Description-Content-Type: text/markdown

# cardspine

Typed client for the [Cardspine](https://cardspine.com/docs) trading card catalog API. Python 3.9 or later; its one dependency is `httpx`.

```sh
pip install cardspine
```

## Quickstart

```python
from cardspine import Cardspine

with Cardspine() as cardspine:  # reads CARDSPINE_API_KEY, or pass the key
    results = cardspine.search(game="pokemon", q="charizard 4/102")
    product = cardspine.get_product(results["items"][0]["tcgplayerProductId"])
    print(product["name"], cardspine.rate_limit)
```

Responses are plain dicts typed with `TypedDict` models generated from the OpenAPI document (`cardspine.models`). The key is sent in the `X-Api-Key` header only. It never appears in URLs, error messages or `repr`, and redirects are never followed with it, even on an `http_client` you pass in.

## Calls

| Method | Endpoint |
|---|---|
| `me()` | `GET /api/v1/me` |
| `list_games()`, `get_game(game)` | `GET /api/v1/games`, `GET /api/v1/games/{game}` |
| `list_sets(game, sort=, release=)` | `GET /api/v1/games/{game}/sets` |
| `list_set_aliases(game, source=)` | `GET /api/v1/games/{game}/set-aliases` |
| `get_product(id)` | `GET /api/v1/products/tcgplayer/{productId}` |
| `get_products(ids)` | `GET /api/v1/products/tcgplayer?ids=` (up to 100) |
| `get_all_products(ids)` | the same, 100 ids per call, merged |
| `replace_contents(id, edit)` | `PUT /api/v1/products/tcgplayer/{productId}/contents` |
| `get_raw_prices(id, condition=, ...)` | `GET /api/v1/products/tcgplayer/{productId}/prices/raw` |
| `get_raw_prices_batch(ids, ...)` | `POST /api/v1/prices/raw` (up to 100) |
| `get_all_raw_prices(ids, ...)` | the same, 100 ids per call, merged |
| `search(...)`, `search_pages(...)` | `GET /api/v1/search` |
| `record_selection(product_id=)` | `POST /api/v1/search/selections` |
| `get_changes(after=, limit=)`, `change_pages(after=)` | `GET /api/v1/changes` |
| `download_catalog(if_none_match=)` | `GET /api/v1/catalog/download` |

## Pages, batches and the catalog

```python
for page in cardspine.search_pages(game="pokemon", sets=[604], sort="number", page_size=100):
    print(len(page["items"]))

result = cardspine.get_all_products(ids)
if result.stopped_by:
    print(f"Rate limited; retry {len(result.pending)} ids after {result.stopped_by.retry_after} s")

download = cardspine.download_catalog(if_none_match=saved_etag)
if download:
    with download:
        for product in download.products():
            save(product)
    for page in cardspine.change_pages(after=download.change_cursor):
        apply(page["changes"])
        save_cursor(page["cursor"])
```

`search_pages` follows `nextCursor` where the API sets one, and otherwise walks page numbers up to the 1,000-result window. The catalog is streamed and decompressed as it arrives, never held in memory whole. The timeout covers a download only until its headers arrive, since the file can take minutes.

## Errors, retries and limits

Error answers raise `CardspineError` with `status`, `title`, `detail`, `type`, `code`, `retry_after` and `rate_limit`, parsed from the problem details body. Subclasses: `CardspineAuthError` (401, 403), `CardspineNotFoundError` (404), `CardspineGoneError` (410: the change cursor is too old, so download the catalog again and resume from its `change_cursor`), `CardspineRateLimitError` (429) and `CardspineConnectionError` (no answer).

Reads are retried up to 3 times: on 429 and 503 after the `Retry-After` interval, and on other 5xx and network failures with exponential backoff and jitter. A `Retry-After` over 60 seconds, such as a spent monthly allowance, is not waited out; the call raises at once. `record_selection` and `replace_contents` are never retried. `cardspine.rate_limit` holds the `X-RateLimit-*` and `X-Quota-*` values of the latest response.

```python
Cardspine(
    "your key",
    base_url="https://api.cardspine.com",
    api_path="/api/v1",
    max_retries=3,
    timeout=30.0,
    max_retry_after=60.0,
)
```

## License

MIT
