Skip to content

Commit 17a199e

Browse files
committed
Refactor get_api_token.py to be K8s and docker friendly.
1 parent 9dbae8f commit 17a199e

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

get_api_token.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
from pyDataverse.api import Api
22
import json
33
import dvconfig
4+
from pathlib import Path
5+
import os
6+
import sys
7+
48
base_url = dvconfig.base_url
5-
api_token = dvconfig.api_token
6-
api = Api(base_url, api_token)
7-
username = 'dataverseAdmin'
8-
password = 'admin1'
9+
api = Api(base_url, '')
10+
11+
username = os.getenv('DATAVERSE_USER', 'dataverseAdmin')
12+
password = os.getenv('DATAVERSE_PASSWORD', 'admin1')
13+
# On K8s or with Docker we should get secrets from files, not env vars
14+
if Path(password).is_file():
15+
f = open(Path(password), 'r')
16+
password = f.read().strip()
17+
f.close()
18+
919
endpoint = '/builtin-users/' + username + '/api-token'
1020
params = {}
1121
params['password'] = password
12-
resp = api.get_request(endpoint, params=params, auth=True)
13-
api_token = resp.json()['data']['message']
14-
print(api_token)
22+
23+
resp = api.get_request(endpoint, params=params, auth=False)
24+
if resp.json()['status'] == "OK":
25+
api_token = resp.json()['data']['message']
26+
print(api_token)
27+
sys.exit(0)
28+
else:
29+
print("ERROR receiving API token:", file=sys.stderr)
30+
print(resp.json(), file=sys.stderr)
31+
print("Did you enable :AllowApiTokenLookupViaApi configuration option?", file=sys.stderr)
32+
print("See http://guides.dataverse.org/en/latest/installation/config.html#allowapitokenlookupviaapi", file=sys.stderr)
33+
sys.exit(1)

0 commit comments

Comments
 (0)