From 4ef2ef60266f1c05304c0f182344df8f97fa52ea Mon Sep 17 00:00:00 2001 From: LeRaymy Date: Tue, 30 Apr 2024 10:48:46 +0200 Subject: [PATCH 1/2] feat: use logger instead of print --- okx/okxclient.py | 5 +++-- okx/utils.py | 10 ++++++---- setup.py | 1 + 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/okx/okxclient.py b/okx/okxclient.py index f799b22..d553679 100644 --- a/okx/okxclient.py +++ b/okx/okxclient.py @@ -3,6 +3,7 @@ import httpx from httpx import Client +from loguru import logger from . import consts as c, utils, exceptions @@ -33,8 +34,8 @@ def _request(self, method, request_path, params): header = utils.get_header_no_sign(self.flag, self.debug) response = None if self.debug == True: - print('domain:',self.domain) - print('url:',request_path) + logger.debug('domain:',self.domain) + logger.debug('url:',request_path) if method == c.GET: response = self.get(request_path, headers=header) elif method == c.POST: diff --git a/okx/utils.py b/okx/utils.py index f130a9b..23d7dad 100644 --- a/okx/utils.py +++ b/okx/utils.py @@ -3,6 +3,8 @@ import datetime from . import consts as c +from loguru import logger + def sign(message, secretKey): mac = hmac.new(bytes(secretKey, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256') @@ -12,7 +14,7 @@ def sign(message, secretKey): def pre_hash(timestamp, method, request_path, body,debug = True): if debug == True: - print('body: ',body) + logger.debug('body: ',body) return str(timestamp) + str.upper(method) + request_path + body @@ -25,7 +27,7 @@ def get_header(api_key, sign, timestamp, passphrase, flag,debug = True): header[c.OK_ACCESS_PASSPHRASE] = passphrase header['x-simulated-trading'] = flag if debug == True: - print('header: ',header) + logger.debug('header: ',header) return header def get_header_no_sign(flag,debug = True): @@ -33,7 +35,7 @@ def get_header_no_sign(flag,debug = True): header[c.CONTENT_TYPE] = c.APPLICATION_JSON header['x-simulated-trading'] = flag if debug == True: - print('header: ',header) + logger.debug('header: ',header) return header def parse_params_to_str(params): @@ -42,7 +44,7 @@ def parse_params_to_str(params): if(value != ''): url = url + str(key) + '=' + str(value) + '&' url = url[0:-1] - #print('url:',url) + #logger.debug('url:',url) return url diff --git a/setup.py b/setup.py index e4e8838..b91e237 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "importlib-metadata", "httpx[http2]", "keyring", + "loguru", "requests", "Twisted", "pyOpenSSL" From c295296a64d40128427e706320905ae6c949aff7 Mon Sep 17 00:00:00 2001 From: LeRaymy Date: Tue, 30 Apr 2024 18:52:48 +0200 Subject: [PATCH 2/2] fix: use f-string --- okx/okxclient.py | 4 ++-- okx/utils.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/okx/okxclient.py b/okx/okxclient.py index d553679..a3040c3 100644 --- a/okx/okxclient.py +++ b/okx/okxclient.py @@ -34,8 +34,8 @@ def _request(self, method, request_path, params): header = utils.get_header_no_sign(self.flag, self.debug) response = None if self.debug == True: - logger.debug('domain:',self.domain) - logger.debug('url:',request_path) + logger.debug(f'domain: {self.domain}') + logger.debug(f'url: {request_path}') if method == c.GET: response = self.get(request_path, headers=header) elif method == c.POST: diff --git a/okx/utils.py b/okx/utils.py index 23d7dad..c330ba5 100644 --- a/okx/utils.py +++ b/okx/utils.py @@ -14,7 +14,7 @@ def sign(message, secretKey): def pre_hash(timestamp, method, request_path, body,debug = True): if debug == True: - logger.debug('body: ',body) + logger.debug(f'body: {body}') return str(timestamp) + str.upper(method) + request_path + body @@ -27,7 +27,7 @@ def get_header(api_key, sign, timestamp, passphrase, flag,debug = True): header[c.OK_ACCESS_PASSPHRASE] = passphrase header['x-simulated-trading'] = flag if debug == True: - logger.debug('header: ',header) + logger.debug(f'header: {header}') return header def get_header_no_sign(flag,debug = True): @@ -35,7 +35,7 @@ def get_header_no_sign(flag,debug = True): header[c.CONTENT_TYPE] = c.APPLICATION_JSON header['x-simulated-trading'] = flag if debug == True: - logger.debug('header: ',header) + logger.debug(f'header: {header}') return header def parse_params_to_str(params): @@ -44,7 +44,7 @@ def parse_params_to_str(params): if(value != ''): url = url + str(key) + '=' + str(value) + '&' url = url[0:-1] - #logger.debug('url:',url) + #logger.debug(f'url: {url}') return url