Skip to content

Commit

Permalink
Merge pull request #55 from NanmiCoder/feature/add_request_timeout
Browse files Browse the repository at this point in the history
feat: 增加请求的超时时间配置
  • Loading branch information
minibear2021 authored Apr 1, 2024
2 parents 3d2bb7c + 01a3181 commit 383c755
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
7 changes: 6 additions & 1 deletion examples/server/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
# 代理设置,None或者{"https": "http://10.10.1.10:1080"},详细格式参见https://requests.readthedocs.io/en/latest/user/advanced/#proxies
PROXY = None

# 请求超时时间配置
timeout = (10, 30) # 建立连接最大超时时间是10s,读取响应的最大超时时间是30s

# 初始化
wxpay = WeChatPay(
wechatpay_type=WeChatPayType.NATIVE,
Expand All @@ -56,7 +59,9 @@
cert_dir=CERT_DIR,
logger=LOGGER,
partner_mode=PARTNER_MODE,
proxy=PROXY)
proxy=PROXY,
timeout=timeout,
)

app = Flask(__name__)

Expand Down
7 changes: 5 additions & 2 deletions wechatpayv3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def __init__(self,
cert_dir=None,
logger=None,
partner_mode=False,
proxy=None):
proxy=None,
timeout=None):
"""
:param wechatpay_type: 微信支付类型,示例值:WeChatPayType.MINIPROG
:param mchid: 直连商户号,示例值:'1230000109'
Expand All @@ -28,6 +29,7 @@ def __init__(self,
:param logger: 日志记录器,示例值logging.getLoger('demo')
:param partner_mode: 接入模式,默认False为直连商户模式,True为服务商模式
:param proxy: 代理设置,示例值:{"https": "http://10.10.1.10:1080"}
:param timeout: 超时时间,示例值:(10, 30), 10为建立连接的最大超时时间,30为读取响应的最大超时实践
"""
from .core import Core

Expand All @@ -41,7 +43,8 @@ def __init__(self,
apiv3_key=apiv3_key,
cert_dir=cert_dir,
logger=logger,
proxy=proxy)
proxy=proxy,
timeout=timeout)
self._partner_mode = partner_mode

def sign(self, data, sign_type=SignType.RSA_SHA256):
Expand Down
13 changes: 7 additions & 6 deletions wechatpayv3/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class Core():
def __init__(self, mchid, cert_serial_no, private_key, apiv3_key, cert_dir=None, logger=None, proxy=None):
def __init__(self, mchid, cert_serial_no, private_key, apiv3_key, cert_dir=None, logger=None, proxy=None, timeout=None):
self._proxy = proxy
self._mchid = mchid
self._cert_serial_no = cert_serial_no
Expand All @@ -24,6 +24,7 @@ def __init__(self, mchid, cert_serial_no, private_key, apiv3_key, cert_dir=None,
self._cert_dir = cert_dir + '/' if cert_dir else None
self._logger = logger
self._init_certificates()
self._timeout = timeout

def _update_certificates(self):
path = '/v3/certificates'
Expand Down Expand Up @@ -117,15 +118,15 @@ def request(self, path, method=RequestType.GET, data=None, skip_verify=False, si
self._logger.debug('Request headers: %s' % headers)
self._logger.debug('Request params: %s' % data)
if method == RequestType.GET:
response = requests.get(url=self._gate_way + path, headers=headers, proxies=self._proxy)
response = requests.get(url=self._gate_way + path, headers=headers, proxies=self._proxy, timeout=self._timeout)
elif method == RequestType.POST:
response = requests.post(url=self._gate_way + path, json=None if files else data, data=data if files else None, headers=headers, files=files, proxies=self._proxy)
response = requests.post(url=self._gate_way + path, json=None if files else data, data=data if files else None, headers=headers, files=files, proxies=self._proxy, timeout=self._timeout)
elif method == RequestType.PATCH:
response = requests.patch(url=self._gate_way + path, json=data, headers=headers, proxies=self._proxy)
response = requests.patch(url=self._gate_way + path, json=data, headers=headers, proxies=self._proxy, timeout=self._timeout)
elif method == RequestType.PUT:
response = requests.put(url=self._gate_way + path, json=data, headers=headers, proxies=self._proxy)
response = requests.put(url=self._gate_way + path, json=data, headers=headers, proxies=self._proxy, timeout=self._timeout)
elif method == RequestType.DELETE:
response = requests.delete(url=self._gate_way + path, headers=headers, proxies=self._proxy)
response = requests.delete(url=self._gate_way + path, headers=headers, proxies=self._proxy, timeout=self._timeout)
else:
raise Exception('wechatpayv3 does no support this request type.')
if self._logger:
Expand Down

0 comments on commit 383c755

Please sign in to comment.