Skip to content

Commit

Permalink
Merge pull request #34 from ghrcdaac/mlh0079-5336-reuse-token
Browse files Browse the repository at this point in the history
Mlh0079 5336 reuse token
  • Loading branch information
camposeddie authored Dec 1, 2023
2 parents 6a85b0b + 4f7410f commit a994995
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
46 changes: 17 additions & 29 deletions pylot/plugins/helpers/pylot_helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a994995

Please sign in to comment.