From c603368cfb5c24933a22c0ea6f551a31e55c26be Mon Sep 17 00:00:00 2001 From: goodspeed Date: Tue, 26 Apr 2016 15:57:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ setup.py | 2 +- weixin/oauth2.py | 47 ++++++++++++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index bce3b96..cc9f860 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ python-weixin ----- A Python client for the Weixin REST APIs +0.1.1 新增功能 +----- +完善服务异常处理 + 0.1.0 功能 ----- 微信公众平台 和开放平台 支持 diff --git a/setup.py b/setup.py index 05fbe1f..192be37 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup(name="python-weixin", - version="0.1.0", + version="0.1.1", description="Weixin API client", license="BSD", install_requires=["simplejson","requests","six", "chardet"], diff --git a/weixin/oauth2.py b/weixin/oauth2.py index 95b1718..075279e 100644 --- a/weixin/oauth2.py +++ b/weixin/oauth2.py @@ -13,7 +13,8 @@ import requests -from requests.exceptions import ConnectTimeout +from requests.exceptions import (ConnectTimeout, ReadTimeout, + ConnectionError as _ConnectionError) from six.moves.urllib.parse import urlencode @@ -34,7 +35,16 @@ def __str__(self): class ConnectTimeoutError(Exception): - def __init__(self, code='timeout', description='Connect timeout'): + def __init__(self, code, description): + self.code = code + self.description = description + + def __str__(self): + return '%s: %s' % (self.code, self.description) + + +class ConnectionError(Exception): + def __init__(self, code, description): self.code = code self.description = description @@ -141,8 +151,11 @@ def get_authorize_login_url(self, scope=None, state=None): url = self._url_for_authorize(scope=scope, state=state) try: response = requests.get(url, timeout=TIMEOUT) - except ConnectTimeout: - raise ConnectTimeoutError() + except (ConnectTimeout, ReadTimeout): + raise ConnectTimeoutError('timeout', 'Connect timeout') + except _ConnectionError: + raise ConnectionError('conntect_error', + 'Failed to establish a new connection') headers = response.headers if int(headers.get('content-length', 384)) < 500: # 微信 参数错误返回html页面 http 状态码也是200 @@ -161,8 +174,11 @@ def exchange_for_access_token(self, code=None, refresh_token, scope=scope) try: response = requests.get(access_token_url, timeout=TIMEOUT) - except ConnectTimeout: - raise ConnectTimeoutError() + except (ConnectTimeout, ReadTimeout): + raise ConnectTimeoutError('timeout', 'Connect timeout') + except _ConnectionError: + raise ConnectionError('conntect_error', + 'Failed to establish a new connection') parsed_content = simplejson.loads(response.content.decode()) if parsed_content.get('errcode', 0): raise OAuth2AuthExchangeError( @@ -241,14 +257,11 @@ def make_request(self, url, method="GET", body=None, headers=None): if 'User-Agent' not in headers: headers.update({"User-Agent": "%s Python Client" % self.api.api_name}) - if method == 'GET': - try: - return requests.get(url, headers=headers, timeout=TIMEOUT) - except ConnectTimeout: - raise ConnectTimeoutError() - elif method == 'POST': - try: - return requests.post(url, data=body, headers=headers, - timeout=TIMEOUT) - except ConnectTimeout: - raise ConnectTimeoutError() + try: + return requests.request(method, url, data=body, + headers=headers, timeout=TIMEOUT) + except (ConnectTimeout, ReadTimeout): + raise ConnectTimeoutError('timeout', 'Connect timeout') + except _ConnectionError: + raise ConnectionError('conntect_error', + 'Failed to establish a new connection')