|
1
|
|
|
import requests |
|
|
|
|
|
|
2
|
|
|
import logging |
|
|
|
|
|
|
3
|
|
|
import sys |
|
|
|
|
|
|
4
|
|
|
import os |
|
|
|
|
|
|
5
|
|
|
import json |
|
|
|
|
|
|
6
|
|
|
from dotenv import load_dotenv |
|
7
|
|
|
from LORIS.helper import is_response_success |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.INFO) |
|
11
|
|
|
logger = logging.getLogger('LORISQuery') |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def login(): |
|
15
|
|
|
""" |
|
16
|
|
|
Logs into LORIS using the stored credential. Must use PyCurl as Requests is not working. |
|
17
|
|
|
:return: BOOL if or not it is successful. also, the JSON token that is necessary to conduct further transactions. |
|
|
|
|
|
|
18
|
|
|
""" |
|
19
|
|
|
logger = logging.getLogger('LORIS_login') |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
#Load environmental variables. |
|
22
|
|
|
load_dotenv() |
|
23
|
|
|
|
|
24
|
|
|
username = os.getenv("LORISusername") |
|
25
|
|
|
password = os.getenv("LORISpassword") |
|
26
|
|
|
|
|
27
|
|
|
data = json.dumps({"username":username, "password":password}) |
|
28
|
|
|
|
|
29
|
|
|
#Login URL |
|
30
|
|
|
url = os.getenv("LORISurl") |
|
31
|
|
|
updated_url = url + 'login' |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
# requests style login # NOT WORKING! |
|
35
|
|
|
r = requests.post(updated_url, data=data) |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
logger.info(str(r.status_code) + r.reason) |
|
39
|
|
|
|
|
40
|
|
|
response_json = r.json() |
|
41
|
|
|
|
|
42
|
|
|
return is_response_success(r.status_code, 200), response_json.get('token') |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
View Code Duplication |
def getCNBP(token, endpoint): |
|
|
|
|
|
|
46
|
|
|
""" |
|
47
|
|
|
Get from a CNBP LORIS database endpoint |
|
48
|
|
|
:param endpoint: |
|
49
|
|
|
:return: bool on if such PSCID (INSTITUTIONID + PROJECTID + SUBJECTID) exist already. |
|
50
|
|
|
""" |
|
51
|
|
|
logger = logging.getLogger('LORIS_get') |
|
|
|
|
|
|
52
|
|
|
logger.info("Getting LORIS endpoing: "+ endpoint + "at") |
|
53
|
|
|
load_dotenv() |
|
54
|
|
|
url = os.getenv("LORISurl") |
|
55
|
|
|
updatedurl = url + endpoint |
|
56
|
|
|
logger.info(updatedurl) |
|
57
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
60
|
|
|
s.headers.update(HEADERS) |
|
61
|
|
|
r = s.get(updatedurl) |
|
|
|
|
|
|
62
|
|
|
logger.info("Get Result:" + str(r.status_code) + r.reason) |
|
63
|
|
|
|
|
64
|
|
|
return r.status_code, r.json() |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
def postCNBP(token, endpoint, data): |
|
|
|
|
|
|
68
|
|
|
""" |
|
69
|
|
|
post some data to a LORIS end point. |
|
70
|
|
|
:param endpoint: |
|
71
|
|
|
:param data: |
|
72
|
|
|
:return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests) |
|
|
|
|
|
|
73
|
|
|
""" |
|
74
|
|
|
logger = logging.getLogger('LORIS_post') |
|
|
|
|
|
|
75
|
|
|
logger.info("Posting data to: "+endpoint) |
|
76
|
|
|
logger.info("Data: "+data) |
|
77
|
|
|
logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!") |
|
|
|
|
|
|
78
|
|
|
load_dotenv() |
|
79
|
|
|
url = os.getenv("LORISurl") |
|
80
|
|
|
updatedurl = url + endpoint |
|
81
|
|
|
|
|
82
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
85
|
|
|
s.headers.update(HEADERS) |
|
86
|
|
|
r = s.post(updatedurl, data=data) |
|
|
|
|
|
|
87
|
|
|
logger.info("Post Result:" + str(r.status_code) + r.reason) |
|
88
|
|
|
|
|
89
|
|
|
return r.status_code, r |
|
90
|
|
|
|
|
91
|
|
View Code Duplication |
def putCNBP(token, endpoint, data): |
|
|
|
|
|
|
92
|
|
|
""" |
|
93
|
|
|
Put some data to a LORIS end point. |
|
94
|
|
|
:param endpoint: |
|
95
|
|
|
:param data: |
|
96
|
|
|
:return: bool on if request is successful, r for the request (CAN BE NULL for 201 based requests) |
|
|
|
|
|
|
97
|
|
|
""" |
|
98
|
|
|
logger = logging.getLogger('LORIS_put') |
|
|
|
|
|
|
99
|
|
|
logger.info("Putting data to: "+endpoint) |
|
100
|
|
|
logger.info("Data: "+data) |
|
101
|
|
|
logger.info("!!!!!!!!!!BEWARE THAT SOME ENDPOINTS HAVE TRAILING SLASH, OTHERS DON'T.!!!!!!!!!!!!!!") |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
load_dotenv() |
|
104
|
|
|
url = os.getenv("LORISurl") |
|
105
|
|
|
updatedurl = url + endpoint |
|
106
|
|
|
|
|
107
|
|
|
HEADERS = {'Authorization': 'token {}'.format(token)} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
with requests.Session() as s: |
|
|
|
|
|
|
110
|
|
|
s.headers.update(HEADERS) |
|
111
|
|
|
r = s.put(updatedurl, data=data) |
|
|
|
|
|
|
112
|
|
|
logger.info("Put Result:" + str(r.status_code) + r.reason) |
|
113
|
|
|
|
|
114
|
|
|
return r.status_code, r |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
# Only executed when running directly. |
|
119
|
|
|
if __name__ == '__main__': |
|
120
|
|
|
#print(login()) |
|
121
|
|
|
#getCNBP("projects") |
|
122
|
|
|
Success, token = login() |
|
|
|
|
|
|
123
|
|
|
#print("Test complete") |
|
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.