Untitled
unknown
plain_text
3 years ago
1.9 kB
19
Indexable
import hmac
import time
import hashlib
import aiohttp
from urllib.parse import urlencode
async def dispatch_request(api_key, http_method):
session = aiohttp.ClientSession()
session.headers.update(
{"Content-Type": "application/json;charset=utf-8", "X-MBX-APIKEY": api_key}
)
return {
"GET": session.get,
"DELETE": session.delete,
"PUT": session.put,
"POST": session.post,
}.get(http_method, "GET")
def get_timestamp():
return int(time.time() * 1000)
def hashing(api_secret, query_string):
return hmac.new(
api_secret.encode("utf-8"), query_string.encode("utf-8"), hashlib.sha256
).hexdigest()
class requester:
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
self.base_url = "https://api.binance.com"
async def send_signed_request(self, http_method, url_path, payload={}):
query_string = urlencode(payload, True)
if query_string:
query_string = "{}×tamp={}".format(query_string, get_timestamp())
else:
query_string = "timestamp={}".format(get_timestamp())
url = (
self.base_url + url_path + "?" + query_string + "&signature=" + hashing(self.api_secret,
query_string)
)
async with dispatch_request(self.api_key, http_method) as response:
return await response.json()
async def send_public_request(self, url_path, payload={}):
query_string = urlencode(payload, True)
url = self.base_url + url_path
if query_string:
url = url + "?" + query_string
async with dispatch_request(self.api_key, "GET") as response:
return await response.json()Editor is loading...