Abstract header gen to seperate function

small_kucoin_fixes
jaredgoldman 2023-03-08 23:45:51 -05:00
parent 7074ca7713
commit cda045f123
1 changed files with 38 additions and 30 deletions

View File

@ -77,20 +77,17 @@ class Client:
self._key_secret = config["key_secret"] self._key_secret = config["key_secret"]
self._key_passphrase = config["key_passphrase"] self._key_passphrase = config["key_passphrase"]
async def _request( def _gen_auth_req_headers(
self, self,
action: Literal["POST", "GET", "PUT", "DELETE"], action: Literal["POST", "GET", "PUT", "DELETE"],
endpoint: str, endpoint: str,
api_v: str = "v2", api_v: str = "v2",
) -> Any: ):
now = int(time.time() * 1000) now = int(time.time() * 1000)
path = f'/api/{api_v}{endpoint}' path = f'/api/{api_v}{endpoint}'
str_to_sign = str(now) + action + path str_to_sign = str(now) + action + path
headers = {}
# Add headers to request if authenticated # Add headers to request if authenticated
if self._authenticated:
signature = base64.b64encode( signature = base64.b64encode(
hmac.new( hmac.new(
self._key_secret.encode('utf-8'), self._key_secret.encode('utf-8'),
@ -107,7 +104,7 @@ class Client:
).digest() ).digest()
) )
headers = { return {
"KC-API-SIGN": signature, "KC-API-SIGN": signature,
"KC-API-TIMESTAMP": str(now), "KC-API-TIMESTAMP": str(now),
"KC-API-KEY": self._key_id, "KC-API-KEY": self._key_id,
@ -115,9 +112,20 @@ class Client:
"KC-API-KEY-VERSION": "2" "KC-API-KEY-VERSION": "2"
} }
api_url = f"https://api.kucoin.com{path}" async def _request(
self,
action: Literal["POST", "GET", "PUT", "DELETE"],
endpoint: str,
api_v: str = "v2",
) -> Any:
headers = {}
if self._authenticated:
headers = self._gen_auth_req_headers(action, endpoint, api_v)
api_url = f"https://api.kucoin.com/api/{api_v}{endpoint}"
res = await asks.request(action, api_url, headers=headers) res = await asks.request(action, api_url, headers=headers)
# breakpoint()
if "data" in res.json(): if "data" in res.json():
return res.json()["data"] return res.json()["data"]
else: else: