Skip to content

Commit b44e4c8

Browse files
authored
Merge pull request #10 from Polymarket/harley/websocket-compatibility
fix: correct WebSocket dependency compatibility
2 parents 55ecec8 + d141027 commit b44e4c8

4 files changed

Lines changed: 73 additions & 4 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,26 @@ jobs:
7272

7373
- name: Run tests
7474
run: uv run pytest
75+
76+
test-minimum-websockets:
77+
runs-on: ubuntu-latest
78+
steps:
79+
- uses: actions/checkout@v4
80+
81+
- name: Set up Python
82+
uses: actions/setup-python@v5
83+
with:
84+
python-version: '3.10'
85+
86+
- name: Install uv
87+
uses: astral-sh/setup-uv@38f3f104447c67c051c4a08e39b64a148898af3a # v4
88+
89+
- name: Install dependencies
90+
run: |
91+
uv sync --all-extras
92+
uv pip install 'websockets==13.0'
93+
94+
- name: Run tests with minimum websockets
95+
run: |
96+
uv run --no-sync python -c 'import websockets; assert websockets.__version__ == "13.0"'
97+
uv run --no-sync pytest

‎polymarket_us/websocket/base.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any
1010

1111
import websockets
12-
from websockets.asyncio.client import ClientConnection
12+
from websockets.asyncio.client import ClientConnection, connect
1313

1414
from polymarket_us.auth import create_auth_headers
1515
from polymarket_us.errors import PolymarketUSError
@@ -50,7 +50,7 @@ async def connect(self) -> None:
5050
url = f"{self.base_url}{self.path}"
5151
headers = create_auth_headers(self.key_id, self.secret_key, "GET", self.path)
5252

53-
self._ws = await websockets.connect(url, additional_headers=headers)
53+
self._ws = await connect(url, additional_headers=headers)
5454
self._emit("open")
5555

5656
# Start message handler

‎pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "polymarket-us"
7-
version = "1.0.0"
7+
version = "1.0.1"
88
description = "Polymarket US Python SDK"
99
readme = "README.md"
1010
license = "MIT"
@@ -35,7 +35,7 @@ classifiers = [
3535
dependencies = [
3636
"httpx>=0.27.0",
3737
"pynacl>=1.5.0",
38-
"websockets>=12.0",
38+
"websockets>=13.0",
3939
]
4040

4141
[project.optional-dependencies]

‎tests/test_websocket.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
"""Tests for WebSocket functionality."""
22

3+
import asyncio
4+
import base64
5+
from typing import Literal
6+
37
import pytest
8+
from nacl.signing import SigningKey
9+
from websockets.asyncio.server import ServerConnection, serve
10+
from websockets.http11 import Request
411

512
from polymarket_us import AuthenticationError, PolymarketUS
613
from polymarket_us.websocket import MarketsWebSocket, PrivateWebSocket
714

815
TEST_SECRET_KEY = "nWGxne/9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A="
916

1017

18+
@pytest.mark.parametrize("stream", ["markets", "private"])
19+
async def test_connect_sends_auth_headers(stream: Literal["markets", "private"]) -> None:
20+
requests: asyncio.Queue[Request] = asyncio.Queue()
21+
22+
async def handler(connection: ServerConnection) -> None:
23+
assert connection.request is not None
24+
await requests.put(connection.request)
25+
await connection.send('{"heartbeat": {}}')
26+
await connection.wait_closed()
27+
28+
async with serve(handler, "127.0.0.1", 0, close_timeout=1) as server:
29+
port = server.sockets[0].getsockname()[1]
30+
with PolymarketUS(
31+
key_id="test-key",
32+
secret_key=TEST_SECRET_KEY,
33+
api_base_url=f"http://127.0.0.1:{port}",
34+
) as client:
35+
ws = client.ws.markets() if stream == "markets" else client.ws.private()
36+
heartbeat = asyncio.Event()
37+
ws.on("heartbeat", heartbeat.set)
38+
try:
39+
await asyncio.wait_for(ws.connect(), timeout=5)
40+
request = await asyncio.wait_for(requests.get(), timeout=5)
41+
assert request.path == f"/v1/ws/{stream}"
42+
assert request.headers["X-PM-Access-Key"] == "test-key"
43+
timestamp = request.headers["X-PM-Timestamp"]
44+
assert timestamp.isdigit()
45+
signature = base64.b64decode(request.headers["X-PM-Signature"])
46+
signing_key = SigningKey(base64.b64decode(TEST_SECRET_KEY))
47+
signing_key.verify_key.verify(
48+
(timestamp + "GET" + request.path).encode(), signature
49+
)
50+
await asyncio.wait_for(heartbeat.wait(), timeout=5)
51+
assert ws.is_connected
52+
finally:
53+
await asyncio.wait_for(ws.close(), timeout=5)
54+
assert not ws.is_connected
55+
56+
1157
class TestWebSocketFactory:
1258
"""Tests for WebSocket factory."""
1359

0 commit comments

Comments
 (0)