finite_state_sdk.token_cache
1import finite_state_sdk 2import os 3import time 4 5 6""" 7A class for caching Finite State API tokens so that a new token is not required for every run of the script 8Use this in place of finite_state_sdk.get_auth_token 9Example Usage 10--- 11token_cache = TokenCache(ORGANIZATION_CONTEXT) 12token = token_cache.get_token(CLIENT_ID, CLIENT_SECRET) 13""" 14 15 16class TokenCache(): 17 """ 18 A class for caching Finite State API tokens so that a new token is not required for every run of the script 19 deprecated: Use finite_state_sdk.get_auth_token instead 20 """ 21 def __init__(self, organization_context, client_id=None): 22 self.token = None 23 24 self.client_id = client_id 25 self.organization_context = organization_context 26 self.token_path = f'.tokencache/{self.organization_context}' 27 if self.client_id: 28 self.token_path = f'.tokencache/{self.organization_context}-{self.client_id}' 29 else: 30 self.token_file = f'.tokencache/{self.organization_context}/token.txt' 31 self.token_file = f'{self.token_path}/token.txt' 32 33 # create the token cache directory if it doesn't exist 34 if not os.path.exists('.tokencache'): 35 os.mkdir('.tokencache') 36 37 # create the client directory if it doesn't exist 38 if not os.path.exists(self.token_path): 39 os.mkdir(self.token_path) 40 41 def _get_token_from_api(self, client_id, client_secret): 42 # get a new token 43 self.token = finite_state_sdk.get_auth_token(client_id, client_secret) 44 45 # write it to disk 46 with open(self.token_file, 'w') as f: 47 f.write(self.token) 48 49 def get_token(self, client_id, client_secret): 50 # try to read from disk 51 if self.token is None: 52 if os.path.exists(self.token_file): 53 # check how old the file is, if it is more than 24 hours old, delete it 54 if os.path.getmtime(self.token_file) < time.time() - 24 * 60 * 60: 55 print("Token is more than 24 hours old, deleting it...") 56 self.invalidate_token() 57 58 self._get_token_from_api(client_id, client_secret) 59 return self.token 60 else: 61 print("Getting saved token from disk...") 62 with open(self.token_file, 'r') as f: 63 self.token = f.read() 64 65 return self.token 66 else: 67 print("Querying the API for a new token...") 68 self._get_token_from_api(client_id, client_secret) 69 return self.token 70 71 else: 72 return self.token 73 74 def invalidate_token(self): 75 self.token = None 76 if os.path.exists(self.token_file): 77 os.remove(self.token_file) 78 self.token = None
class
TokenCache:
17class TokenCache(): 18 """ 19 A class for caching Finite State API tokens so that a new token is not required for every run of the script 20 deprecated: Use finite_state_sdk.get_auth_token instead 21 """ 22 def __init__(self, organization_context, client_id=None): 23 self.token = None 24 25 self.client_id = client_id 26 self.organization_context = organization_context 27 self.token_path = f'.tokencache/{self.organization_context}' 28 if self.client_id: 29 self.token_path = f'.tokencache/{self.organization_context}-{self.client_id}' 30 else: 31 self.token_file = f'.tokencache/{self.organization_context}/token.txt' 32 self.token_file = f'{self.token_path}/token.txt' 33 34 # create the token cache directory if it doesn't exist 35 if not os.path.exists('.tokencache'): 36 os.mkdir('.tokencache') 37 38 # create the client directory if it doesn't exist 39 if not os.path.exists(self.token_path): 40 os.mkdir(self.token_path) 41 42 def _get_token_from_api(self, client_id, client_secret): 43 # get a new token 44 self.token = finite_state_sdk.get_auth_token(client_id, client_secret) 45 46 # write it to disk 47 with open(self.token_file, 'w') as f: 48 f.write(self.token) 49 50 def get_token(self, client_id, client_secret): 51 # try to read from disk 52 if self.token is None: 53 if os.path.exists(self.token_file): 54 # check how old the file is, if it is more than 24 hours old, delete it 55 if os.path.getmtime(self.token_file) < time.time() - 24 * 60 * 60: 56 print("Token is more than 24 hours old, deleting it...") 57 self.invalidate_token() 58 59 self._get_token_from_api(client_id, client_secret) 60 return self.token 61 else: 62 print("Getting saved token from disk...") 63 with open(self.token_file, 'r') as f: 64 self.token = f.read() 65 66 return self.token 67 else: 68 print("Querying the API for a new token...") 69 self._get_token_from_api(client_id, client_secret) 70 return self.token 71 72 else: 73 return self.token 74 75 def invalidate_token(self): 76 self.token = None 77 if os.path.exists(self.token_file): 78 os.remove(self.token_file) 79 self.token = None
A class for caching Finite State API tokens so that a new token is not required for every run of the script deprecated: Use finite_state_sdk.get_auth_token instead
TokenCache(organization_context, client_id=None)
22 def __init__(self, organization_context, client_id=None): 23 self.token = None 24 25 self.client_id = client_id 26 self.organization_context = organization_context 27 self.token_path = f'.tokencache/{self.organization_context}' 28 if self.client_id: 29 self.token_path = f'.tokencache/{self.organization_context}-{self.client_id}' 30 else: 31 self.token_file = f'.tokencache/{self.organization_context}/token.txt' 32 self.token_file = f'{self.token_path}/token.txt' 33 34 # create the token cache directory if it doesn't exist 35 if not os.path.exists('.tokencache'): 36 os.mkdir('.tokencache') 37 38 # create the client directory if it doesn't exist 39 if not os.path.exists(self.token_path): 40 os.mkdir(self.token_path)
def
get_token(self, client_id, client_secret):
50 def get_token(self, client_id, client_secret): 51 # try to read from disk 52 if self.token is None: 53 if os.path.exists(self.token_file): 54 # check how old the file is, if it is more than 24 hours old, delete it 55 if os.path.getmtime(self.token_file) < time.time() - 24 * 60 * 60: 56 print("Token is more than 24 hours old, deleting it...") 57 self.invalidate_token() 58 59 self._get_token_from_api(client_id, client_secret) 60 return self.token 61 else: 62 print("Getting saved token from disk...") 63 with open(self.token_file, 'r') as f: 64 self.token = f.read() 65 66 return self.token 67 else: 68 print("Querying the API for a new token...") 69 self._get_token_from_api(client_id, client_secret) 70 return self.token 71 72 else: 73 return self.token