diff --git a/CHANGELOG.md b/CHANGELOG.md index 16c39df..1b8701e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Your `api_secret` is the private key (RSA) you generate - Learn more [here](https://www.bybit.com/en-US/help-center/bybitHC_Article?id=000001923&language=en_US) - See examples files: [HTTP](https://github.com/bybit-exchange/pybit/blob/master/examples/http_example_rsa_authentication.py) and [WebSocket](https://github.com/bybit-exchange/pybit/blob/master/examples/websocket_example_rsa_authentication.py) +- Add optional `private_auth_expire` arg for WebSocket (https://github.com/bybit-exchange/pybit/pull/154) ## [5.5.0] - 2023-07-17 ### Added diff --git a/pybit/_http_manager.py b/pybit/_http_manager.py index d08680b..95400cb 100644 --- a/pybit/_http_manager.py +++ b/pybit/_http_manager.py @@ -42,11 +42,11 @@ class _V5HTTPManager: recv_window: bool = field(default=5000) force_retry: bool = field(default=False) retry_codes: defaultdict[dict] = field( - default_factory=lambda: {}, + default_factory=dict, init=False, ) ignore_codes: dict = field( - default_factory=lambda: {}, + default_factory=dict, init=False, ) max_retries: bool = field(default=3) diff --git a/pybit/_websocket_stream.py b/pybit/_websocket_stream.py index ae324b5..a119819 100644 --- a/pybit/_websocket_stream.py +++ b/pybit/_websocket_stream.py @@ -37,6 +37,7 @@ def __init__( retries=10, restart_on_error=True, trace_logging=False, + private_auth_expire=1 ): self.testnet = testnet self.domain = domain @@ -50,6 +51,9 @@ def __init__( self.ws_name = ws_name if api_key: self.ws_name += " (Auth)" + + # Delta time for private auth expiration in seconds + self.private_auth_expire = private_auth_expire # Setup the callback directory following the format: # { @@ -202,7 +206,7 @@ def generate_rsa(): return base64.b64encode(signature).decode() # Generate expires. - expires = _helpers.generate_timestamp() + 1000 + expires = _helpers.generate_timestamp() + (self.private_auth_expire * 1000) # Generate signature. _val = f"GET/realtime{expires}" diff --git a/tests/test_pybit.py b/tests/test_pybit.py index c16de51..67159d1 100644 --- a/tests/test_pybit.py +++ b/tests/test_pybit.py @@ -80,3 +80,21 @@ def test_topic_category_mismatch(self): ) ws.order_stream(callback=self._callback_function) + +class PrivateWebSocketTest(unittest.TestCase): + # Connect to private websocket and see if we can auth. + def _callback_function(msg): + print(msg) + + def test_private_websocket_connect(self): + ws_private = WebSocket( + testnet=True, + channel_type="private", + api_key="...", + api_secret="...", + trace_logging=True, + #private_auth_expire=10 + ) + + ws_private.position_stream(callback=self._callback_function) + #time.sleep(10)