|
1
|
|
|
import sys |
|
2
|
|
|
import os |
|
3
|
|
|
import json |
|
4
|
|
|
import argparse |
|
5
|
|
|
import getpass |
|
6
|
|
|
import logging |
|
7
|
|
|
import requests |
|
8
|
|
|
from LORIS_helper import * |
|
9
|
|
|
from dotenv import load_dotenv |
|
10
|
|
|
|
|
11
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
12
|
|
|
logger = logging.getLogger('LORISQuery') |
|
13
|
|
|
|
|
14
|
|
|
def is_response_success(status_code, expected_code): |
|
15
|
|
|
""" |
|
16
|
|
|
A simple function to determine the success of the status code |
|
17
|
|
|
:param status_code: |
|
18
|
|
|
:return: boolean value |
|
19
|
|
|
""" |
|
20
|
|
|
if status_code == expected_code: |
|
21
|
|
|
return True |
|
22
|
|
|
else: |
|
23
|
|
|
return False |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def check_json(data): |
|
28
|
|
|
""" |
|
29
|
|
|
Check if the data input is JSON format compatible. |
|
30
|
|
|
:param data: |
|
31
|
|
|
:return: |
|
32
|
|
|
""" |
|
33
|
|
|
try: |
|
34
|
|
|
JSON = json.loads(data) |
|
35
|
|
|
return True, JSON |
|
36
|
|
|
except ValueError: |
|
37
|
|
|
return False, None |
|
38
|
|
|
except: |
|
39
|
|
|
return False, None |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def login(): |
|
43
|
|
|
""" |
|
44
|
|
|
Logs into LORIS using the stored credential. Must use PyCurl as Requests is not working. |
|
45
|
|
|
:return: BOOL if or not it is successful. also, the JSON token that is necessary to conduct further transactions. |
|
46
|
|
|
""" |
|
47
|
|
|
logger = logging.getLogger('LORIS_login') |
|
48
|
|
|
|
|
49
|
|
|
#Load environmental variables. |
|
50
|
|
|
load_dotenv() |
|
51
|
|
|
|
|
52
|
|
|
is_travis = 'TRAVIS' in os.environ |
|
53
|
|
|
|
|
54
|
|
|
username = os.getenv("LORISusername") |
|
55
|
|
|
password = os.getenv("LORISpassword") |
|
56
|
|
|
|
|
57
|
|
|
data = json.dumps({"username":username, "password":password}) |
|
58
|
|
|
|
|
59
|
|
|
#Login URL |
|
60
|
|
|
url = os.getenv("LORISurl") |
|
61
|
|
|
updated_url = url + 'login' |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
# requests style login # NOT WORKING! |
|
65
|
|
|
r = requests.post(updated_url, data=data) |
|
66
|
|
|
logger.info(str(r.status_code) + r.reason) |
|
67
|
|
|
|
|
68
|
|
|
response_json = r.json() |
|
69
|
|
|
|
|
70
|
|
|
return is_response_success(r.status_code, 200), response_json.get('token') |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
def getCNBP(token, endpoint): |
|
|
|
|
|
|
74
|
|
|
""" |
|
75
|
|
|
Get from a CNBP LORIS database endpoint |
|
76
|
|
|
:param endpoint: |
|
77
|
|
|
:return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
78
|
|
|
""" |
|
79
|
|
|
logger = logging.getLogger('LORIS_get') |
|
80
|
|
|
logger.info("Getting LORIS endpoing: "+ endpoint + "at") |
|
81
|
|
|
load_dotenv() |
|
82
|
|
|
url = os.getenv("LORISurl") |
|
83
|
|
|
updatedurl = url + endpoint |
|
84
|
|
|
logger.info(updatedurl) |
|
85
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
86
|
|
|
|
|
87
|
|
|
with requests.Session() as s: |
|
88
|
|
|
s.headers.update(HEADERS) |
|
89
|
|
|
r = s.get(updatedurl) |
|
90
|
|
|
logger.info("Get Result:" + str(r.status_code) + r.reason) |
|
91
|
|
|
|
|
92
|
|
|
return r.status_code, r.json() |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
def postCNBP(token, endpoint, data): |
|
|
|
|
|
|
96
|
|
|
""" |
|
97
|
|
|
post some data to a LORIS end point. |
|
98
|
|
|
:param endpoint: |
|
99
|
|
|
:param data: |
|
100
|
|
|
:return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests) |
|
101
|
|
|
""" |
|
102
|
|
|
logger = logging.getLogger('LORIS_post') |
|
103
|
|
|
logger.info("Posting data to: "+endpoint) |
|
104
|
|
|
logger.info("Data: "+data) |
|
105
|
|
|
logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!") |
|
106
|
|
|
load_dotenv() |
|
107
|
|
|
url = os.getenv("LORISurl") |
|
108
|
|
|
updatedurl = url + endpoint |
|
109
|
|
|
|
|
110
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
111
|
|
|
|
|
112
|
|
|
with requests.Session() as s: |
|
113
|
|
|
s.headers.update(HEADERS) |
|
114
|
|
|
r = s.post(updatedurl, data=data) |
|
115
|
|
|
logger.info("Post Result:" + str(r.status_code) + r.reason) |
|
116
|
|
|
|
|
117
|
|
|
return r.status_code, r |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
def putCNBP(token, endpoint, data): |
|
|
|
|
|
|
120
|
|
|
""" |
|
121
|
|
|
Put some data to a LORIS end point. |
|
122
|
|
|
:param endpoint: |
|
123
|
|
|
:param data: |
|
124
|
|
|
:return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests) |
|
125
|
|
|
""" |
|
126
|
|
|
logger = logging.getLogger('LORIS_put') |
|
127
|
|
|
logger.info("Putting data to: "+endpoint) |
|
128
|
|
|
logger.info("Data: "+data) |
|
129
|
|
|
logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!") |
|
130
|
|
|
|
|
131
|
|
|
load_dotenv() |
|
132
|
|
|
url = os.getenv("LORISurl") |
|
133
|
|
|
updatedurl = url + endpoint |
|
134
|
|
|
|
|
135
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
136
|
|
|
|
|
137
|
|
|
with requests.Session() as s: |
|
138
|
|
|
s.headers.update(HEADERS) |
|
139
|
|
|
r = s.put(updatedurl, data=data) |
|
140
|
|
|
logger.info("Put Result:" + str(r.status_code) + r.reason) |
|
141
|
|
|
|
|
142
|
|
|
return r.status_code, r |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
# Only executed when running directly. |
|
147
|
|
|
if __name__ == '__main__': |
|
148
|
|
|
#print(login()) |
|
149
|
|
|
#getCNBP("projects") |
|
150
|
|
|
Success, token = login() |
|
151
|
|
|
#print("Test complete") |