def check_list(list_value, min_value, max_value, name): if list_value is None: return if len(list_value) > max_value: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " is out of bound, the max size is " + str(max_value)) if len(list_value) < min_value: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " should contain " + str(min_value) + " item(s) at least")
def format_date(value, name): if value is None: return None if not isinstance(value, str): raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " must be string") try: new_time = time.strptime(value, "%Y-%m-%d") return time.strftime("%Y-%m-%d", new_time) except: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " is not invalid date format")
def check_response(json_wrapper): if json_wrapper.contain_key("success"): success = json_wrapper.get_boolean("success") if success is False: err_code = json_wrapper.get_int_or_default("code", "") err_msg = json_wrapper.get_string_or_default("msg", "") if err_code == "": raise BinanceApiException(BinanceApiException.EXEC_ERROR, "[Executing] " + err_msg) else: raise BinanceApiException(BinanceApiException.EXEC_ERROR, "[Executing] " + str(err_code) + ": " + err_msg) elif json_wrapper.contain_key("code"): code = json_wrapper.get_int("code") msg = json_wrapper.get_string_or_default("msg", "") if code != 200: raise BinanceApiException(BinanceApiException.EXEC_ERROR, "[Executing] " + str(code) + ": " + msg)
def check_range(value, min_value, max_value, name): if value is None: return if min_value > value or value > max_value: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " is out of bound. " + str(value) + " is not in [" + str( min_value) + "," + str(max_value) + "]")
def create_signature_with_query(secret_key, query): if secret_key is None or secret_key == "": raise BinanceApiException(BinanceApiException.KEY_MISSING, "Secret key are required") signature = hmac.new(secret_key.encode(), msg=query.encode(), digestmod=hashlib.sha256).hexdigest() return signature
def on_error(self, error_message): if self.request.error_handler is not None: print('error') exception = BinanceApiException( BinanceApiException.SUBSCRIPTION_ERROR, error_message) self.request.error_handler(exception) self.logger.error("[Sub][" + str(self.id) + "] " + str(error_message))
def create_signature(secret_key, builder): if secret_key is None or secret_key == "": raise BinanceApiException(BinanceApiException.KEY_MISSING, "Secret key are required") # keys = builder.param_map.keys() # query_string = '&'.join(['%s=%s' % (key, parse.quote(builder.param_map[key], safe='')) for key in keys]) query_string = builder.build_url() signature = hmac.new(secret_key.encode(), msg=query_string.encode(), digestmod=hashlib.sha256).hexdigest() builder.put_url("signature", signature)
def check_symbol(symbol): if not isinstance(symbol, str): raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] symbol must be string") if re.match(reg_ex, symbol): raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + symbol + " is invalid symbol")
def greater_or_equal(value, base, name): if value is not None and value < base: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " should be greater than " + base)
def check_should_none(value, name): if value is not None: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + name + " should be null")
def check_currency(currency): if not isinstance(currency, str): raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] currency must be string") if re.match(reg_ex, currency) is not None: raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] " + currency + " is invalid currency")
def check_symbol_list(symbols): if not isinstance(symbols, list): raise BinanceApiException(BinanceApiException.INPUT_ERROR, "[Input] symbols in subscription is not a list") for symbol in symbols: check_symbol(symbol)
def __check_mandatory_field(self, name): if name not in self.json_object: raise BinanceApiException(BinanceApiException.RUNTIME_ERROR, "[Json] Get json item field: " + name + " does not exist")
def on_close(self): if self.request.error_handler is not None and self.retry_count <= 0: exception = BinanceApiException( BinanceApiException.SUBSCRIPTION_ERROR, "close_socket") self.request.error_handler(exception) self.logger.error("[Sub][" + str(self.id) + "] close_socket")