-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalma_api_client.py
executable file
·332 lines (292 loc) · 12.8 KB
/
alma_api_client.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import requests
from time import sleep
class AlmaAPIClient:
def __init__(self, api_key: str) -> None:
self.API_KEY = api_key
self.BASE_URL = "https://api-na.hosted.exlibrisgroup.com"
def _get_headers(self, format: str = "json") -> dict:
return {
"Authorization": f"apikey {self.API_KEY}",
"Accept": f"application/{format}",
"Content-Type": f"application/{format}",
}
def _get_api_data(self, response: requests.Response, format: str = "json") -> dict:
"""Return dictionary with response content and selected response headers.
If format is not json, the (presumably) XML content is in api_data["content"],
as a byte array.
"""
try:
if format == "json":
api_data: dict = response.json()
else:
api_data = {"content": response.content}
except requests.exceptions.JSONDecodeError:
# Some responses return nothing, which can't be decoded...
api_data = {}
# Add a few response elements caller can use
api_data["api_response"] = {
"headers": response.headers,
"status_code": response.status_code,
"request_url": response.url,
}
return api_data
def _call_get_api(
self, api: str, parameters: dict = None, format: str = "json"
) -> dict:
if parameters is None:
parameters = {}
get_url = self.BASE_URL + api
headers = self._get_headers(format)
response = requests.get(get_url, headers=headers, params=parameters)
api_data: dict = self._get_api_data(response, format)
return api_data
def _call_post_api(
self, api: str, data: dict, parameters: dict = None, format: str = "json"
) -> dict:
if parameters is None:
parameters = {}
post_url = self.BASE_URL + api
headers = self._get_headers(format)
# TODO: Non-JSON POST?
response = requests.post(
post_url, headers=headers, json=data, params=parameters
)
api_data: dict = self._get_api_data(response, format)
return api_data
def _call_put_api(
self, api: str, data: str, parameters: dict = None, format: str = "json"
) -> dict:
if parameters is None:
parameters = {}
headers = self._get_headers(format)
put_url = self.BASE_URL + api
# Handle both XML (required by update_bib) and default JSON
if format == "xml":
response = requests.put(
put_url, headers=headers, data=data, params=parameters
)
else:
# json default
response = requests.put(
put_url, headers=headers, json=data, params=parameters
)
api_data: dict = self._get_api_data(response, format)
return api_data
def _call_delete_api(
self, api: str, parameters: dict = None, format: str = "json"
) -> dict:
if parameters is None:
parameters = {}
delete_url = self.BASE_URL + api
headers = self._get_headers(format)
response = requests.delete(delete_url, headers=headers, params=parameters)
# Success is HTTP 204, "No Content"
if response.status_code != 204:
# TODO: Real error handling
print(delete_url)
print(response.status_code)
print(response.headers)
print(response.text)
# exit(1)
api_data: dict = self._get_api_data(response, format)
return api_data
def create_item(
self, bib_id: str, holding_id: str, data: dict, parameters: dict = None
) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{bib_id}/holdings/{holding_id}/items"
return self._call_post_api(api, data, parameters)
def get_items(self, bib_id: str, holding_id: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{bib_id}/holdings/{holding_id}/items"
return self._call_get_api(api, parameters)
def get_integration_profiles(self, parameters: dict = None) -> dict:
# Caller can pass search parameters, but must deal with possible
# multiple matches.
if parameters is None:
parameters = {}
api = "/almaws/v1/conf/integration-profiles"
return self._call_get_api(api, parameters)
def get_jobs(self, parameters: dict = None) -> dict:
# Caller normally will pass parameters, but they're not required.
# Caller must deal with possible multiple matches.
if parameters is None:
parameters = {}
api = "/almaws/v1/conf/jobs"
return self._call_get_api(api, parameters)
def run_job(self, job_id, data: dict = None, parameters: dict = None) -> dict:
# Tells Alma to queue / run a job; does *not* wait for completion.
# Caller must provide job_id outside of parameters.
# Running a scheduled job requires empty data {}; not sure about other jobs
if parameters is None:
parameters = {}
api = f"/almaws/v1/conf/jobs/{job_id}"
return self._call_post_api(api, data, parameters)
def wait_for_completion(
self, job_id: str, instance_id: str, seconds_to_poll: int = 15
) -> dict:
# Running a job just queues it to run; Alma assigns an instance id.
# This method allows the caller to wait until the given instance of
# the job has completed.
api = f"/almaws/v1/conf/jobs/{job_id}/instances/{instance_id}"
# progress value (0-100) can't be used as it remains 0 if FAILED.
# Use status instead; values from
# https://developers.exlibrisgroup.com/alma/apis/docs/xsd/rest_job_instance.xsd/
status = "NONE" # Fake value until API is called.
while status in [
"NONE",
"QUEUED",
"PENDING",
"INITIALIZING",
"RUNNING",
"FINALIZING",
]:
instance = self._call_get_api(api)
status = instance["status"]["value"]
print(status)
sleep(seconds_to_poll)
return instance
def get_fees(self, user_id: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/users/{user_id}/fees"
return self._call_get_api(api, parameters)
def get_analytics_report(self, parameters: dict = None) -> dict:
# Docs say to URL-encode report name (path);
# request lib is doing it automatically.
if parameters is None:
parameters = {}
api = "/almaws/v1/analytics/reports"
return self._call_get_api(api, parameters)
def get_analytics_path(self, path: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/analytics/paths/{path}"
return self._call_get_api(api, parameters)
def get_vendors(self, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = "/almaws/v1/acq/vendors"
return self._call_get_api(api, parameters)
def get_vendor(self, vendor_code: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/acq/vendors/{vendor_code}"
return self._call_get_api(api, parameters)
def get_bib(self, mms_id: str, parameters: dict = None) -> dict:
"""Return dictionary response, with Alma bib record (in Alma XML format),
in "content" element.
"""
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{mms_id}"
return self._call_get_api(api, parameters, format="xml")
def update_bib(self, mms_id: str, data: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{mms_id}"
return self._call_put_api(api, data, parameters, format="xml")
def get_holding(
self, mms_id: str, holding_id: str, parameters: dict = None
) -> dict:
"""Return dictionary response, with Alma holding record (in Alma XML format),
in "content" element.
"""
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{mms_id}/holdings/{holding_id}"
return self._call_get_api(api, parameters, format="xml")
def update_holding(
self, mms_id: str, holding_id: str, data: str, parameters: dict = None
) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/bibs/{mms_id}/holdings/{holding_id}"
return self._call_put_api(api, data, format="xml")
def get_set_members(self, set_id: str, parameters: dict = None) -> None:
if parameters is None:
parameters = {}
api = f"/almaws/v1/conf/sets/{set_id}/members"
return self._call_get_api(api, parameters)
def create_user(self, user: dict, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = "/almaws/v1/users"
return self._call_post_api(api, user, parameters)
def delete_user(self, user_id: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/users/{user_id}"
return self._call_delete_api(api, parameters)
def get_user(self, user_id: str, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/users/{user_id}"
return self._call_get_api(api, parameters)
def update_user(self, user_id: str, user: dict, parameters: dict = None) -> dict:
if parameters is None:
parameters = {}
api = f"/almaws/v1/users/{user_id}"
return self._call_put_api(api, user, parameters)
def get_general_configuration(self) -> dict:
"""Return general configuration info.
Useful for checking production / sandbox via environment_type.
"""
api = "/almaws/v1/conf/general"
return self._call_get_api(api)
def get_code_tables(self) -> dict:
"""Return list of code tables. This specific API is undocumented."""
api = "/almaws/v1/conf/code-tables"
return self._call_get_api(api)
def get_code_table(self, code_table: str, parameters: dict = None) -> dict:
"""Return specific code table, via name from get_code_tables()."""
if parameters is None:
parameters = {}
api = f"/almaws/v1/conf/code-tables/{code_table}"
return self._call_get_api(api, parameters)
def get_mapping_tables(self) -> dict:
"""Return list of mapping tables. This specific API is undocumented."""
api = "/almaws/v1/conf/mapping-tables"
return self._call_get_api(api)
def get_mapping_table(self, mapping_table: str, parameters: dict = None) -> dict:
"""Return specific mapping table, via name from get_mapping_tables()."""
if parameters is None:
parameters = {}
api = f"/almaws/v1/conf/code-tables/{mapping_table}"
return self._call_get_api(api, parameters)
def get_libraries(self) -> dict:
"""Return all libraries."""
api = "/almaws/v1/conf/libraries"
return self._call_get_api(api)
def get_library(self, library_code: str) -> dict:
"""Return data for a single library, via code.
Doesn't provide more details than each entry in get_libaries().
"""
api = f"/almaws/v1/conf/libraries/{library_code}"
return self._call_get_api(api)
def get_circulation_desks(self, library_code: str, parameters: dict = None) -> dict:
"""Return data about circ desks in a single library, via code."""
if parameters is None:
parameters = {}
api = f"/almaws/v1/conf/libraries/{library_code}/circ-desks/"
return self._call_get_api(api, parameters)
def get_funds(self, parameters: dict = None) -> dict:
"""Return data about all funds matching search in parameters."""
if parameters is None:
parameters = {}
api = "/almaws/v1/acq/funds"
return self._call_get_api(api, parameters)
def get_fund(self, fund_id: str, parameters: dict = None) -> dict:
"""Return data about a specific fund."""
if parameters is None:
parameters = {}
api = f"/almaws/v1/acq/funds/{fund_id}"
return self._call_get_api(api, parameters)
def update_fund(self, fund_id: str, fund: dict, parameters: dict = None) -> dict:
"""Update a specific fund."""
if parameters is None:
parameters = {}
api = f"/almaws/v1/acq/funds/{fund_id}"
return self._call_put_api(api, fund, parameters)