I have some session class like
class Session:
def __init__(self,):
self._token_response_dict = self.get_token_details(Session._token_payload, Session._token_headers)
self.token = self._token_response_dict['access_token']
self.token_valid_time = datetime.datetime.now() + datetime.timedelta(
seconds=self._token_response_dict['expires_in'])
self.token_type = self._token_response_dict['token_type']
def is_valid(self):
return True if datetime.datetime.now() < self.token_valid_time else False
@staticmethod
def get_token_details(payload, headers):
r = requests.post('http://webservice/token',
data=payload, headers=headers)
return json.loads(r.json())
Before calling some function I have to check whether the session is valid or not. i am using session as a global variable like below.
def get_all_docs(client_id):
global session
if session.is_valid():
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "aff6cf56-dc63-ae16-762f-4637762733ce",
'authorization': session.token_type + ' ' + session.token
}
payload = {
'LastRefreshTime': datetime.datetime.now().strptime(os.environ['LastRefreshTime'], '%d-%m-%Y %H:%M:%S'),
'ClientID': client_id
}
r = requests.get(Details.get_docs_url, params=payload, headers=headers)
return json.loads(r.json())
else:
session = Session()
get_all_docs(client_id)
How to perform this validation using python decorators?
Aucun commentaire:
Enregistrer un commentaire