def test_check_error(self): error_json = dict(error_code=0) assert error_json == check_error(error_json) error_json = dict(errcode=1, errmsg="test") with pytest.raises(ClientException) as err: check_error(error_json) assert err.value.args[0] == "1: test"
def test_check_error(self): error_json = dict( error_code=0 ) assert error_json == check_error(error_json) error_json = dict( errcode=1, errmsg="test" ) with pytest.raises(ClientException) as err: check_error(error_json) assert err.value.args[0] == "1: test"
def request(self, method, url, **kwargs): if "params" not in kwargs: kwargs["params"] = {"access_token": self.token} if isinstance(kwargs.get("data", ""), dict): body = _json.dumps(kwargs["data"], ensure_ascii=False) body = body.encode('utf8') kwargs["data"] = body r = requests.request(method=method, url=url, **kwargs) r.raise_for_status() r.encoding = "utf-8" json = r.json() if 'errcode' in json: json['errcode'] = int(json['errcode']) if 'errcode' in json and json['errcode'] != 0: errcode = json['errcode'] errmsg = json.get('errmsg', errcode) if errcode in (WeChatErrorCode.INVALID_CREDENTIAL.value, WeChatErrorCode.INVALID_ACCESS_TOKEN.value, WeChatErrorCode.EXPIRED_ACCESS_TOKEN.value): logger.info( 'Access token expired, fetch a new one and retry request') self.session.delete(self.access_token_key) self.get_access_token() access_token = self.session.get(self.access_token_key) logger.info('get new token %s' % access_token) kwargs["params"] = {"access_token": access_token} return super(WxClient, self).request(method=method, url=url, **kwargs) else: if check_error(json): return json if check_error(json): return json