Last active
October 19, 2020 15:50
-
-
Save sharife3/36790aef9073f96f9f608fd177dd2cb4 to your computer and use it in GitHub Desktop.
Example of connecting and consuming APEX:E3 Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Request dependendencies need to be installed | |
import websocket | |
import _thread | |
import time | |
import sys | |
import requests | |
import json | |
clientId = "[CLIENT_ID]" | |
clientSecret = "[CLIENT_SECRET]" | |
authUrl = "https://keycloak.ae3platform.com/auth/realms/ApexE3/protocol/openid-connect/token" | |
wsUrl = "wss://ws.ae3platform.com/" | |
def get_access_token(): | |
print("--------- Authenticating ---------\n\n") | |
data = { | |
'grant_type': (None, 'client_credentials'), | |
'client_id': (None, clientId), | |
'client_secret': (None, clientSecret), | |
} | |
r = requests.post(authUrl, data) | |
result = r.json() | |
accessToken = result['access_token'] | |
print("--------- Authentication Token ---------\n\n") | |
print(accessToken) | |
print("\n\n") | |
return accessToken | |
def on_message(ws, message): | |
print("\n--------- WebSocket Message ---------\n") | |
print(message) | |
def on_error(ws, error): | |
print(error) | |
def on_close(ws): | |
print("### closed ###") | |
def on_open(ws): | |
def run(*args): | |
# while True: | |
print("--------- WebSocket Opened ---------\n\n") | |
subscriptionRequest = { | |
"action": "SUBSCRIBE", | |
"data": { | |
"event": "ORDERBOOK", | |
"baseId": "BTC:CRYPTO", | |
"quoteId": "USDT:CRYPTO", | |
"exchangeId": ["OKEX", "HITBTC", "BITFINEX", "BINANCE", "GATEIO", "FTX", "BITTREX", "HUOBIPRO", "KRAKEN", "POLONIEX", "ZB"], | |
"marketType": "SPOT", | |
"assetClassification": "ALT", | |
}, | |
} | |
print("--------- Subscription Request ---------\n\n") | |
print(subscriptionRequest) | |
print("\n\n") | |
time.sleep(1) | |
ws.send(json.dumps(subscriptionRequest)) | |
_thread.start_new_thread(run, ()) | |
if __name__ == "__main__": | |
websocket.enableTrace(True) | |
accessToken = get_access_token() | |
text = wsUrl+"?token="+accessToken | |
print(text) | |
ws = websocket.WebSocketApp( | |
text, on_message=on_message, on_error=on_error, on_close=on_close) | |
ws.on_open = on_open | |
ws.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment