Skip to content

refactor: share one Session across the client tree via composition (#2, #3)#137

Merged
martinkersner merged 1 commit into
mainfrom
refactor/shared-session-composition
Jul 3, 2026
Merged

refactor: share one Session across the client tree via composition (#2, #3)#137
martinkersner merged 1 commit into
mainfrom
refactor/shared-session-composition

Conversation

@martinkersner

Copy link
Copy Markdown
Member

Summary

PR 3 of 4 from the SDK-structure review — fixes issues #2 (per-resource Sessions) and #3 (is-a instead of has-a) together, since they share one root cause.

Before: every resource subclassed API, so each super().__init__ opened its own requests.Session. A single Datamaxi client created ~15 sessions/connection pools; grouping classes like Cex were an API (inheriting send_request, _handle_exception, session, …) they never used as one.

After: a new Resource base (in api.py) composes an API — holds a shared instance and forwards request_endpoint/query. Datamaxi builds one API and threads it through the whole tree via api=; the value rides through **kwargs, so resource method bodies are untouched. Standalone instantiation (CexTicker(api_key=..., base_url=...)) still builds its own transport.

Net: one Session / connection pool for the entire client, and client.cex is now a namespace (Resource), not an API.

Verification

  • Smoke test on a live Datamaxi("k"): 1 distinct _api object and 1 distinct Session across cex, cex.candle, cex.ticker, cex.symbol, premium, funding_rate, liquidation, index_price; isinstance(cex, Resource) True, isinstance(cex, API) False; standalone CexTicker keeps its own transport.
  • pytest -m "not integration": 134 passed, 11 skipped
  • black + flake8 clean across datamaxi/

Compatibility

No public surface change — client.cex.candle(...), direct resource instantiation, and the mocked/live test lanes all behave as before. conftest's API.send_request monkeypatch still applies (delegation routes through the shared API). _endpoints.py (codegen'd upstream) untouched.

Known failures

None.

Note

The Resource methods forward only request_endpoint/query — the two API members any resource actually calls on self (verified by grep; no resource touches self.session/self.base_url/send_request directly).

Resources subclassed API, so each opened its own requests.Session; a
Datamaxi client spun up ~15 pools and every grouping class (Cex, ...)
was itself an API it never used as one.

Add an API-composing Resource base (has-a, not is-a) that holds a shared
API and forwards request_endpoint/query. Datamaxi now builds ONE API and
threads it through the whole tree via api=, so all sub-clients reuse a
single Session / connection pool. Resource method bodies are unchanged
(api rides through **kwargs); standalone instantiation still builds its
own transport.

No public surface change: client.cex.candle(...) etc. work as before.
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