Skip to content

Commit 5985f46

Browse files
committed
add Auth.value property to access auth information
1 parent 7140b25 commit 5985f46

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

docs/guide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ in `Auth.from_key()`.
4646

4747
Once the authentication information is obtained, the most convenient way of
4848
managing it for local use is to write it to a secret file using `Auth.write()`.
49+
It can also be accessed, e.g. to store in an environment variable, as
50+
`Auth.value`.
4951

5052
For example, to obtain and store authentication information:
5153

planet/auth.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ def from_dict(
161161
) -> Auth:
162162
pass
163163

164+
@property
165+
@abc.abstractmethod
166+
def value(self):
167+
pass
168+
164169
def write(
165170
self,
166171
filename: str = None
@@ -254,8 +259,8 @@ def __init__(
254259
'''
255260
if not key:
256261
raise APIKeyAuthException('API key cannot be empty.')
257-
self.key = key
258-
super().__init__(self.key, '')
262+
self._key = key
263+
super().__init__(self._key, '')
259264

260265
@classmethod
261266
def from_dict(
@@ -268,7 +273,11 @@ def from_dict(
268273

269274
def to_dict(self):
270275
'''Represent APIKeyAuth as a dict.'''
271-
return {self.DICT_KEY: self.key}
276+
return {self.DICT_KEY: self._key}
277+
278+
@property
279+
def value(self):
280+
return self._key
272281

273282

274283
class _SecretFile():

tests/unit/test_auth.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727

2828
def test_Auth_read_key():
2929
test_auth = auth.Auth.read(key='test')
30-
assert test_auth.key == 'test'
30+
assert test_auth.value == 'test'
3131

3232

3333
def test_Auth_read_env(monkeypatch):
3434
monkeypatch.setenv('PL_API_KEY', 'a')
3535

3636
test_auth_env1 = auth.Auth.read()
37-
assert test_auth_env1.key == 'a'
37+
assert test_auth_env1.value == 'a'
3838

3939

4040
def test_Auth_read_file(tmp_path, monkeypatch):
@@ -45,7 +45,7 @@ def test_Auth_read_file(tmp_path, monkeypatch):
4545
monkeypatch.delenv('PL_API_KEY', raising=False)
4646
monkeypatch.setattr(auth, 'SECRET_FILE_PATH', secret_path)
4747
test_auth = auth.Auth.read()
48-
assert test_auth.key == 'testvar'
48+
assert test_auth.value == 'testvar'
4949

5050

5151
def test_Auth_read_error(tmp_path, monkeypatch):
@@ -68,7 +68,7 @@ def test_Auth_from_file_alternate_success(tmp_path):
6868
fp.write('{"key": "testvar"}')
6969

7070
test_auth = auth.Auth.from_file(secret_path)
71-
assert test_auth.key == 'testvar'
71+
assert test_auth.value == 'testvar'
7272

7373

7474
def test_Auth_from_file_alternate_doesnotexist(tmp_path):
@@ -92,7 +92,7 @@ def test_Auth_from_env_alternate_success(monkeypatch):
9292
monkeypatch.delenv('PL_API_KEY', raising=False)
9393

9494
test_auth_env1 = auth.Auth.from_env(alternate)
95-
assert test_auth_env1.key == 'testkey'
95+
assert test_auth_env1.value == 'testkey'
9696

9797

9898
def test_Auth_from_env_alternate_doesnotexist(monkeypatch):
@@ -121,7 +121,7 @@ def test_Auth_from_login(monkeypatch):
121121

122122
monkeypatch.setattr(auth, 'AUTH_URL', test_url)
123123
test_auth = auth.Auth.from_login('email', 'pw')
124-
assert test_auth.key == 'foobar'
124+
assert test_auth.value == 'foobar'
125125

126126

127127
def test_Auth_write_doesnotexist(tmp_path):

0 commit comments

Comments
 (0)