-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchivesspace.py
477 lines (413 loc) · 16.6 KB
/
archivesspace.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""**archivesspace** is a python module for making queries to ArchivesSpace much easier.
This code lives at: https://github.com/SmithCollegeLibraries/archivesspace-python
Documentation located here: https://smithcollegelibraries.github.io/archivesspace-python/
Compatibility
-------------
As of writing, **archivesspace** has only been tested with ArchivesSpace 2.1.2 and Python 3.
YMMV with other versions.
Getting started
------------------------------------------------------
At the heart of the module is the class `ArchivesSpace`. To set up a connection
create an `ArchivesSpace` with your login credentials, and run the `connect()`
method.
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>>
>>> aspace.connect()
>>> print(aspace.connection['user']['username'])
admin
To continue you will first need to familiarize yourself with the ArchivesSpace
REST API documentation located here:
https://archivesspace.github.io/archivesspace/api/#archivesspace-rest-api
Pro tip: If fields are missing from the API documentation, get them
from the horse's mouth by checking the ArchivesSpace JSON Schemas located
here:
https://github.com/archivesspace/archivesspace/blob/master/common/schemas
Note that required fields are indicated by "ifmissing" *not* "required."
Getting a record
-----------------
To retrieve a record from ArchivesSpace use the get() method.
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> jsonResponse = aspace.get("/users/1")
>>> jsonResponse['username']
'admin'
Posting a record
-----------------
To post a record to ArchivesSpace use the `post()` method.
Example:
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>>
>>> data = { "jsonmodel_type":"subject",
... "external_ids":[],
... "publish":True,
... "used_within_repositories":[],
... "used_within_published_repositories":[],
... "terms":[{ "jsonmodel_type":"term",
... "term":"Alberta",
... "term_type":"geographic",
... "vocabulary":"/vocabularies/1"}],
... "external_documents":[],
... "vocabulary":"/vocabularies/1",
... "authority_id":"myid114",
... "source":"local"}
>>>
>>> response = aspace.post("/subjects", data)
>>> # import pdb; pdb.set_trace()
>>> response['uri']
'/subjects/...'
Updating a record
-------------------
Upading a record in ArchivesSpace is a two step process. First, retrieve the
record, then post the modified version back to ArchivesSpace.
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> myrecord = aspace.get('/subjects/1')
>>> myrecord['scope_note'] = "Hello World"
>>> response = aspace.post('/subjects/1', requestData=myrecord)
>>> response['lock_version']
1
Behind the scenes: there's a special field called `lock_version` included in the
retrieved data structure. This field is required by ArchivesSpace when
you post the record back. This field ensures that only one agent edits the
record at a time.
Deleting a record
-------------------
You may find you wish to Delete a record.
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> data = { "jsonmodel_type":"subject",
... "external_ids":[],
... "publish":True,
... "used_within_repositories":[],
... "used_within_published_repositories":[],
... "terms":[{ "jsonmodel_type":"term",
... "term":"Corie Marshall",
... "term_type":"topical",
... "vocabulary":"/vocabularies/1"}],
... "external_documents":[],
... "vocabulary":"/vocabularies/1",
... "authority_id":"myid1a",
... "source":"local"}
>>>
>>> response = aspace.post("/subjects", requestData=data)
>>> uri = response['uri']
>>> jsonResponse = aspace.delete(uri)
>>> jsonResponse['status']
'Deleted'
Getting listings and search results
-----------------------------------
ArchivesSpace uses *paginated* responses for queries that would return many items.
To do a paginated query use the `getPaged()` method.
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> response = aspace.getPaged("/subjects")
>>> for subject in response:
... print(subject['title'])
...
Alberta
Reference
---------
"""
import requests
from string import Template
import json
import logging
import pprint
import configparser
logging.getLogger("requests").setLevel(logging.WARNING)
# Custom Error classes
class ConnectionError(Exception):
pass
class BadRequestType(Exception):
pass
class NotPaginated(Exception):
pass
class AspaceBadRequest(Exception):
def __init__(self, requestdata, response):
self.requestdata = requestdata
self.response = response
def __str__(self):
return "ASpace Bad Request 400 %s \nRequest data '%s'" % (formatResponse(self.response), formatJson(self.requestdata))
class AspaceForbidden(Exception):
pass
class AspaceNotFound(Exception):
pass
class AspaceError(Exception):
pass
def formatJson(data):
"Use this function to make data look nice"
return json.dumps(data, indent=4)
def formatResponse(response):
"Get the data element of a requests response and format it to be pretty"
return formatJson(response.json())
def logResponse(response):
logging.error('Response: ' + formatResponse(response))
def checkStatusCodes(response, data={}):
"""This helper function checks the response from a request for problems and then
returns the data if everything is fine.
"""
if response.status_code == 403:
logging.error("Forbidden -- check your credentials.")
logResponse(response)
raise AspaceForbidden
elif response.status_code == 400:
# logResponse(response)
raise AspaceBadRequest(data, response)
elif response.status_code == 404:
logging.error("Not Found.")
logResponse(response)
raise AspaceNotFound
elif response.status_code == 500:
logging.error("500 Internal Server Error")
logResponse(response)
raise AspaceError
elif response.status_code == 200:
return response.json()
else:
logging.error(str(response.status_code))
logResponse(response)
raise AspaceError
def _unionRequestData(defaultRequestData, newRequestData):
"""Merge default request data and any data passed to the method into one
unified set of data values to pass to ASpace for the request. Passed data
overrides default data.
>>> unionedData = _unionRequestData({"foo": "bar"}, {"hello": "world"})
>>> unionedData['foo']
'bar'
>>> unionedData['hello']
'world'
>>> _unionRequestData({"foo": "bar"}, {"foo": "world"})
{'foo': 'world'}
>>>
"""
data = {}
passedData = ""
try:
passedData = newRequestData
except:
pass
# Merge
data.update(defaultRequestData)
data.update(passedData)
return data
class ArchivesSpace(object):
"""Base class for establishing a session with an ArchivesSpace repository,
and doing API queries against it.
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> print(aspace.connection['user']['username'])
admin
"""
# Optional custom JSON serializer to be passed to json.dumps if provided
# See method ArchivesSpace.setJsonSerializerDefault()
jsonSerializerDefault = None
def __init__(self):
self.configData = configparser.ConfigParser()
self.serverConfig = None
self.session = None
def setServer(self, protocol, domain, port, username, password, path=""):
self.protocol = protocol
self.domain = domain
self.port = port
self.path = path
self.username = username
self.password = password
def setServerCfg(self, fileName, section="DEFAULT"):
try:
self.configData.read_file(open(fileName), source=fileName)
except FileNotFoundError:
logging.error('No configuration file found. Configuration file required. Please copy archivesspace-example.cfg to %s and edit.' % fileName)
exit(1)
try:
self.serverConfig = self.configData[section]
except KeyError:
print("'%s' section not present in configuration file %s" % (section, fileName))
exit(1)
try:
self.setServer(self.serverConfig['protocol'], self.serverConfig['hostname'], self.serverConfig['port'], self.serverConfig['username'], self.serverConfig['password'], path=self.serverConfig['path'])
except KeyError as e:
print("Missing configuration option %s" % e)
exit(1)
def _getHost(self):
"""Returns the host string containing the protocol domain name and port."""
hostTemplate = Template('$protocol://$domain:$port/$path')
return hostTemplate.substitute(protocol = self.protocol, domain = self.domain, port = self.port, path = self.path)
def _request(self, path, type, data):
# Send the request
datajson = ''
try:
if type == "post":
if self.jsonSerializerDefault is not None:
datajson = json.dumps(data, default = self.jsonSerializerDefault) # turn the data into json format for POST requests
else:
datajson = json.dumps(data) # turn the data into json format for POST requests
r = self.session.post(self._getHost() + path, data = datajson)
elif type == "get":
r = self.session.get(self._getHost() + path, data = data)
elif type == "delete":
r = self.session.delete(self._getHost() + path, data = data)
else:
raise BadRequestType
except requests.exceptions.ConnectionError:
logging.error('Unable to connect to ArchivesSpace. Check the host information.')
raise ConnectionError
else:
jsonResponse = checkStatusCodes(r, data=data)
return jsonResponse
def post(self, path, requestData={}):
"""Do a POST request to ArchivesSpace and return the JSON response
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>>
>>> data = { "jsonmodel_type":"subject",
... "external_ids":[],
... "publish":True,
... "used_within_repositories":[],
... "used_within_published_repositories":[],
... "terms":[{ "jsonmodel_type":"term",
... "term":"North Pole",
... "term_type":"geographic",
... "vocabulary":"/vocabularies/1"}],
... "external_documents":[],
... "vocabulary":"/vocabularies/1",
... "authority_id":"myid314",
... "source":"local"}
>>>
>>> response = aspace.post("/subjects", requestData=data)
>>> response['uri']
'/subjects/...'
>>>
"""
data = ""
try:
data = requestData
except e:
raise e
return self._request(path, 'post', data)
def get(self, path, requestData={}):
"""Do a GET request to ArchivesSpace and return the JSON response
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> jsonResponse = aspace.get("/users/1")
>>> jsonResponse['username']
'admin'
"""
data = ""
try:
data = requestData
except:
pass
return self._request(path, 'get', data)
def delete(self, path, requestData={}):
"""Do a DELETE request to ArchivesSpace and return the JSON repsonse
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> data = { "jsonmodel_type":"subject",
... "external_ids":[],
... "publish":True,
... "used_within_repositories":[],
... "used_within_published_repositories":[],
... "terms":[{ "jsonmodel_type":"term",
... "term":"Claire Marshall",
... "term_type":"topical",
... "vocabulary":"/vocabularies/1"}],
... "external_documents":[],
... "vocabulary":"/vocabularies/1",
... "authority_id":"myid73",
... "source":"local"}
>>>
>>> response = aspace.post("/subjects", requestData=data)
>>> uri = response['uri']
>>> jsonResponse = aspace.delete(uri)
>>> jsonResponse['status']
'Deleted'
"""
data = ""
try:
data = requestData
except:
pass
return self._request(path, 'delete', data)
def connect(self):
"""Start a sessions with ArchivesSpace. This must be done before anything else.
>>> from archivesspace import ArchivesSpace
>>> aspace = ArchivesSpace()
>>> aspace.setServer('http', 'localhost', '8089', 'admin', 'admin')
>>> aspace.connect()
>>> print(aspace.connection['user']['username'])
admin
"""
pathTemplate = Template('/users/$username/login')
path = pathTemplate.substitute(username = self.username)
# Use the requests Session class to handle the session
# http://docs.python-requests.org/en/master/user/advanced/#session-objects
self.session = requests.Session()
try:
response = self.session.post(self._getHost() + path, { "password" : self.password })
jsonResponse = checkStatusCodes(response)
except requests.exceptions.ConnectionError:
logging.error("Connection Error.")
exit(1)
else:
self.connection = jsonResponse # Save connection details as python data
self.sessionId = self.connection['session']
self.session.headers.update({ 'X-ArchivesSpace-Session' : self.sessionId })
def getPaged(self, path, requestData={}):
"""Automatically request all the pages to build a complete data set"""
requestData = _unionRequestData({"page": "1"}, requestData)
# Start a place to add the pages to as they come in
fullSet = []
# Get the first page
response = self.get(path, requestData=requestData)
# Start the big data set
try:
fullSet = response['results']
except KeyError:
raise NotPaginated
# Then determine how many pages there are
numPages = int(response['last_page']) + 1
# Loop through all the pages and append them to a single big data structure
for page in range(1, numPages):
requestDataPaginated = _unionRequestData(requestData, {"page": str(page)})
response = self.get(path, requestData=requestDataPaginated)
fullSet.extend(response['results'])
# Return the big data structure
return fullSet
def getPagedAllIds(self, path):
"""Get a list of all of the IDs"""
response = self.get(path, requestData={"all_ids": True})
# Expecting a list of ints, if it's not there's problem
if all(isinstance(item, int) for item in response):
return response
else:
raise NotPaginated
def setJsonSerializerDefault(self, jsonSerializerDefault):
"""Set an optional custom JSON serializer to be passed to json.dumps.
c.f.
https://docs.python.org/3/library/json.html#json.JSONEncoder.default
If you don't know what this is, don't use it.
"""
self.jsonSerializerDefault = jsonSerializerDefault
if __name__ == "__main__":
import doctest
print("Running tests...")
doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=True)