33import time
44from os .path import exists , expanduser
55from pathlib import Path
6- from typing import List , Optional , Tuple
6+ from typing import List , Optional , Tuple , Union
77
88import requests
99from requests .auth import HTTPBasicAuth
@@ -39,18 +39,32 @@ def __init__(
3939 api_endpoint : str ,
4040 cached_profile : str = None ,
4141 cache_path : Path = Path (expanduser ('~/.cradl/token-cache.json' )),
42+ access_token : str = None ,
43+ access_token_expiration : Union [float , str ] = None ,
4244 ):
4345 if not all ([client_id , client_secret , auth_endpoint , api_endpoint ]):
4446 raise MissingCredentials
4547
46- self ._token = read_token_from_cache (cached_profile , cache_path ) if cached_profile else NULL_TOKEN
4748 self .client_id = client_id
4849 self .client_secret = client_secret
4950 self .auth_endpoint = auth_endpoint
5051 self .api_endpoint = api_endpoint
5152 self .cached_profile = cached_profile
5253 self .cache_path = cache_path
5354
55+ if access_token and access_token_expiration :
56+ try :
57+ expiration = float (access_token_expiration )
58+ except ValueError :
59+ raise ValueError (
60+ f'Invalid access_token_expiration "{ access_token_expiration } ". Expected a unix timestamp'
61+ )
62+ self ._token = access_token , expiration
63+ elif cached_profile :
64+ self ._token = read_token_from_cache (cached_profile , cache_path )
65+ else :
66+ self ._token = NULL_TOKEN
67+
5468 @property
5569 def access_token (self ) -> str :
5670 access_token , expiration = self ._token
@@ -124,12 +138,14 @@ def read_from_environ() -> List[Optional[str]]:
124138 :return: List of client_id, client_secret, auth_endpoint, api_endpoint
125139 :rtype: List[Optional[str]]"""
126140
127- return [os .environ .get (k ) for k in (
128- 'CRADL_CLIENT_ID' ,
129- 'CRADL_CLIENT_SECRET' ,
130- 'CRADL_AUTH_ENDPOINT' ,
131- 'CRADL_API_ENDPOINT' ,
132- )]
141+ return dict (
142+ access_token = os .environ .get ('CRADL_ACCESS_TOKEN' ),
143+ access_token_expiration = os .environ .get ('CRADL_ACCESS_TOKEN_EXPIRATION' ),
144+ api_endpoint = os .environ .get ('CRADL_API_ENDPOINT' ),
145+ auth_endpoint = os .environ .get ('CRADL_AUTH_ENDPOINT' ),
146+ client_id = os .environ .get ('CRADL_CLIENT_ID' ),
147+ client_secret = os .environ .get ('CRADL_CLIENT_SECRET' ),
148+ )
133149
134150
135151def read_from_file (credentials_path : str = expanduser ('~/.cradl/credentials.json' ),
@@ -152,13 +168,16 @@ def read_from_file(credentials_path: str = expanduser('~/.cradl/credentials.json
152168 raise MissingCredentials (f'Could not find credentials for profile { profile } ' )
153169
154170 credentials = all_credentials [profile ]
155- client_id = credentials .get ('client_id' )
156- client_secret = credentials .get ('client_secret' )
157- auth_endpoint = credentials .get ('auth_endpoint' )
158- api_endpoint = credentials .get ('api_endpoint' )
159- cached_profile = profile if credentials .get ('use_cache' , False ) else None
160171
161- return [client_id , client_secret , auth_endpoint , api_endpoint , cached_profile ]
172+ return dict (
173+ access_token = credentials .get ('access_token' ),
174+ access_token_expiration = credentials .get ('access_token_expiration' ),
175+ api_endpoint = credentials .get ('api_endpoint' ),
176+ auth_endpoint = credentials .get ('auth_endpoint' ),
177+ cached_profile = profile if credentials .get ('use_cache' , False ) else None ,
178+ client_id = credentials .get ('client_id' ),
179+ client_secret = credentials .get ('client_secret' ),
180+ )
162181
163182
164183def guess_credentials (profile = None ) -> Credentials :
@@ -173,12 +192,13 @@ def guess_credentials(profile=None) -> Credentials:
173192
174193 if profile :
175194 try :
176- return Credentials (* read_from_file (profile = profile ))
195+ return Credentials (** read_from_file (profile = profile ))
177196 except :
178197 raise MissingCredentials (f'Could not find valid credentials for { profile } in ~/.cradl/credentials.json' )
179198
180199 for guesser in [read_from_environ , read_from_file ]:
181- args = guesser () # type: ignore
182- if len (args ) >= 4 and all (args [:4 ]):
183- return Credentials (* args )
200+ try :
201+ return Credentials (** guesser ()) # Will raise MissingCredentials if not all required fields are present
202+ except MissingCredentials :
203+ continue
184204 raise MissingCredentials
0 commit comments