-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_office_lib.py
278 lines (206 loc) · 5.65 KB
/
user_office_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! /usr/b:win/env python3
import json
import sys
import requests
class UserOffice:
"""
UserOffice Client
...
Attributes
----------
None
Methods
-------
login(email, password):
Sign in to UserOffice and set the access token for this instance.
get_proposal(id):
Get a proposal by id.
"""
def __init__(self, base_url: str):
self._access_token = ""
self._headers = {}
self._base_url = base_url + ( "/graphql" if not base_url.endswith("graphql") else "")
self._proposal_fields = """
primaryKey,
proposalId,
proposer {
id,
firstname,
lastname,
institutionId,
email,
position
},
instruments {
id,
name,
shortCode,
description
}
"""
def login(self, email: str, password: str) -> None:
"""
Sign in to UserOffice and set the access token for this instance.
Parameters
----------
email : str
User email address
password : str
User password
Returns
-------
None
"""
query = """
mutation UserMutations {
login(email: "%s", password: "%s") {
token,
rejection {
reason,
context,
exception
}
}
}
""" % (
email,
password,
)
res = requests.post(
self._base_url,
json={"query": query}
)
if res.status_code != 200:
sys.exit(res.text)
access_token = res.json()["data"]["login"]["token"]
self._access_token = access_token
self._headers = {"Authorization": "Bearer " + self._access_token}
def set_access_token(self, token: str, error: bool = True) -> bool:
"""
Set the access token and test that it can access
Parameters
----------
token: str
access token
error: bool (defaul True)
throw error if it is not avble to connect if True
Returns
-------
result: bool
true if it could verify that it can access useroffice api
"""
self._access_token = token
self._headers = {"authorization": "Bearer " + self._access_token}
query = """
query {
users {
totalCount
}
}
"""
res = requests.post(
self._base_url,
json={"query": query},
headers=self._headers
)
output = ( res.status_code == 200 and "totalCount" in res.json()["data"]["users"] )
if error and not output:
# throw exception
raise Exception("Unable to establish connection with User Office")
return output
def proposals_get_one_by_primary_key(self, primaryKey: int) -> dict:
"""
Get a proposal by primary key.
Parameters
----------
primaryKey : int
The proposal primary key
Returns
-------
dict
The proposal with requested primary key
"""
query = """
query Proposals {
proposal(primaryKey: %d) {
%s
}
}
""" % (
primaryKey,
self._proposal_fields
)
res = requests.post(
self._base_url,
json={"query": query},
headers=self._headers
)
if res.status_code != 200:
sys.exit(res.text)
return res.json()["data"]["proposal"]
def proposals_get_one(self, id: str, logger) -> dict:
"""
Get a proposal by id.
Parameters
----------
id : int
The proposal id
Returns
-------
dict
The proposal with requested id
"""
query = """
query {
proposals(filter: { referenceNumbers: ["%s"] }) {
proposals {
%s
}
}
}
""" % (
id,
self._proposal_fields
)
#logger.info("user_office.proposals_get_one query : {}".format(query))
res = requests.post(
self._base_url,
json={"query": query},
headers=self._headers
)
if res.status_code != 200:
logger.info("user_office.proposals_get_one error : {}".format(res.text))
sys.exit(res.text)
data = res.json()
logger.info("user_office.proposals_get_one result :")
logger.info(json.dumps(data,indent=4))
return res.json()["data"]["proposals"]["proposals"][0]
def users_get_one_email(self, id: str) -> dict:
"""
Get a proposal by id.
Parameters
----------
id : int
The user id
Returns
-------
str
The user email
"""
query = """
query {
user(userId:%s) {
email
}
}
""" % (
id
)
res = requests.post(
self._base_url,
json={"query": query},
headers=self._headers
)
if res.status_code != 200:
sys.exit(res.text)
return res.json()["data"]["user"]["email"]