Skip to content

feat: add proxy support to ClobClient initialization#315

Open
zyaiire wants to merge 2 commits intoPolymarket:mainfrom
zyaiire:feat/309-add-proxy-support
Open

feat: add proxy support to ClobClient initialization#315
zyaiire wants to merge 2 commits intoPolymarket:mainfrom
zyaiire:feat/309-add-proxy-support

Conversation

@zyaiire
Copy link
Copy Markdown

@zyaiire zyaiire commented Mar 28, 2026

Summary

Fixes #309

Adds optional proxy parameter to ClobClient initialization, allowing users in regions with restricted access to configure an HTTP/HTTPS proxy for all CLOB API requests.

Changes

  • Added set_proxy(proxy) function to py_clob_client/http_helpers/helpers.py that reconfigures the shared httpx.Client with the given proxy URL
  • Added optional proxy: Optional[str] = None parameter to ClobClient.__init__ — when provided, calls set_proxy() during initialization
  • Added unit tests for set_proxy() in tests/http_helpers/test_proxy.py

Usage

from py_clob_client.client import ClobClient

# With proxy
client = ClobClient(
    host="https://clob.polymarket.com",
    chain_id=137,
    key="0x...",
    proxy="http://user:pass@proxy-host:8080",
)

# Without proxy (default, backward-compatible)
client = ClobClient(
    host="https://clob.polymarket.com",
    chain_id=137,
    key="0x...",
)

Testing

  • Added tests/http_helpers/test_proxy.py with 3 test cases covering proxy set, proxy=None, and proxy="" (empty string)
  • No new dependencies required — httpx natively supports the proxy parameter
  • Fully backward-compatible: proxy defaults to None, preserving existing behavior

Note

Medium Risk
Introduces a new global httpx.Client reconfiguration 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() in http_helpers/helpers.py, which rebuilds the shared httpx.Client with (or without) a proxy setting.

ClobClient.__init__ now accepts proxy and 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.

…t#309)

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@zyaiire zyaiire requested a review from a team as a code owner March 28, 2026 10:55
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

set_proxy("http://localhost:8080")

mock_client_cls.assert_called_with(http2=True, proxy="http://localhost:8080")
old_client.close.assert_called_once()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Additional Locations (2)
Fix in Cursor Fix in Web

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.

ClobClient add proxy ?

1 participant