class APIRequests(object):
def __init__(self):
import pdb;pdb.set_trace()
request_session = requests.session()
cookies = cache.get(settings.SSO_SESSION_COOKIES)
if not cookies:
request_session.post(settings.SSO_LOGIN_URL, json={
"username": settings.SSO_USERNAME,
"password": settings.SSO_PASSWORD
})
cookies = dict(request_session.cookies.items())
cache.set(settings.SSO_SESSION_COOKIES, cookies, timeout=settings.SSO_TIME_OUT)
self.cookies = cookies
def get(self, *args, **kwargs):
return requests.get(cookies=self.cookies, *args, **kwargs)
def post(self, *args, **kwargs):
return requests.get(cookies=self.cookies, *args, **kwargs)
def patch(self, *args, **kwargs):
return requests.patch(cookies=self.cookies, *args, **kwargs)
api_requests = APIRequests()
答案是:仅在application启动的时候初始化执行一遍,所以上面的代码不正确,当cookies过期后,不会再去重新获取新的cookies。