Skip to content

Commit e4090ce

Browse files
Added token to ws client and historical candles endpoint
1 parent b1ec17d commit e4090ce

4 files changed

Lines changed: 69 additions & 5 deletions

File tree

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,20 @@ orderbooks = client.get_historical_orderbooks(
480480
)
481481
```
482482

483+
* [`get_historical_candles`](https://developers.shrimpy.io/docs/#get-historical-candles)
484+
485+
```python
486+
candles = client.get_historical_candles(
487+
'Bittrex',
488+
'LTC',
489+
'BTC',
490+
'2019-05-19T00:00:00.000Z',
491+
'2019-05-20T00:00:00.000Z',
492+
100,
493+
'1m'
494+
)
495+
```
496+
483497
### Management Methods
484498

485499
* [`get_status`](https://developers.shrimpy.io/docs/#get-status)
@@ -507,12 +521,23 @@ The client handles pings to the Shrimpy server based on the [`API Documentation`
507521
import shrimpy
508522

509523

510-
client = shrimpy.ShrimpyWsClient()
524+
public_key = '6d73c2464a71b94a81aa7b13d...'
525+
private_key = 'e6238b0de3cdf19c7861f8e8f5d137ce7113ac1e884b191a14bbb2...'
526+
527+
# This is a sample handler, it simply prints the incoming message to the console
528+
def error_handler(err):
529+
print(err)
530+
511531

512532
# This is a sample handler, it simply prints the incoming message to the console
513533
def handler(msg):
514534
print(msg)
515535

536+
537+
api_client = shrimpy.ShrimpyApiClient(public_key, private_key)
538+
raw_token = api_client.get_token()
539+
client = shrimpy.ShrimpyWsClient(error_handler, raw_token['token'])
540+
516541
subscribe_data = {
517542
"type": "subscribe",
518543
"exchange": "coinbasepro",

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
setuptools.setup(
1515
name="shrimpy_python",
16-
version="0.0.7",
16+
version="0.0.8",
1717
author="ShrimpyOfficial",
1818
author_email="support@shrimpy.io",
1919
description="The Official Shrimpy API Python Client",

shrimpy/shrimpy_api_client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,25 @@ def get_historical_orderbooks(self, exchange, base_trading_symbol, quote_trading
399399
return self._call_endpoint('GET', query_string)
400400

401401

402+
def get_historical_candles(self, exchange, base_trading_symbol, quote_trading_symbol, start_time, end_time, limit, interval):
403+
endpoint = 'historical/candles'
404+
params = {
405+
'exchange': exchange,
406+
'baseTradingSymbol': base_trading_symbol,
407+
'quoteTradingSymbol': quote_trading_symbol,
408+
'startTime': start_time,
409+
'endTime': end_time,
410+
'limit': limit,
411+
'interval': interval
412+
}
413+
query_string = self._create_query_string(
414+
endpoint,
415+
params
416+
)
417+
418+
return self._call_endpoint('GET', query_string)
419+
420+
402421
def get_historical_instruments(self, exchange=None, base_trading_symbol=None, quote_trading_symbol=None):
403422
endpoint = 'historical/instruments'
404423
params = {}
@@ -429,6 +448,16 @@ def get_usage(self):
429448
return self._call_endpoint('GET', endpoint)
430449

431450

451+
#############
452+
# WebSocket #
453+
#############
454+
455+
def get_token(self):
456+
endpoint = 'ws/token'
457+
458+
return self._call_endpoint('GET', endpoint)
459+
460+
432461
###########
433462
# Helpers #
434463
###########

shrimpy/shrimpy_ws_client.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class ShrimpyWsClient():
3535
are defined, errors must be handled explicitly.
3636
'''
3737

38-
def __init__(self, error_handler=None):
39-
self.base_uri = 'wss://ws-feed.shrimpy.io/'
38+
def __init__(self, error_handler=None, token=None):
39+
self.base_url = 'wss://ws-feed.shrimpy.io'
4040
self.subscription_handlers = {}
4141
self.error_handler = error_handler
4242
self.pending_messages_to_send = []
@@ -45,6 +45,7 @@ def __init__(self, error_handler=None):
4545
self.is_closed = False
4646
self.connection = None
4747
self.connection_close_timeout = 10
48+
self.token = token
4849

4950
def connect(self):
5051
self.socket_thread = threading.Thread(target=self._run_socket_thread)
@@ -97,7 +98,11 @@ async def _connect(self):
9798
'''
9899
Handle connecting to shrimpy websocket server
99100
'''
100-
self.connection = await websockets.client.connect(self.base_uri)
101+
url = self.base_url
102+
if (self.token):
103+
url = self.base_url + "?token=" + self.token
104+
105+
self.connection = await websockets.client.connect(url)
101106
if (self.connection is None) or (not self.connection.open):
102107
raise ConnectionFailureException('Failed to start the connection. Please reconnect.')
103108

@@ -114,6 +119,11 @@ async def _disconnect(self):
114119
# Exceptions during connection termination aren't very useful
115120
pass
116121

122+
async def _reconnect(self, token=None):
123+
self.token = token
124+
await self._disconnect()
125+
await self._connect()
126+
117127
async def _receive_message_handler(self):
118128
'''
119129
The core loop that runs the websocket logic

0 commit comments

Comments
 (0)