Created
December 5, 2018 18:36
-
-
Save GamePad64/1bfda9f7efc85a5dcbef3aa131000791 to your computer and use it in GitHub Desktop.
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 decimal | |
import hashlib | |
import hmac | |
import json | |
import time | |
import urllib | |
import requests | |
class Api: | |
_api_url = 'https://tradernet.ru/api' | |
_api_key = None | |
_api_secret = None | |
def __init__(self, api_key, api_secret): | |
self._api_key = api_key | |
self._api_secret = api_secret | |
def _signature(self, d): | |
params = [] | |
for key in sorted(d): | |
params.append((key, d[key])) | |
params = urllib.parse.urlencode(params) | |
H = hmac.new(self._api_secret.encode(), params.encode(), digestmod=hashlib.sha256) | |
return H.hexdigest() | |
def call_v1(self, method, params): | |
q = {'cmd': method, 'params': params} | |
r = requests.post(self._api_url, data={'q': json.dumps(q)}) | |
return json.loads(r.content.decode(), parse_float=decimal.Decimal) | |
def call_v2(self, method, params): | |
params['cmd'] = method | |
params['apiKey'] = self._api_key | |
params['nonce'] = int(time.time() * 10000) | |
headers = {'X-NtApi-Sig': self._signature(params), | |
'Content-Type': 'application/x-www-form-urlencoded'} | |
url = '{}/v2/cmd/{}'.format(self._api_url, method) | |
r = requests.post(url, headers=headers, data=params) | |
return json.loads(r.content.decode(), parse_float=decimal.Decimal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment