This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path73-ArchivesSpace-API-Workshop.html
85 lines (72 loc) · 3.1 KB
/
73-ArchivesSpace-API-Workshop.html
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
<!DOCTYPE html>
<html>
<head>
<link href="css/slides.css" rel="stylesheet" />
</head>
<body>
<nav>
<a id="start-slide" rel="nav" href="00-ArchivesSpace-API-Workshop.html" title="Return to start of presentation">Start</a>
<a id="prev-slide" rel="nav" href="72-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="74-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>6. Accessions</h1>
<h2>create_accession implementation</h2>
<p>This process works the same as for agent so I’m jumping straight to the code.</p>
<p>This <em>import</em> block is the same one we used with agents.</p>
<pre><code class="language-python"> #!/usr/bin/env python3
import urllib.request
import urllib.parse
import urllib.error
import json
# Local modules
import login
</code></pre>
<p>In our definition section we’ll create a <em>create_accession</em> function.</p>
<pre><code class="language-python"> def create_accession(api_url, auth_token, repo_id, accession_model):
'''create an accession returning a status object'''
data = json.JSONEncoder().encode(accession_model).encode('utf-8')
url = api_url+'/repositories/'+str(repo_id)+'/accessions'
req = urllib.request.Request(
url = url,
data = None,
headers = {'X-ArchivesSpace-Session': auth_token},
method = 'POST')
try:
response = urllib.request.urlopen(req, data)
except urllib.error.URLError as e:
print(e.reason)
return None
except urllib.error.HTTPError as e:
print(e.code)
print(e.read())
return None
src = response.read().decode('utf-8')
return json.JSONDecoder().decode(src)
</code></pre>
<p>Finally our tests, just like before go in our closing <em>if</em> block</p>
<pre><code class="language-python"> if __name__ == "__main__":
import getpass
import datetime
api_url = input('ArchivesSpace API URL: ')
username = input('ArchivesSpace username: ')
password = getpass.getpass('ArchivesSpace password: ')
auth_token = login.login(api_url, username, password)
# Test create_acccession
print("Test create_accession()")
repo_id = int(input('What is the repository id (numeric): '))
print('Please provide the accession fields request')
title = input('title: ')
# This is the minimal Accession record
accession_model = {
'title': title,
'id_0': 'test_'+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'accession_date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
}
result = create_accession(api_url, auth_token, repo_id, accession_model)
print('Create accession result', json.dumps(result, indent=4))
</code></pre>
<p>Full listing <a href="accession.py">accession.py</a></p>
</section>
<script type="text/javascript" src="js/keyboard-nav.js"></script>
</body>
</html>