@@ 190-229 (lines=40) @@ | ||
187 | ||
188 | return self._handle_result(res, method, url, **kwargs) |
|
189 | ||
190 | def _handle_result(self, res, method=None, url=None, **kwargs): |
|
191 | result = json.loads(res.content.decode('utf-8', 'ignore'), strict=False) |
|
192 | if 'errcode' in result: |
|
193 | result['errcode'] = int(result['errcode']) |
|
194 | ||
195 | if 'errcode' in result and result['errcode'] != 0: |
|
196 | errcode = result['errcode'] |
|
197 | errmsg = result.get('errmsg', errcode) |
|
198 | if self.auto_retry and errcode in ( |
|
199 | WeChatErrorCode.INVALID_CREDENTIAL.value, |
|
200 | WeChatErrorCode.INVALID_ACCESS_TOKEN.value, |
|
201 | WeChatErrorCode.EXPIRED_ACCESS_TOKEN.value): |
|
202 | logger.info('Component access token expired, fetch a new one and retry request') |
|
203 | self.fetch_access_token() |
|
204 | kwargs['params']['component_access_token'] = self.session.get( |
|
205 | 'component_access_token' |
|
206 | ) |
|
207 | return self._request( |
|
208 | method=method, |
|
209 | url_or_endpoint=url, |
|
210 | **kwargs |
|
211 | ) |
|
212 | elif errcode == WeChatErrorCode.OUT_OF_API_FREQ_LIMIT.value: |
|
213 | # api freq out of limit |
|
214 | raise APILimitedException( |
|
215 | errcode, |
|
216 | errmsg, |
|
217 | client=self, |
|
218 | request=res.request, |
|
219 | response=res |
|
220 | ) |
|
221 | else: |
|
222 | raise WeChatClientException( |
|
223 | errcode, |
|
224 | errmsg, |
|
225 | client=self, |
|
226 | request=res.request, |
|
227 | response=res |
|
228 | ) |
|
229 | return result |
|
230 | ||
231 | def fetch_access_token(self): |
|
232 | """ |
|
@@ 744-781 (lines=38) @@ | ||
741 | ||
742 | return self._handle_result(res, method=method, url=url, **kwargs) |
|
743 | ||
744 | def _handle_result(self, res, method=None, url=None, **kwargs): |
|
745 | result = json.loads(res.content.decode('utf-8', 'ignore'), strict=False) |
|
746 | if 'errcode' in result: |
|
747 | result['errcode'] = int(result['errcode']) |
|
748 | ||
749 | if 'errcode' in result and result['errcode'] != 0: |
|
750 | errcode = result['errcode'] |
|
751 | errmsg = result.get('errmsg', errcode) |
|
752 | if self.component.auto_retry and errcode in ( |
|
753 | WeChatErrorCode.INVALID_CREDENTIAL.value, |
|
754 | WeChatErrorCode.INVALID_ACCESS_TOKEN.value, |
|
755 | WeChatErrorCode.EXPIRED_ACCESS_TOKEN.value): |
|
756 | logger.info('Component access token expired, fetch a new one and retry request') |
|
757 | self.component.fetch_access_token() |
|
758 | kwargs['params']['component_access_token'] = self.component.access_token |
|
759 | return self._request( |
|
760 | method=method, |
|
761 | url_or_endpoint=url, |
|
762 | **kwargs |
|
763 | ) |
|
764 | elif errcode == WeChatErrorCode.OUT_OF_API_FREQ_LIMIT.value: |
|
765 | # api freq out of limit |
|
766 | raise APILimitedException( |
|
767 | errcode, |
|
768 | errmsg, |
|
769 | client=self, |
|
770 | request=res.request, |
|
771 | response=res |
|
772 | ) |
|
773 | else: |
|
774 | raise WeChatComponentOAuthException( |
|
775 | errcode, |
|
776 | errmsg, |
|
777 | client=self, |
|
778 | request=res.request, |
|
779 | response=res |
|
780 | ) |
|
781 | return result |
|
782 | ||
783 | def _get(self, url, **kwargs): |
|
784 | return self._request( |