-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalutils.py
More file actions
65 lines (58 loc) · 2.05 KB
/
Copy pathlocalutils.py
File metadata and controls
65 lines (58 loc) · 2.05 KB
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
# script containing common functions for working with data with ArchivesSnake,
# so that you don't need to write the same code in multiple Python scripts
import os
import json
from asnake.aspace import ASpace
aspace = ASpace()
# get a JSON object from the ArchivesSpace API using ArchivesSnake
def get_json(uri):
r = aspace.client.get(uri)
if r.status_code == 200:
return r.json()
else:
r.raise_for_status()
# post a JSON object to the ArchivesSpace API using ArchivesSnake
def post_json(uri, data, returnURI=False):
r = aspace.client.post(uri, json=data)
message = r.json()
if r.status_code == 200:
if 'uri' in message:
print("{}: {}".format(message['status'], message['uri']))
if returnURI:
return message['uri']
else:
print("{}: {}".format(message['status'], uri))
if returnURI:
return uri
else:
print("Error: {}".format(message['error']))
if returnURI:
return None
# here's if you want to post without a JSON object (i.e. to suppress a record)
def post_uri(uri):
r = aspace.client.post(uri)
message = r.json()
if r.status_code == 200:
if 'uri' in message:
print("{}: {}".format(message['status'], message['uri']))
else:
print("{}: {}".format(message['status'], uri))
else:
print("Error: {}".format(message['error']))
# delete an object through the ArchivesSpace API using ArchivesSnake
def delete_json(uri):
r = aspace.client.delete(uri)
message = r.json()
if r.status_code == 200:
if 'uri' in message:
print("{}: {}".format(message['status'], message['uri']))
else:
print("{}: {}".format(message['status'], uri))
else:
print("Error: {}".format(message['error']))
# write JSON to a file (e.g. if you want to work on it in OpenRefine)
def write_json(filename, data):
if os.path.exists(filename):
os.remove(filename)
with open(filename, 'w') as f:
f.write(json.dumps(data))