Last active
August 29, 2015 14:05
-
-
Save itsff/a18622badbfa02992c7c to your computer and use it in GitHub Desktop.
Python EdgeServer client
This file contains hidden or 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
import websocket | |
import json | |
import requests | |
import base64 | |
def get_access_token(username, password, auth_url="https://id.ttstage.com/oauth/token"): | |
login_payload = {'grant_type': 'password', 'username': username, 'password': password} | |
auth = base64.b64encode("b7b9974e8e2244e387ab64cadd0d667a:830b5b391b474a4780a8697d0396a27762aa40367e8549578ae71149df8c78c5") | |
h = {'Authorization': 'Basic ' + auth} | |
r = requests.post(url=auth_url, data=login_payload, headers=h, verify=False) | |
if not r.ok: | |
raise Exception("Login failed") | |
access_token = json.loads(r.content)['access_token'] | |
return access_token | |
def on_message(ws, message): | |
print message | |
msg = json.loads(message) | |
if "Ping" in msg: | |
ws.send('{"Pong":{}}') | |
if "UserLoginResponse" in msg: | |
m = {"AccountListRequest": { | |
"request_id": "bla bla bla" | |
}} | |
ws.send(json.dumps(m)) | |
if "AccountListResponse" in msg: | |
accts = msg["AccountListResponse"]["accounts"] | |
for a in accts: | |
print a["account_id"] | |
m = {"OrderSubscriptionRequest": { | |
"request_id": "bla blaaaaa", | |
"action": 1, | |
"account_id": a["account_id"] | |
}} | |
ws.send(json.dumps(m)) | |
def on_error(ws, error): | |
print error | |
def on_close(ws): | |
print "### closed ###" | |
def on_open(ws): | |
# Send a login request | |
msg = {'UserLoginRequest': { | |
'user_request_id': '1', | |
'auth_token': get_access_token("[email protected]", "Tt12345678"), | |
'sender_client_type': 2, | |
}} | |
msg_str = json.dumps(msg) | |
print msg_str | |
ws.send(msg_str) | |
pass | |
if __name__ == "__main__": | |
websocket.enableTrace(True) | |
ws = websocket.WebSocketApp("wss://edge-int-dev-cert.debesys.net", | |
on_message=on_message, | |
on_error=on_error, | |
on_close=on_close, | |
header=['Sec-WebSocket-Protocol: json-1']) | |
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