diff --git a/pylot/plugins/helpers/pylot_helpers.py b/pylot/plugins/helpers/pylot_helpers.py index 8591733..3b14760 100644 --- a/pylot/plugins/helpers/pylot_helpers.py +++ b/pylot/plugins/helpers/pylot_helpers.py @@ -1,9 +1,9 @@ import json import os import pathlib +from time import time from dataclasses import dataclass -from datetime import datetime -from tempfile import mkdtemp +from tempfile import gettempdir from cumulus_api import CumulusApi @@ -18,34 +18,22 @@ def get_config_options(cls): options = json.load(_file) return options - @staticmethod - def get_hash_token_file(): - now: datetime = datetime.now() - captured_time: int = 60 - for ele in range(0, 60, 15): - if now.minute < ele: - captured_time = ele - break - return f"{now.day}-{now.hour}-{captured_time}" - @classmethod def get_cumulus_api_instance(cls): - """ Get Cumulus instance with cached token""" - if 'PRIVATE_API_LAMBDA_ARN' in os.environ: - print('getting non-token instance') - cml = CumulusApi() - pass - else: - print('getting token instance') - get_hashed_file_name = cls.get_hash_token_file() - token: str - tempfile = f'{mkdtemp()}/{get_hashed_file_name}' - if not os.path.isfile(tempfile): - with open(tempfile, 'w', encoding='utf-8') as _file: - cml = CumulusApi() - _file.write(cml.TOKEN) - else: - with open(tempfile, 'r', encoding='utf-8') as _file: + token_dir = f'{gettempdir()}/pylot_token/' + os.makedirs(token_dir, exist_ok=True) + token_file = f'{token_dir}token' + token = None + if os.path.isfile(token_file): + print(f'Using local token: {token_file}') + file_stat = os.stat(token_file) + if int(time()) - int(file_stat.st_mtime) < 3600: + with open(token_file, 'r', encoding='utf-8') as _file: token = _file.readline() - cml = CumulusApi(token=token) + + cml = CumulusApi(token=token) + if not token and cml.TOKEN: + with open(token_file, 'w+', encoding='utf-8') as _file: + _file.write(cml.TOKEN) + return cml diff --git a/requirements.txt b/requirements.txt index 7be2053..3221b31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -cumulus-api @ https://github.com/ghrcdaac/cumulus-api/archive/refs/tags/v3.0.0.zip +cumulus-api @ https://github.com/ghrcdaac/cumulus-api/archive/refs/tags/v3.0.1.zip boto3==1.24.28