Skip to content
Snippets Groups Projects
Commit 1ebaf142 authored by Geneviève Boucher's avatar Geneviève Boucher
Browse files

Merge branch '20-modify-the-command-line-to-use-the-personal-authentication-token' into 'master'

Resolve "Modify the command line to use the personal authentication token"

Closes #20

See merge request !18
parents ec8159e3 aeb69437
No related branches found
No related tags found
1 merge request!18Resolve "Modify the command line to use the personal authentication token"
......@@ -26,36 +26,39 @@ class Client:
api_datafiles_meta = 'api/v1/datafiles/json/metadata'
api_datafiles_annotation = 'api/v1/datafiles/json/annotation'
api_my_datasets = 'api/v1/my-datasets/list/json/'
""" python client to IRICdata API"""
def __init__(self, username, password=None, token=None,
url='https://thepond.bioinfo.iric.ca'):
self.url = url
self.user = username
self.pwd = password
self.pwd = password
self.token = token
if self.token is None and self.pwd is None:
self.pwd = getpass()
session = requests.session()
if self.token is not None:
if self.pwd is not None:
sys.stderr.write('WARNING: Ambiguous authentification, ignoring ' +
'password in favor of PAT\n')
self.token = self.token.strip()
session.headers.update({'Iric-Auth-Token': F'{self.token}'})
elif self.user and self.pwd:
login = os.path.join(self.url, 'login/')
session.get(login)
login_url = os.path.join(self.url, 'login/')
session.get(login_url)
csrftoken = session.cookies['csrftoken']
cookies = {'csrftoken': csrftoken}
payload = {
'username': self.user, 'password': self.pwd,
'csrfmiddlewaretoken': csrftoken, 'next': ''
}
headers = {'Referer': login}
headers = {'Referer': login_url}
login = session.post(login, cookies=cookies, data=payload,
login = session.post(login_url, cookies=cookies, data=payload,
headers=headers)
if login.status_code == 200:
......@@ -69,9 +72,9 @@ class Client:
raise IricDataConnectionError('Could not initiate connexion ' +
'with IRIC-Data')
else:
raise IricDataConnectionError('Could not initiate connexion ' +
'with IRIC-Data')
raise IricDataConnectionError('Connexion failed -- please verify ' +
'that you submitted a token or a ' +
'username/password combination')
self.session = session
......@@ -80,7 +83,7 @@ class Client:
except:
self.labs = None
print('get_available_labs() method is not implemented in api/v1 of IRIC-Data')
self.datasets = self.get_available_datasets()
#self.datafiles = self.get_available_datafiles()
......@@ -115,7 +118,7 @@ class Client:
"""
def get_datafiles_list(self, key_anno=None, value_anno=None,
dataset_id=None):
if key_anno is None and value_anno is None and dataset_id is None:
sys.stderr.write("WARNING: return all files is not implemented\n")
return(None)
......
......@@ -9,15 +9,23 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", help="Username", type=str, required=True)
parser.add_argument("-p", "--password", help="Password", type=str, default=None)
parser.add_argument("-t", "--token-file-path", type=str, default=None,
help="File containg Personal Authentication Token (PAT)")
parser.add_argument("--url", help="URL", type=str, default='https://thepond.bioinfo.iric.ca')
parser.add_argument("--dataset-id", help="Dataset ID", type=str, default=None)
parser.add_argument("--datafile-id", help="Datafile ID", type=str, default=None)
parser.add_argument("-D", "--download", help="Download", action='store_true')
parser.add_argument("-U", "--upload", help="Upload", action='store_true')
parser.add_argument("-P", "--update", help="Update", action='store_true')
args = parser.parse_args().__dict__
client = Client(args['username'], args['password'], args['url'])
if args['token_file_path'] is not None:
token = open(args['token_file_path'], 'r').read().strip()
else:
token = None
client = Client(username=args['username'], password=args['password'], token=token, url=args['url'])
if args['download']:
dsid = args['dataset_id']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment