Day 2620:学习

2024-03-29  本文已影响0人  kafkaliu

处理access token失效问题:

cache 设置 ttl

异常时重试机制

def __retry_on_access_token_expired():

r"""

内部装饰器。access token失效时清除缓存并重试请求。https://developer.work.weixin.qq.com/document/path/91039

错误码42001为access_token已过期。https://developer.work.weixin.qq.com/devtool/query

"""

def decorator(func):

@wraps(func)

def wrapper(url, **kwargs):

response = func(url, **kwargs)

if response.headers.get("error-code") == "42001":

get_access_token.cache_clear()

return func(url, **kwargs)

else:

return response

return wrapper

return decorator

@__retry_on_access_token_expired()

def wecom_post(url, **kwargs):

r"""

发送企业微信POST请求。内置access_token。

:param url: 请求url

"""

params = kwargs.pop("params", {})

params.update({"access_token": get_access_token()})

return requests.post(

url=url,

params=params,

**kwargs,

)

上一篇下一篇

猜你喜欢

热点阅读