I have an api request function that works properly. My signature serializes fine, and the api request will go through, but the moment i turn this in to an asynchronous function it gives me a serializing issue somewhere that is genuinely unaffected by the asynchronicity of the function.
I get: #Signature-Cannot serialize non-str key b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ=
async def kucoin_order(pair,side,amount):#Signature-Cannot serialize non-str key b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ='
kucoin_balance,api_key, api_secret, api_passphrase = {},settings_feed['kucoin_key'], settings_feed['kucoin_secret'], settings_feed['kucoin_quote']
timestamp = str(int(round(time.time() * 1000)))
data = {"clientOid": timestamp,"side": side,"symbol": pair,"type": "market","size": amount}
data_json = json.dumps(data)
str_to_sign = str(timestamp) + 'POST' + '/api/v1/orders' + data_json
signature=base64.b64encode(hmac.new(api_secret.encode('utf-8'),str_to_sign.encode('utf-8'),hashlib.sha256).digest())
passphrase=base64.b64encode(hmac.new(api_secret.encode('utf-8'),api_passphrase.encode('utf-8'),hashlib.sha256).digest())
headers = {"KC-API-SIGN": signature,#Cannot serialize non-str key b'aQbzHhquttFv+Kpd9TNkTHOmNy183ybJZTEP8PoDJyQ='
"KC-API-TIMESTAMP": str(timestamp),
"KC-API-KEY": api_key,
"KC-API-PASSPHRASE":passphrase,
"KC-API-KEY-VERSION":"2",
"Content-Type":"application/json"}
try:
async with aiohttp.ClientSession() as session:
assets = await session.post(f"https://api.kucoin.com/api/v1/orders",headers = headers, data = data_json)
if not assets.status == 200:raise Exception('Status code not 200')
assets = await assets.json()
if 'status' in assets and assets['status'] == 'error': raise Exception('Status code not 200')
if debug: print('Kucoin order for', pair, assets)
response = requests.post(url, headers=headers, data=data_json).json()
print(response.json())
except Exception as err:
print(err)
I've tried different variations of the encoding of bytecode for the signature to no avail. So even if you don't have an api key/pass, you can still see it won't encode it properly. There is no returning bad, just that it won't encode it properly.
aiohttp
doesn't automatically convert bools and bytes to a string in the headers, params, etc. dictionaries. Do it yourself.