MCPcopy Create free account
hub / github.com/danpaquin/coinbasepro-python / WebsocketClient

Class WebsocketClient

cbpro/websocket_client.py:20–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18
19
20class WebsocketClient(object):
21 def __init__(
22 self,
23 url="wss://ws-feed.pro.coinbase.com",
24 products=None,
25 message_type="subscribe",
26 mongo_collection=None,
27 should_print=True,
28 auth=False,
29 api_key="",
30 api_secret="",
31 api_passphrase="",
32 # Make channels a required keyword-only argument; see pep3102
33 *,
34 # Channel options: ['ticker', 'user', 'matches', 'level2', 'full']
35 channels):
36 self.url = url
37 self.products = products
38 self.channels = channels
39 self.type = message_type
40 self.stop = True
41 self.error = None
42 self.ws = None
43 self.thread = None
44 self.auth = auth
45 self.api_key = api_key
46 self.api_secret = api_secret
47 self.api_passphrase = api_passphrase
48 self.should_print = should_print
49 self.mongo_collection = mongo_collection
50
51 def start(self):
52 def _go():
53 self._connect()
54 self._listen()
55 self._disconnect()
56
57 self.stop = False
58 self.on_open()
59 self.thread = Thread(target=_go)
60 self.keepalive = Thread(target=self._keepalive)
61 self.thread.start()
62
63 def _connect(self):
64 if self.products is None:
65 self.products = ["BTC-USD"]
66 elif not isinstance(self.products, list):
67 self.products = [self.products]
68
69 if self.url[-1] == "/":
70 self.url = self.url[:-1]
71
72 if self.channels is None:
73 self.channels = [{"name": "ticker", "product_ids": [product_id for product_id in self.products]}]
74 sub_params = {'type': 'subscribe', 'product_ids': self.products, 'channels': self.channels}
75 else:
76 sub_params = {'type': 'subscribe', 'product_ids': self.products, 'channels': self.channels}
77

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected