capitalcom-cli 0.5.0__tar.gz → 0.6.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.
- {capitalcom_cli-0.5.0/capitalcom_cli.egg-info → capitalcom_cli-0.6.0}/PKG-INFO +23 -14
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/README.md +22 -13
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/__init__.py +1 -1
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/session_cmds.py +3 -10
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/risk.py +4 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/session.py +17 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/sdk/__init__.py +1 -1
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/sdk/app.py +3 -3
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/sdk/config.py +1 -1
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0/capitalcom_cli.egg-info}/PKG-INFO +23 -14
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capitalcom_cli.egg-info/SOURCES.txt +2 -0
- capitalcom_cli-0.6.0/tests/test_coverage_doc_in_sync.py +35 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_risk.py +48 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_session_cmds.py +15 -21
- capitalcom_cli-0.6.0/tests/test_session_reads.py +83 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/LICENSE +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/NOTICE +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/__main__.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/__init__.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/account_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/app.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/context.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/doctor_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/live_guard.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/market_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/output.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/runner.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/stream_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/trade_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/cli/watchlist_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/__init__.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/audit.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/config.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/errors.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/http_client.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/models.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/rate_limit.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/state.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/utils.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/core/websocket_client.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/sdk/risk_policy.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/__init__.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/accounts.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/confirmations.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/markets.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/streaming.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/trading.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capital_cli/services/watchlists.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capitalcom_cli.egg-info/dependency_links.txt +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capitalcom_cli.egg-info/entry_points.txt +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capitalcom_cli.egg-info/requires.txt +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/capitalcom_cli.egg-info/top_level.txt +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/pyproject.toml +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/setup.cfg +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_account_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_app.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_audit.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_build_broker_request.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_cli_usage_errors.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_config_errors.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_core_port.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_credential_cmd.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_doctor.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_errors.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_global_flag_hoist.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_http_client.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_init_state_singletons.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_live_guard.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_market_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_output.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_output_color.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_output_plain.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_redaction.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_runner.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_session_persistence.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_state.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_stream_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_trade_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_trade_limit.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_tz_datetimes.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_utils.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_version.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_watchlist_cmds.py +0 -0
- {capitalcom_cli-0.5.0 → capitalcom_cli-0.6.0}/tests/test_websocket_parsing.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: capitalcom-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Command-line client for the Capital.com Open API
|
|
5
5
|
Author: Simon Tarara
|
|
6
6
|
License: Apache-2.0
|
|
@@ -38,6 +38,7 @@ Browse markets, manage accounts and watchlists, preview and execute trades behin
|
|
|
38
38
|
[](LICENSE)
|
|
39
39
|
[](https://www.python.org/downloads/)
|
|
40
40
|
[](https://github.com/SimonTarara62/capitalcom-cli/releases)
|
|
41
|
+
[](docs/api-coverage.md)
|
|
41
42
|
|
|
42
43
|
```text
|
|
43
44
|
$ capctl market search "gold"
|
|
@@ -67,22 +68,26 @@ The Capital.com web platform is built for clicking; `capctl` is built for **repe
|
|
|
67
68
|
See [practical use cases](docs/use-cases.md) for worked, copy-pasteable scenarios.
|
|
68
69
|
|
|
69
70
|
**Scope:** `capctl` is first and foremost a command-line application, but the same
|
|
70
|
-
tested broker engine is also embeddable as
|
|
71
|
-
[Use as a library](#use-as-a-library
|
|
71
|
+
tested broker engine is also embeddable as a Python SDK — see
|
|
72
|
+
[Use as a library](#use-as-a-library) and [docs/sdk.md](docs/sdk.md).
|
|
72
73
|
The `capital_cli.core.*` internals remain private and may change between releases.
|
|
73
|
-
The CLI itself is short-lived: each command runs as its own process
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
The CLI itself is short-lived: each command runs as its own process. By default
|
|
75
|
+
(`CAP_PERSIST_SESSION=true`) the short-lived session tokens are cached so
|
|
76
|
+
back-to-back commands reuse one login instead of re-authenticating each time
|
|
77
|
+
(which can trip Capital.com's login-rate limit and return HTTP 429); `capctl
|
|
78
|
+
session login` is mainly a connectivity/account check.
|
|
76
79
|
|
|
77
80
|
## Features
|
|
78
81
|
|
|
79
|
-
- **Six command groups** covering the
|
|
82
|
+
- **Six command groups** covering the **full** Capital.com Open API surface — `session`, `market`, `account`, `trade`, `watchlist`, `stream` — verified by an automated [coverage matrix](docs/api-coverage.md)
|
|
80
83
|
- **Safety-first trading** — trading is off by default; enabling it requires an explicit EPIC allowlist, every execution goes through a two-phase *preview → execute* flow with risk checks, and mutating commands require `--yes`
|
|
81
84
|
- **`--json` everywhere** — every command can emit raw JSON for piping into `jq`, scripts, or CI jobs
|
|
82
85
|
- **Distinct exit codes per failure class** — scripts can branch on *why* a command failed (auth vs. risk-block vs. upstream error)
|
|
83
86
|
- **Real-time streaming** — live price tables, price-level alerts, and portfolio snapshots over WebSocket
|
|
84
87
|
- **Demo and live environments** — defaults to demo; live requires explicit opt-in
|
|
85
88
|
- **Built-in rate limiting** — client-side token buckets respect Capital.com's 10 req/s global, 1 req/s session, and trading-burst limits
|
|
89
|
+
- **Session-token caching** — back-to-back commands reuse one login instead of re-authenticating (avoids HTTP 429); on by default, opt out with `CAP_PERSIST_SESSION=false`
|
|
90
|
+
- **Embeddable SDK** — the same broker engine ships as an async Python library with a stable 0.x surface; see [Use as a library](#use-as-a-library)
|
|
86
91
|
|
|
87
92
|
## Installation
|
|
88
93
|
|
|
@@ -173,6 +178,7 @@ Main settings and their defaults:
|
|
|
173
178
|
| `CAP_REQUIRE_EXPLICIT_CONFIRM` | `true` | Mutations need `--yes` |
|
|
174
179
|
| `CAP_DRY_RUN` | `false` | Block all executions regardless of other flags |
|
|
175
180
|
| `CAP_DEFAULT_ACCOUNT_ID` | (none) | Account selected after login |
|
|
181
|
+
| `CAP_PERSIST_SESSION` | `true` | Cache short-lived session tokens in the state file so back-to-back commands reuse one login (avoids HTTP 429); set `false` to keep tokens in-process only |
|
|
176
182
|
| `CAP_HTTP_TIMEOUT_S` | `15` | HTTP timeout |
|
|
177
183
|
| `CAP_LOG_LEVEL` | `WARNING` | `DEBUG` … `CRITICAL` |
|
|
178
184
|
| `CAP_WS_ENABLED` | `false` | Required for `capctl stream …` |
|
|
@@ -272,9 +278,9 @@ capctl account topup 1000 --yes # demo environment only
|
|
|
272
278
|
Read-only:
|
|
273
279
|
|
|
274
280
|
```bash
|
|
275
|
-
capctl trade positions
|
|
281
|
+
capctl trade positions [--limit 10] # -n/--limit caps rows shown
|
|
276
282
|
capctl trade position DEAL_ID
|
|
277
|
-
capctl trade orders
|
|
283
|
+
capctl trade orders [--limit 10] # -n/--limit caps rows shown
|
|
278
284
|
capctl trade confirm DEAL_REFERENCE [--wait --timeout 30]
|
|
279
285
|
```
|
|
280
286
|
|
|
@@ -390,7 +396,7 @@ capctl stream alerts BTCUSD 75000 --direction ABOVE --duration 3600
|
|
|
390
396
|
*/15 * * * * capctl --json trade positions >> ~/logs/positions.jsonl 2>&1
|
|
391
397
|
```
|
|
392
398
|
|
|
393
|
-
## Use as a library
|
|
399
|
+
## Use as a library
|
|
394
400
|
|
|
395
401
|
Beyond the CLI, the same broker engine — risk policy, two-phase preview→execute,
|
|
396
402
|
rate limiting, WebSocket streaming — is available as an embeddable async SDK.
|
|
@@ -413,9 +419,12 @@ async def main():
|
|
|
413
419
|
asyncio.run(main())
|
|
414
420
|
```
|
|
415
421
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
422
|
+
**Stability:** the documented SDK surface — `CapitalComApp`, `CapitalComConfig`,
|
|
423
|
+
`RiskPolicy`, the service classes, and the `capital_cli.core.models` pydantic
|
|
424
|
+
models — is **stable**: no breaking changes within the 0.x series without a
|
|
425
|
+
deprecation cycle; breaking changes are reserved for a future 1.0. The
|
|
426
|
+
`capital_cli.core.*` internals remain private. See [docs/sdk.md](docs/sdk.md) for
|
|
427
|
+
read-only, preview→execute, streaming, and risk-policy examples.
|
|
419
428
|
|
|
420
429
|
## Exit codes
|
|
421
430
|
|
|
@@ -482,7 +491,7 @@ Or use the task runner: `make check` (lint + typecheck + test), `make docs`, `ma
|
|
|
482
491
|
## Documentation
|
|
483
492
|
|
|
484
493
|
- [Full CLI reference](docs/CLI.md) — every command and option (auto-generated)
|
|
485
|
-
- [Using capctl as a Python SDK](docs/sdk.md) —
|
|
494
|
+
- [Using capctl as a Python SDK](docs/sdk.md) — stable async SDK, import paths, examples, versioning
|
|
486
495
|
- [Scripting & automation](docs/scripting.md) — CI credentials, exit codes, jq recipes
|
|
487
496
|
- [Getting started from zero](docs/getting-started.md) — account, API key, install, first trade
|
|
488
497
|
- [Practical use cases](docs/use-cases.md) — what people actually do with capctl, with copy-pasteable scenarios
|
|
@@ -9,6 +9,7 @@ Browse markets, manage accounts and watchlists, preview and execute trades behin
|
|
|
9
9
|
[](LICENSE)
|
|
10
10
|
[](https://www.python.org/downloads/)
|
|
11
11
|
[](https://github.com/SimonTarara62/capitalcom-cli/releases)
|
|
12
|
+
[](docs/api-coverage.md)
|
|
12
13
|
|
|
13
14
|
```text
|
|
14
15
|
$ capctl market search "gold"
|
|
@@ -38,22 +39,26 @@ The Capital.com web platform is built for clicking; `capctl` is built for **repe
|
|
|
38
39
|
See [practical use cases](docs/use-cases.md) for worked, copy-pasteable scenarios.
|
|
39
40
|
|
|
40
41
|
**Scope:** `capctl` is first and foremost a command-line application, but the same
|
|
41
|
-
tested broker engine is also embeddable as
|
|
42
|
-
[Use as a library](#use-as-a-library
|
|
42
|
+
tested broker engine is also embeddable as a Python SDK — see
|
|
43
|
+
[Use as a library](#use-as-a-library) and [docs/sdk.md](docs/sdk.md).
|
|
43
44
|
The `capital_cli.core.*` internals remain private and may change between releases.
|
|
44
|
-
The CLI itself is short-lived: each command runs as its own process
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
The CLI itself is short-lived: each command runs as its own process. By default
|
|
46
|
+
(`CAP_PERSIST_SESSION=true`) the short-lived session tokens are cached so
|
|
47
|
+
back-to-back commands reuse one login instead of re-authenticating each time
|
|
48
|
+
(which can trip Capital.com's login-rate limit and return HTTP 429); `capctl
|
|
49
|
+
session login` is mainly a connectivity/account check.
|
|
47
50
|
|
|
48
51
|
## Features
|
|
49
52
|
|
|
50
|
-
- **Six command groups** covering the
|
|
53
|
+
- **Six command groups** covering the **full** Capital.com Open API surface — `session`, `market`, `account`, `trade`, `watchlist`, `stream` — verified by an automated [coverage matrix](docs/api-coverage.md)
|
|
51
54
|
- **Safety-first trading** — trading is off by default; enabling it requires an explicit EPIC allowlist, every execution goes through a two-phase *preview → execute* flow with risk checks, and mutating commands require `--yes`
|
|
52
55
|
- **`--json` everywhere** — every command can emit raw JSON for piping into `jq`, scripts, or CI jobs
|
|
53
56
|
- **Distinct exit codes per failure class** — scripts can branch on *why* a command failed (auth vs. risk-block vs. upstream error)
|
|
54
57
|
- **Real-time streaming** — live price tables, price-level alerts, and portfolio snapshots over WebSocket
|
|
55
58
|
- **Demo and live environments** — defaults to demo; live requires explicit opt-in
|
|
56
59
|
- **Built-in rate limiting** — client-side token buckets respect Capital.com's 10 req/s global, 1 req/s session, and trading-burst limits
|
|
60
|
+
- **Session-token caching** — back-to-back commands reuse one login instead of re-authenticating (avoids HTTP 429); on by default, opt out with `CAP_PERSIST_SESSION=false`
|
|
61
|
+
- **Embeddable SDK** — the same broker engine ships as an async Python library with a stable 0.x surface; see [Use as a library](#use-as-a-library)
|
|
57
62
|
|
|
58
63
|
## Installation
|
|
59
64
|
|
|
@@ -144,6 +149,7 @@ Main settings and their defaults:
|
|
|
144
149
|
| `CAP_REQUIRE_EXPLICIT_CONFIRM` | `true` | Mutations need `--yes` |
|
|
145
150
|
| `CAP_DRY_RUN` | `false` | Block all executions regardless of other flags |
|
|
146
151
|
| `CAP_DEFAULT_ACCOUNT_ID` | (none) | Account selected after login |
|
|
152
|
+
| `CAP_PERSIST_SESSION` | `true` | Cache short-lived session tokens in the state file so back-to-back commands reuse one login (avoids HTTP 429); set `false` to keep tokens in-process only |
|
|
147
153
|
| `CAP_HTTP_TIMEOUT_S` | `15` | HTTP timeout |
|
|
148
154
|
| `CAP_LOG_LEVEL` | `WARNING` | `DEBUG` … `CRITICAL` |
|
|
149
155
|
| `CAP_WS_ENABLED` | `false` | Required for `capctl stream …` |
|
|
@@ -243,9 +249,9 @@ capctl account topup 1000 --yes # demo environment only
|
|
|
243
249
|
Read-only:
|
|
244
250
|
|
|
245
251
|
```bash
|
|
246
|
-
capctl trade positions
|
|
252
|
+
capctl trade positions [--limit 10] # -n/--limit caps rows shown
|
|
247
253
|
capctl trade position DEAL_ID
|
|
248
|
-
capctl trade orders
|
|
254
|
+
capctl trade orders [--limit 10] # -n/--limit caps rows shown
|
|
249
255
|
capctl trade confirm DEAL_REFERENCE [--wait --timeout 30]
|
|
250
256
|
```
|
|
251
257
|
|
|
@@ -361,7 +367,7 @@ capctl stream alerts BTCUSD 75000 --direction ABOVE --duration 3600
|
|
|
361
367
|
*/15 * * * * capctl --json trade positions >> ~/logs/positions.jsonl 2>&1
|
|
362
368
|
```
|
|
363
369
|
|
|
364
|
-
## Use as a library
|
|
370
|
+
## Use as a library
|
|
365
371
|
|
|
366
372
|
Beyond the CLI, the same broker engine — risk policy, two-phase preview→execute,
|
|
367
373
|
rate limiting, WebSocket streaming — is available as an embeddable async SDK.
|
|
@@ -384,9 +390,12 @@ async def main():
|
|
|
384
390
|
asyncio.run(main())
|
|
385
391
|
```
|
|
386
392
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
393
|
+
**Stability:** the documented SDK surface — `CapitalComApp`, `CapitalComConfig`,
|
|
394
|
+
`RiskPolicy`, the service classes, and the `capital_cli.core.models` pydantic
|
|
395
|
+
models — is **stable**: no breaking changes within the 0.x series without a
|
|
396
|
+
deprecation cycle; breaking changes are reserved for a future 1.0. The
|
|
397
|
+
`capital_cli.core.*` internals remain private. See [docs/sdk.md](docs/sdk.md) for
|
|
398
|
+
read-only, preview→execute, streaming, and risk-policy examples.
|
|
390
399
|
|
|
391
400
|
## Exit codes
|
|
392
401
|
|
|
@@ -453,7 +462,7 @@ Or use the task runner: `make check` (lint + typecheck + test), `make docs`, `ma
|
|
|
453
462
|
## Documentation
|
|
454
463
|
|
|
455
464
|
- [Full CLI reference](docs/CLI.md) — every command and option (auto-generated)
|
|
456
|
-
- [Using capctl as a Python SDK](docs/sdk.md) —
|
|
465
|
+
- [Using capctl as a Python SDK](docs/sdk.md) — stable async SDK, import paths, examples, versioning
|
|
457
466
|
- [Scripting & automation](docs/scripting.md) — CI credentials, exit codes, jq recipes
|
|
458
467
|
- [Getting started from zero](docs/getting-started.md) — account, API key, install, first trade
|
|
459
468
|
- [Practical use cases](docs/use-cases.md) — what people actually do with capctl, with copy-pasteable scenarios
|
|
@@ -7,7 +7,6 @@ from typing import Any
|
|
|
7
7
|
import typer
|
|
8
8
|
|
|
9
9
|
from capital_cli.cli.runner import run
|
|
10
|
-
from capital_cli.core.http_client import get_client
|
|
11
10
|
from capital_cli.core.session import get_session_manager
|
|
12
11
|
|
|
13
12
|
app = typer.Typer(no_args_is_help=True, help="Session lifecycle: login, ping, logout.")
|
|
@@ -98,9 +97,7 @@ def time(ctx: typer.Context) -> None:
|
|
|
98
97
|
out = ctx.obj.out
|
|
99
98
|
|
|
100
99
|
async def _do() -> dict[str, Any]:
|
|
101
|
-
|
|
102
|
-
data = (await client.get("/time")).json()
|
|
103
|
-
return data if isinstance(data, dict) else {"serverTime": data}
|
|
100
|
+
return await get_session_manager().server_time()
|
|
104
101
|
|
|
105
102
|
out.record(run(out, _do, label="session time"), title="Server time")
|
|
106
103
|
|
|
@@ -111,10 +108,7 @@ def details(ctx: typer.Context) -> None:
|
|
|
111
108
|
out = ctx.obj.out
|
|
112
109
|
|
|
113
110
|
async def _do() -> dict[str, Any]:
|
|
114
|
-
|
|
115
|
-
client = get_client()
|
|
116
|
-
await sm.ensure_logged_in()
|
|
117
|
-
return (await client.get("/session")).json()
|
|
111
|
+
return await get_session_manager().details()
|
|
118
112
|
|
|
119
113
|
out.record(run(out, _do, label="session details"), title="Session details")
|
|
120
114
|
|
|
@@ -125,7 +119,6 @@ def encryption_key(ctx: typer.Context) -> None:
|
|
|
125
119
|
out = ctx.obj.out
|
|
126
120
|
|
|
127
121
|
async def _do() -> dict[str, Any]:
|
|
128
|
-
|
|
129
|
-
return (await client.get("/session/encryptionKey")).json()
|
|
122
|
+
return await get_session_manager().encryption_key()
|
|
130
123
|
|
|
131
124
|
out.record(run(out, _do, label="session encryption-key"), title="Encryption key")
|
|
@@ -377,6 +377,10 @@ class RiskEngine:
|
|
|
377
377
|
result.normalized_request["level"] = request.level
|
|
378
378
|
if request.good_till_date:
|
|
379
379
|
result.normalized_request["good_till_date"] = request.good_till_date
|
|
380
|
+
# preview_position already persisted this preview WITHOUT the working
|
|
381
|
+
# order fields; re-save so the CLI's separate execute-order process
|
|
382
|
+
# (which loads from the state file) sees type/level. (#working-order-exec)
|
|
383
|
+
self.state.save_preview(result)
|
|
380
384
|
|
|
381
385
|
return result
|
|
382
386
|
|
|
@@ -187,6 +187,23 @@ class SessionManager:
|
|
|
187
187
|
response = await self.client.get("/ping")
|
|
188
188
|
return response.json() if response.text else {"status": "ok"}
|
|
189
189
|
|
|
190
|
+
async def server_time(self) -> dict[str, Any]:
|
|
191
|
+
"""Return the broker's current server time. No authentication required."""
|
|
192
|
+
response = await self.client.get("/time")
|
|
193
|
+
data = response.json() if response.text else {}
|
|
194
|
+
return data if isinstance(data, dict) else {"serverTime": data}
|
|
195
|
+
|
|
196
|
+
async def details(self) -> dict[str, Any]:
|
|
197
|
+
"""Return server-side session details (client id, account id, currency, timezone)."""
|
|
198
|
+
await self.ensure_logged_in()
|
|
199
|
+
response = await self.client.get("/session")
|
|
200
|
+
return response.json() if response.text else {}
|
|
201
|
+
|
|
202
|
+
async def encryption_key(self) -> dict[str, Any]:
|
|
203
|
+
"""Fetch the API encryption key and timestamp (encrypted-password login)."""
|
|
204
|
+
response = await self.client.get("/session/encryptionKey")
|
|
205
|
+
return response.json() if response.text else {}
|
|
206
|
+
|
|
190
207
|
async def logout(self) -> None:
|
|
191
208
|
"""End session and clear tokens."""
|
|
192
209
|
if not self.tokens:
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""CapitalComApp — the SDK facade wiring config + services + risk policy.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Note (0.x): one app per process. The underlying services use process-global
|
|
4
|
+
singletons, so constructing a second CapitalComApp with a different config in the
|
|
5
|
+
same process is not supported yet.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
from __future__ import annotations
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: capitalcom-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: Command-line client for the Capital.com Open API
|
|
5
5
|
Author: Simon Tarara
|
|
6
6
|
License: Apache-2.0
|
|
@@ -38,6 +38,7 @@ Browse markets, manage accounts and watchlists, preview and execute trades behin
|
|
|
38
38
|
[](LICENSE)
|
|
39
39
|
[](https://www.python.org/downloads/)
|
|
40
40
|
[](https://github.com/SimonTarara62/capitalcom-cli/releases)
|
|
41
|
+
[](docs/api-coverage.md)
|
|
41
42
|
|
|
42
43
|
```text
|
|
43
44
|
$ capctl market search "gold"
|
|
@@ -67,22 +68,26 @@ The Capital.com web platform is built for clicking; `capctl` is built for **repe
|
|
|
67
68
|
See [practical use cases](docs/use-cases.md) for worked, copy-pasteable scenarios.
|
|
68
69
|
|
|
69
70
|
**Scope:** `capctl` is first and foremost a command-line application, but the same
|
|
70
|
-
tested broker engine is also embeddable as
|
|
71
|
-
[Use as a library](#use-as-a-library
|
|
71
|
+
tested broker engine is also embeddable as a Python SDK — see
|
|
72
|
+
[Use as a library](#use-as-a-library) and [docs/sdk.md](docs/sdk.md).
|
|
72
73
|
The `capital_cli.core.*` internals remain private and may change between releases.
|
|
73
|
-
The CLI itself is short-lived: each command runs as its own process
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
The CLI itself is short-lived: each command runs as its own process. By default
|
|
75
|
+
(`CAP_PERSIST_SESSION=true`) the short-lived session tokens are cached so
|
|
76
|
+
back-to-back commands reuse one login instead of re-authenticating each time
|
|
77
|
+
(which can trip Capital.com's login-rate limit and return HTTP 429); `capctl
|
|
78
|
+
session login` is mainly a connectivity/account check.
|
|
76
79
|
|
|
77
80
|
## Features
|
|
78
81
|
|
|
79
|
-
- **Six command groups** covering the
|
|
82
|
+
- **Six command groups** covering the **full** Capital.com Open API surface — `session`, `market`, `account`, `trade`, `watchlist`, `stream` — verified by an automated [coverage matrix](docs/api-coverage.md)
|
|
80
83
|
- **Safety-first trading** — trading is off by default; enabling it requires an explicit EPIC allowlist, every execution goes through a two-phase *preview → execute* flow with risk checks, and mutating commands require `--yes`
|
|
81
84
|
- **`--json` everywhere** — every command can emit raw JSON for piping into `jq`, scripts, or CI jobs
|
|
82
85
|
- **Distinct exit codes per failure class** — scripts can branch on *why* a command failed (auth vs. risk-block vs. upstream error)
|
|
83
86
|
- **Real-time streaming** — live price tables, price-level alerts, and portfolio snapshots over WebSocket
|
|
84
87
|
- **Demo and live environments** — defaults to demo; live requires explicit opt-in
|
|
85
88
|
- **Built-in rate limiting** — client-side token buckets respect Capital.com's 10 req/s global, 1 req/s session, and trading-burst limits
|
|
89
|
+
- **Session-token caching** — back-to-back commands reuse one login instead of re-authenticating (avoids HTTP 429); on by default, opt out with `CAP_PERSIST_SESSION=false`
|
|
90
|
+
- **Embeddable SDK** — the same broker engine ships as an async Python library with a stable 0.x surface; see [Use as a library](#use-as-a-library)
|
|
86
91
|
|
|
87
92
|
## Installation
|
|
88
93
|
|
|
@@ -173,6 +178,7 @@ Main settings and their defaults:
|
|
|
173
178
|
| `CAP_REQUIRE_EXPLICIT_CONFIRM` | `true` | Mutations need `--yes` |
|
|
174
179
|
| `CAP_DRY_RUN` | `false` | Block all executions regardless of other flags |
|
|
175
180
|
| `CAP_DEFAULT_ACCOUNT_ID` | (none) | Account selected after login |
|
|
181
|
+
| `CAP_PERSIST_SESSION` | `true` | Cache short-lived session tokens in the state file so back-to-back commands reuse one login (avoids HTTP 429); set `false` to keep tokens in-process only |
|
|
176
182
|
| `CAP_HTTP_TIMEOUT_S` | `15` | HTTP timeout |
|
|
177
183
|
| `CAP_LOG_LEVEL` | `WARNING` | `DEBUG` … `CRITICAL` |
|
|
178
184
|
| `CAP_WS_ENABLED` | `false` | Required for `capctl stream …` |
|
|
@@ -272,9 +278,9 @@ capctl account topup 1000 --yes # demo environment only
|
|
|
272
278
|
Read-only:
|
|
273
279
|
|
|
274
280
|
```bash
|
|
275
|
-
capctl trade positions
|
|
281
|
+
capctl trade positions [--limit 10] # -n/--limit caps rows shown
|
|
276
282
|
capctl trade position DEAL_ID
|
|
277
|
-
capctl trade orders
|
|
283
|
+
capctl trade orders [--limit 10] # -n/--limit caps rows shown
|
|
278
284
|
capctl trade confirm DEAL_REFERENCE [--wait --timeout 30]
|
|
279
285
|
```
|
|
280
286
|
|
|
@@ -390,7 +396,7 @@ capctl stream alerts BTCUSD 75000 --direction ABOVE --duration 3600
|
|
|
390
396
|
*/15 * * * * capctl --json trade positions >> ~/logs/positions.jsonl 2>&1
|
|
391
397
|
```
|
|
392
398
|
|
|
393
|
-
## Use as a library
|
|
399
|
+
## Use as a library
|
|
394
400
|
|
|
395
401
|
Beyond the CLI, the same broker engine — risk policy, two-phase preview→execute,
|
|
396
402
|
rate limiting, WebSocket streaming — is available as an embeddable async SDK.
|
|
@@ -413,9 +419,12 @@ async def main():
|
|
|
413
419
|
asyncio.run(main())
|
|
414
420
|
```
|
|
415
421
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
422
|
+
**Stability:** the documented SDK surface — `CapitalComApp`, `CapitalComConfig`,
|
|
423
|
+
`RiskPolicy`, the service classes, and the `capital_cli.core.models` pydantic
|
|
424
|
+
models — is **stable**: no breaking changes within the 0.x series without a
|
|
425
|
+
deprecation cycle; breaking changes are reserved for a future 1.0. The
|
|
426
|
+
`capital_cli.core.*` internals remain private. See [docs/sdk.md](docs/sdk.md) for
|
|
427
|
+
read-only, preview→execute, streaming, and risk-policy examples.
|
|
419
428
|
|
|
420
429
|
## Exit codes
|
|
421
430
|
|
|
@@ -482,7 +491,7 @@ Or use the task runner: `make check` (lint + typecheck + test), `make docs`, `ma
|
|
|
482
491
|
## Documentation
|
|
483
492
|
|
|
484
493
|
- [Full CLI reference](docs/CLI.md) — every command and option (auto-generated)
|
|
485
|
-
- [Using capctl as a Python SDK](docs/sdk.md) —
|
|
494
|
+
- [Using capctl as a Python SDK](docs/sdk.md) — stable async SDK, import paths, examples, versioning
|
|
486
495
|
- [Scripting & automation](docs/scripting.md) — CI credentials, exit codes, jq recipes
|
|
487
496
|
- [Getting started from zero](docs/getting-started.md) — account, API key, install, first trade
|
|
488
497
|
- [Practical use cases](docs/use-cases.md) — what people actually do with capctl, with copy-pasteable scenarios
|
|
@@ -53,6 +53,7 @@ tests/test_build_broker_request.py
|
|
|
53
53
|
tests/test_cli_usage_errors.py
|
|
54
54
|
tests/test_config_errors.py
|
|
55
55
|
tests/test_core_port.py
|
|
56
|
+
tests/test_coverage_doc_in_sync.py
|
|
56
57
|
tests/test_credential_cmd.py
|
|
57
58
|
tests/test_doctor.py
|
|
58
59
|
tests/test_errors.py
|
|
@@ -69,6 +70,7 @@ tests/test_risk.py
|
|
|
69
70
|
tests/test_runner.py
|
|
70
71
|
tests/test_session_cmds.py
|
|
71
72
|
tests/test_session_persistence.py
|
|
73
|
+
tests/test_session_reads.py
|
|
72
74
|
tests/test_state.py
|
|
73
75
|
tests/test_stream_cmds.py
|
|
74
76
|
tests/test_trade_cmds.py
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Assert the committed coverage matrix + badge match the registry renderer, so
|
|
2
|
+
docs/api-coverage.md and docs/coverage-badge.json can never silently rot."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from tools.render_coverage import MARKER, render
|
|
8
|
+
|
|
9
|
+
REPO = Path(__file__).resolve().parents[1]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_matrix_table_in_sync():
|
|
13
|
+
table_md, _ = render()
|
|
14
|
+
# UTF-8 explicit: the matrix uses non-ASCII (— / − headers); the default
|
|
15
|
+
# encoding is cp1252 on Windows, which would mangle them and fail the compare.
|
|
16
|
+
committed = (REPO / "docs" / "api-coverage.md").read_text(encoding="utf-8")
|
|
17
|
+
assert MARKER in committed, "run: python tools/render_coverage.py"
|
|
18
|
+
assert committed.split(MARKER, 1)[1].strip() == table_md.split(MARKER, 1)[1].strip(), (
|
|
19
|
+
"docs/api-coverage.md is stale — run: python tools/render_coverage.py"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def test_badge_json_in_sync():
|
|
24
|
+
_, badge_json = render()
|
|
25
|
+
committed = (REPO / "docs" / "coverage-badge.json").read_text(encoding="utf-8")
|
|
26
|
+
assert json.loads(committed) == json.loads(badge_json), (
|
|
27
|
+
"docs/coverage-badge.json is stale — run: python tools/render_coverage.py"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_full_coverage_reached():
|
|
32
|
+
_, badge_json = render()
|
|
33
|
+
assert json.loads(badge_json)["message"].endswith("(100%)"), (
|
|
34
|
+
"coverage matrix is not 100% — fill remaining cells"
|
|
35
|
+
)
|
|
@@ -232,3 +232,51 @@ def test_validate_size_guards_non_positive_increment():
|
|
|
232
232
|
assert check.passed is True
|
|
233
233
|
effective, check = engine._validate_size(1.0, 0.1, 1000.0, -1.0, auto_normalize=False)
|
|
234
234
|
assert check.passed is True
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
async def test_working_order_preview_persists_type_level_to_state_file(tmp_path):
|
|
238
|
+
"""A passing working-order preview must persist type/level to the state file.
|
|
239
|
+
|
|
240
|
+
The CLI's `trade execute-order` runs in a separate process that loads the
|
|
241
|
+
preview from the state file. Regression for the `'type'` KeyError raised when
|
|
242
|
+
the working-order fields were only mutated in-memory and never re-saved.
|
|
243
|
+
"""
|
|
244
|
+
from capital_cli.core.config import get_config
|
|
245
|
+
from capital_cli.core.models import Direction, PreviewWorkingOrderRequest, WorkingOrderType
|
|
246
|
+
from capital_cli.core.state import StateStore
|
|
247
|
+
from capital_cli.services.confirmations import build_broker_request
|
|
248
|
+
|
|
249
|
+
cfg = get_config()
|
|
250
|
+
cfg.cap_allow_trading = True
|
|
251
|
+
cfg.cap_allowed_epics = "ALL"
|
|
252
|
+
cfg.cap_max_position_size = 100.0
|
|
253
|
+
cfg.cap_max_working_order_size = 100.0
|
|
254
|
+
try:
|
|
255
|
+
engine = RiskEngine()
|
|
256
|
+
# Real temp-file-backed state store so save/load round-trips through JSON.
|
|
257
|
+
engine.state = StateStore(path=tmp_path / "state.json")
|
|
258
|
+
_arm_market_details(engine)
|
|
259
|
+
request = PreviewWorkingOrderRequest(
|
|
260
|
+
epic="GOLD",
|
|
261
|
+
direction=Direction.BUY,
|
|
262
|
+
type=WorkingOrderType.LIMIT,
|
|
263
|
+
level=1900.0, # far from market (bid 2000 / offer 2001)
|
|
264
|
+
size=0.5,
|
|
265
|
+
)
|
|
266
|
+
result = await engine.preview_working_order(request)
|
|
267
|
+
assert result.all_checks_passed is True
|
|
268
|
+
|
|
269
|
+
# Simulate a fresh process: drop the in-memory cache so we load from disk.
|
|
270
|
+
engine._preview_cache.clear()
|
|
271
|
+
reloaded = engine.get_preview(result.preview_id)
|
|
272
|
+
|
|
273
|
+
assert reloaded.normalized_request.get("type") == "LIMIT"
|
|
274
|
+
assert "level" in reloaded.normalized_request
|
|
275
|
+
|
|
276
|
+
body = build_broker_request(reloaded.normalized_request, include_order_fields=True)
|
|
277
|
+
assert body["type"] == "LIMIT"
|
|
278
|
+
finally:
|
|
279
|
+
cfg.cap_allow_trading = False
|
|
280
|
+
cfg.cap_allowed_epics = ""
|
|
281
|
+
cfg.cap_max_position_size = 1.0
|
|
282
|
+
cfg.cap_max_working_order_size = 1.0
|
|
@@ -45,44 +45,38 @@ def test_switch(runner, mock_session):
|
|
|
45
45
|
|
|
46
46
|
@pytest.fixture
|
|
47
47
|
def mock_session_client(monkeypatch):
|
|
48
|
-
"""Patch
|
|
48
|
+
"""Patch the SessionManager read methods backing the session commands.
|
|
49
|
+
|
|
50
|
+
The CLI now delegates to ``get_session_manager().server_time()`` etc. rather
|
|
51
|
+
than calling the HTTP client inline, so we stub those passthroughs.
|
|
52
|
+
"""
|
|
49
53
|
sm = MagicMock()
|
|
50
54
|
sm.ensure_logged_in = AsyncMock()
|
|
55
|
+
sm.server_time = AsyncMock(return_value={})
|
|
56
|
+
sm.details = AsyncMock(return_value={})
|
|
57
|
+
sm.encryption_key = AsyncMock(return_value={})
|
|
51
58
|
monkeypatch.setattr("capital_cli.cli.session_cmds.get_session_manager", lambda: sm)
|
|
52
|
-
|
|
53
|
-
client = MagicMock()
|
|
54
|
-
resp = MagicMock()
|
|
55
|
-
resp.json = MagicMock(return_value={})
|
|
56
|
-
client.get = AsyncMock(return_value=resp)
|
|
57
|
-
monkeypatch.setattr("capital_cli.cli.session_cmds.get_client", lambda: client)
|
|
58
|
-
return client
|
|
59
|
+
return sm
|
|
59
60
|
|
|
60
61
|
|
|
61
62
|
def test_time(runner, mock_session_client):
|
|
62
|
-
mock_session_client.
|
|
63
|
-
result = runner.invoke(app, ["--json", "session", "time"])
|
|
64
|
-
assert result.exit_code == 0
|
|
65
|
-
assert json.loads(result.stdout)["serverTime"] == 1700000000000
|
|
66
|
-
assert mock_session_client.get.await_args.args[0] == "/time"
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
def test_time_wraps_bare_value(runner, mock_session_client):
|
|
70
|
-
mock_session_client.get.return_value.json.return_value = 1700000000000
|
|
63
|
+
mock_session_client.server_time.return_value = {"serverTime": 1700000000000}
|
|
71
64
|
result = runner.invoke(app, ["--json", "session", "time"])
|
|
72
65
|
assert result.exit_code == 0
|
|
73
66
|
assert json.loads(result.stdout)["serverTime"] == 1700000000000
|
|
67
|
+
mock_session_client.server_time.assert_awaited_once()
|
|
74
68
|
|
|
75
69
|
|
|
76
70
|
def test_details(runner, mock_session_client):
|
|
77
|
-
mock_session_client.
|
|
71
|
+
mock_session_client.details.return_value = {"clientId": "C1", "accountId": "A1"}
|
|
78
72
|
result = runner.invoke(app, ["--json", "session", "details"])
|
|
79
73
|
assert result.exit_code == 0
|
|
80
74
|
assert json.loads(result.stdout)["clientId"] == "C1"
|
|
81
|
-
|
|
75
|
+
mock_session_client.details.assert_awaited_once()
|
|
82
76
|
|
|
83
77
|
|
|
84
78
|
def test_encryption_key(runner, mock_session_client):
|
|
85
|
-
mock_session_client.
|
|
79
|
+
mock_session_client.encryption_key.return_value = {"encryptionKey": "K", "timeStamp": 1}
|
|
86
80
|
result = runner.invoke(app, ["session", "encryption-key"])
|
|
87
81
|
assert result.exit_code == 0
|
|
88
|
-
|
|
82
|
+
mock_session_client.encryption_key.assert_awaited_once()
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Unit tests for the SessionManager read passthroughs (server_time / details /
|
|
2
|
+
encryption_key) that back both the CLI session commands and the SDK."""
|
|
3
|
+
|
|
4
|
+
import asyncio
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.fixture
|
|
10
|
+
def _demo_env(monkeypatch):
|
|
11
|
+
monkeypatch.setenv("CAP_ENV", "demo")
|
|
12
|
+
monkeypatch.setenv("CAP_API_KEY", "k")
|
|
13
|
+
monkeypatch.setenv("CAP_IDENTIFIER", "id@example.com")
|
|
14
|
+
monkeypatch.setenv("CAP_API_PASSWORD", "pw")
|
|
15
|
+
from capital_cli.core.config import reset_config
|
|
16
|
+
|
|
17
|
+
reset_config()
|
|
18
|
+
yield
|
|
19
|
+
from capital_cli.core.config import reset_config as _rc
|
|
20
|
+
|
|
21
|
+
_rc()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class _FakeResp:
|
|
25
|
+
def __init__(self, payload):
|
|
26
|
+
self._payload = payload
|
|
27
|
+
self.text = "x"
|
|
28
|
+
|
|
29
|
+
def json(self):
|
|
30
|
+
return self._payload
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class _FakeClient:
|
|
34
|
+
def __init__(self, payload):
|
|
35
|
+
self._payload = payload
|
|
36
|
+
self.path = None
|
|
37
|
+
|
|
38
|
+
def set_relogin(self, *_a):
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
async def get(self, path):
|
|
42
|
+
self.path = path
|
|
43
|
+
return _FakeResp(self._payload)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _make_sm(payload):
|
|
47
|
+
from capital_cli.core.session import SessionManager
|
|
48
|
+
|
|
49
|
+
sm = SessionManager()
|
|
50
|
+
sm.client = _FakeClient(payload)
|
|
51
|
+
return sm
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_server_time_hits_time_endpoint(_demo_env):
|
|
55
|
+
sm = _make_sm({"serverTime": "2026-06-14T00:00:00"})
|
|
56
|
+
out = asyncio.run(sm.server_time())
|
|
57
|
+
assert sm.client.path == "/time"
|
|
58
|
+
assert out == {"serverTime": "2026-06-14T00:00:00"}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_server_time_wraps_bare_value(_demo_env):
|
|
62
|
+
sm = _make_sm(1700000000000)
|
|
63
|
+
out = asyncio.run(sm.server_time())
|
|
64
|
+
assert out == {"serverTime": 1700000000000}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_encryption_key_hits_endpoint(_demo_env):
|
|
68
|
+
sm = _make_sm({"encryptionKey": "abc", "timeStamp": 1})
|
|
69
|
+
out = asyncio.run(sm.encryption_key())
|
|
70
|
+
assert sm.client.path == "/session/encryptionKey"
|
|
71
|
+
assert out["encryptionKey"] == "abc"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def test_details_logs_in_then_hits_session(_demo_env, monkeypatch):
|
|
75
|
+
sm = _make_sm({"clientId": "c", "accountId": "a"})
|
|
76
|
+
|
|
77
|
+
async def _noop():
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
monkeypatch.setattr(sm, "ensure_logged_in", _noop)
|
|
81
|
+
out = asyncio.run(sm.details())
|
|
82
|
+
assert sm.client.path == "/session"
|
|
83
|
+
assert out["accountId"] == "a"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|