Skip to content

Commit

Permalink
adding d1_python.cnclient operations (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
iannesbitt committed Aug 14, 2023
1 parent 6b7f7a1 commit e457c0f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mnonboard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from mnonboard import utils
from mnonboard import info_chx
from mnonboard import data_chx
from mnonboard import cn
from mnonboard.defs import CFG, HELP_TEXT, SO_SRVR, CN_SRVR, CN_SRVR_BASEURL, CN_CERT_LOC, APPROVE_SCRIPT_LOC
from mnonboard import default_json, L

Expand All @@ -16,6 +17,13 @@ def run(cfg):
Args:
cfg (dict): Dict containing config variables.
"""
# auth
if not cfg['token']:
cfg['token'] = os.environ.get('D1_AUTH_TOKEN')
if not cfg['token']:
print('Your DataONE auth token is missing. Please enter it here and/or store it in the env variable "D1_AUTH_TOKEN".')
cfg['token'] = info_chx.req_input('Please enter your DataONE authentication token: ')
DC = cn.init_client(cn_url=cfg['cn_url'], auth_token=cfg['token'])
if cfg['info'] == 'user':
# do the full user-driven info gathering process
ufields = info_chx.user_input()
Expand Down
46 changes: 46 additions & 0 deletions mnonboard/cn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from os import environ
from d1_client.cnclient import CoordinatingNodeClient
from d1_common.types.dataoneTypes import Subject, person

from . import defs

def init_client(cn_url: str, auth_token: str):
"""
Initialize a d1_client.cnclient.CoordinatingNodeClient instance.
:param str cn_url: The URL
"""
options: dict = {"headers": {"Authorization": "Bearer " + auth_token}}
return CoordinatingNodeClient(cn_url, **options)

def get_subjects(client: CoordinatingNodeClient, orcid: str):
"""
"""
return client.getSubjectInfo(orcid)

def get_first_subject(client: CoordinatingNodeClient, orcid: str):
"""
"""
try:
return get_subjects(client=client, orcid=orcid)[0]
except IndexError:
return None

def get_subject_name(subject: Subject):
"""
"""
first, last = subject.content()[0].content()[1], subject.content()[0].content()[2]
return "%s %s" % (first, last)

def node_list(client: CoordinatingNodeClient):
"""
"""
nodes = client.listNodes()
for node in nodes.content():
print(node.name)
return nodes

def register_user(client: CoordinatingNodeClient, orcid: str, name: str, email: str=None):
"""
"""

1 change: 1 addition & 0 deletions mnonboard/defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'json_file': 'node.json',
'cn_url': 'https://cn-stage.test.dataone.org/cn',
'mode': 'testing',
'token': None,
'check_files': 5,
'local': False,
}
Expand Down

0 comments on commit e457c0f

Please sign in to comment.