Skip to content

Commit

Permalink
v3 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
jonte-z committed May 23, 2023
1 parent d654681 commit eafa989
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 3.0.0 - 2023-05-23

### Changed
- Modified format of combined streams in Websocket Market Streams. Please refer to `examples/websocket/websocket_stream/combined_streams.py` for example implementation.

### Removed
- Discontinued official support for Python 3.6

## 3.0.0rc2 - 2023-04-21

### Removed
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,14 @@ my_client.ping_connectivity(id="my_request_id")
my_client.ping_connectivity()
```

#### Combined Streams
- If you set `is_combined` to `True`, `"/stream/"` will be appended to the `baseURL` to allow for Combining streams.
- `is_combined` defaults to `False` and `"/ws/"` (raw streams) will be appended to the `baseURL`.

More websocket examples are available in the `examples` folder.

Example file "examples/websocket_api/app_demo.py" demonstrates how Websocket API and Websocket Stream can be used together.


### Connector v1 and v2

```python
Expand Down
2 changes: 1 addition & 1 deletion binance/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.0.0rc2"
__version__ = "3.0.0"
3 changes: 0 additions & 3 deletions binance/websocket/spot/websocket_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@


class SpotWebsocketStreamClient(BinanceWebsocketClient):
ACTION_SUBSCRIBE = "SUBSCRIBE"
ACTION_UNSUBSCRIBE = "UNSUBSCRIBE"

def __init__(
self,
stream_url="wss://stream.binance.com:9443",
Expand Down
33 changes: 16 additions & 17 deletions binance/websocket/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@


class BinanceWebsocketClient:
ACTION_SUBSCRIBE = "SUBSCRIBE"
ACTION_UNSUBSCRIBE = "UNSUBSCRIBE"

def __init__(
self,
stream_url,
Expand Down Expand Up @@ -56,6 +59,14 @@ def _initialize_socket(
logger=logger,
)

def _single_stream(self, stream):
if isinstance(stream, str):
return True
elif isinstance(stream, list):
return False
else:
raise ValueError("Invalid stream name, expect string or array")

def send(self, message: dict):
self.socket_manager.send_message(json.dumps(message))

Expand All @@ -75,17 +86,13 @@ def subscribe(self, stream, id=None):
json_msg = json.dumps({"method": "SUBSCRIBE", "params": stream, "id": id})
self.socket_manager.send_message(json_msg)

def unsubscribe(self, stream: str, id=None):
def unsubscribe(self, stream, id=None):
if not id:
id = get_timestamp()

if not self._single_stream(stream):
raise ValueError("Invalid stream name, expect a string")

stream = [stream]
self.socket_manager.send_message(
json.dumps({"method": "UNSUBSCRIBE", "params": stream, "id": id})
)
if self._single_stream(stream):
stream = [stream]
json_msg = json.dumps({"method": "UNSUBSCRIBE", "params": stream, "id": id})
self.socket_manager.send_message(json_msg)

def ping(self):
self.logger.debug("Sending ping to Binance WebSocket Server")
Expand All @@ -107,11 +114,3 @@ def list_subscribe(self, id=None):
self.socket_manager.send_message(
json.dumps({"method": "LIST_SUBSCRIPTIONS", "id": id})
)

def _single_stream(self, stream):
if isinstance(stream, str):
return True
elif isinstance(stream, list):
return False
else:
raise ValueError("Invalid stream name, expect string or array")
13 changes: 3 additions & 10 deletions examples/websocket/spot/websocket_stream/combined_streams.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/usr/bin/env python

import logging
import time

from binance.lib.utils import config_logging
from binance.websocket.spot.websocket_stream import SpotWebsocketStreamClient

config_logging(logging, logging.DEBUG)
counter = 1


def message_handler(_, message):
Expand All @@ -17,13 +14,9 @@ def message_handler(_, message):
my_client = SpotWebsocketStreamClient(on_message=message_handler, is_combined=True)


# subscribe one stream
my_client.kline(symbol="bnbusdt", interval="1m")

time.sleep(4)

# subscribe another
my_client.ticker(symbol="ethusdt")
my_client.subscribe(
stream=["bnbusdt@bookTicker", "ethusdt@kline_1m"],
)

time.sleep(10)
my_client.stop()
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@
"Intended Audience :: Developers",
"Intended Audience :: Financial and Insurance Industry",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
python_requires=">=3.6",
python_requires=">=3.7",
)

0 comments on commit eafa989

Please sign in to comment.