Skip to content

Split client.py into session, account, orders and push layers (no behaviour change) - #4

Merged
Bogzx merged 1 commit into
mainfrom
improve/2026-10-01-split-client
Oct 1, 2026
Merged

Bogzx merged 1 commit into
mainfrom
improve/2026-10-01-split-client

Conversation

@Bogzx

@Bogzx Bogzx commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Stacked on #3. Merge #3 first. If GitHub doesn't retarget this PR automatically after #3 merges, run gh pr edit 4 --base main.

Splits the 1,430-line client.py into layered modules. No behaviour change.

Why

client.py was a single class holding the HTTP plumbing and error mapping, login, keepalive and re-login, account reads, order placement (including IF-THEN SL/TP groups and conditional modify) and the Push API with its channel class. Reviewing or changing any one of these meant reading all of it.

What changed

The client is now a stack of layers that all operate on the same object, one module each:

Module Layer Contents
_session.py _SessionLayer _url, _request, error mapping, login/logout/ping, keepalive, re-login, parse_interval
_account.py _AccountLayer get_accounts, get_balance, get_positions, get_orders, get_order_history, …
_orders.py _OrdersLayer place_order, modify_order, cancel_order, close_position, close_all, modify_position_sl_tp
_push.py _PushLayer, _PushChannel websocket channels, subscriptions, message routing
_state.py _ClientState the shared attributes, declared once so each layer type-checks on its own
client.py DXTradeClient constructor, repr, context manager; re-exports parse_interval and _PushChannel
  • Method bodies moved unchanged, with one layering fix. The session layer's logout() used to call disconnect_websocket() from the push layer. Now _PushLayer.logout() closes the channels and then calls super().logout(), in the same order as before.
  • One test changed: tests/test_session.py patches time.sleep through dxtrade_wrapper._session instead of dxtrade_wrapper.client. That patches the same global time module.

Verification

  • python -m pytest -q: 127 passed, 13 skipped, the same as Prepare 0.2.0 for PyPI: self-contained sdist, settled public API, Trusted Publishing release workflow #3. DXTRADE_SPEC_TESTS=1 pytest tests/test_spec_conformance.py: 13 passed. ruff: clean. mypy dxtrade_wrapper: no issues in 9 files.
  • API-surface diff against the original class, loaded from a saved copy of the old client.py:
    • no member added or removed;
    • identical signatures for every method;
    • the two properties (is_authenticated, _ws_connected) have byte-identical source;
    • the only docstring change is logout, now split across two layers.
  • examples/offline_demo.py runs end to end and issues the same 11 requests as before.
  • python -m build then twine check --strict: PASSED. The wheel contains all new modules plus py.typed.

🤖 Generated with Claude Code

client.py held HTTP plumbing, login and keepalive, account reads, order
placement and the Push API in one 1,430-line class. It is now a stack of
layers on the same object, one module each:

  _session.py  HTTP, error mapping, login/logout/ping, keepalive, re-login
  _account.py  account discovery, balance, positions, orders, history
  _orders.py   place/modify/cancel/close, IF-THEN SL/TP groups
  _push.py     websocket channels, subscriptions, message routing
  _state.py    the shared attributes, declared once so each layer type-checks
  client.py    DXTradeClient: constructor, repr, context manager

Method bodies moved unchanged except one layering fix: the session layer's
logout() no longer calls the push layer; _PushLayer.logout() closes the
channels and then calls super(), in the same order as before.
DXTradeClient, parse_interval and _PushChannel still import from
dxtrade_wrapper.client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Bogzx
Bogzx changed the base branch from improve/2026-10-01-pypi-release-prep to main October 1, 2026 13:20
@Bogzx
Bogzx merged commit 754dbec into main Oct 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant