feat: add proxy support to ClobClient initialization#315
feat: add proxy support to ClobClient initialization#315zyaiire wants to merge 2 commits intoPolymarket:mainfrom
Conversation
…t#309) Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| self.__fee_rates = {} | ||
|
|
||
| # proxy | ||
| set_proxy(proxy) |
There was a problem hiding this comment.
Unconditional set_proxy call destroys shared HTTP client
High Severity
set_proxy(proxy) is called unconditionally in __init__, even when proxy is None (the default). Since set_proxy always closes the existing module-level _http_client and creates a new one, every ClobClient instantiation — even without a proxy — destroys the shared HTTP client, dropping all keep-alive connections and HTTP/2 state. This is a regression for all existing users and can break in-flight requests if multiple ClobClient instances coexist.
Additional Locations (1)
| set_proxy("http://localhost:8080") | ||
|
|
||
| mock_client_cls.assert_called_with(http2=True, proxy="http://localhost:8080") | ||
| old_client.close.assert_called_once() |
There was a problem hiding this comment.
Tests assert on wrong mock object for close
Medium Severity
The module-level _http_client is a real httpx.Client created at import time, before the mock is applied. When set_proxy runs, the mocked httpx.Client() call returns side_effect[0] (the test's old_client MagicMock) as the new _http_client. Then .close() is called on the real original client, not the MagicMock. The assertion old_client.close.assert_called_once() checks the wrong object, so these tests would fail when run.


Summary
Fixes #309
Adds optional
proxyparameter toClobClientinitialization, allowing users in regions with restricted access to configure an HTTP/HTTPS proxy for all CLOB API requests.Changes
set_proxy(proxy)function topy_clob_client/http_helpers/helpers.pythat reconfigures the sharedhttpx.Clientwith the given proxy URLproxy: Optional[str] = Noneparameter toClobClient.__init__— when provided, callsset_proxy()during initializationset_proxy()intests/http_helpers/test_proxy.pyUsage
Testing
tests/http_helpers/test_proxy.pywith 3 test cases covering proxy set, proxy=None, and proxy="" (empty string)httpxnatively supports theproxyparameterproxydefaults toNone, preserving existing behaviorNote
Medium Risk
Introduces a new global
httpx.Clientreconfiguration path and closes/replaces the shared client, which could affect connection reuse or concurrent callers if used in multi-client scenarios.Overview
Adds optional proxy configuration for all API requests by introducing
set_proxy()inhttp_helpers/helpers.py, which rebuilds the sharedhttpx.Clientwith (or without) aproxysetting.ClobClient.__init__now acceptsproxyand applies it during client construction, and new unit tests validate proxy/non-proxy behavior and that the old client is closed.Written by Cursor Bugbot for commit 5f9e401. This will update automatically on new commits. Configure here.