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 path38-ArchivesSpace-API-Workshop.html
57 lines (52 loc) · 2.25 KB
/
38-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
<!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="37-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="39-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>4. Repositories</h1>
<p>Notice how similar it is to our <strong>login</strong> function. Also how we
add the header to pass along our <em>auth_token</em> with our request.
Some of the important differences are</p>
<ul>
<li>We’re encoding <em>data</em> as JSON instead of urlencoding.</li>
<li>We’ve added an <em>auth_token</em> as a header parameter in the <em>Request</em></li>
<li><em>data</em> is passed via <em>urlopen</em> rather than in our <em>Request</em> object</li>
</ul>
<pre><code class="language-python"> def create_repo(api_url, auth_token, name, repo_code, org_code = '', image_url = '', url = ''):
'''This function sends a create request to the ArchivesSpace REST API'''
data = json.JSONEncoder().encode({
'jsonmodel_type': 'repository',
'name': name,
'repo_code': repo_code,
'org_code': org_code,
'image_url': image_url,
'url': url
}).encode('utf-8')
req = urllib.request.Request(
url = api_url+'/repositories',
data = None,
headers = {'X-ArchivesSpace-Session': auth_token})
try:
response = urllib.request.urlopen(req, data)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read())
return ""
except urllib.error.URLError as e:
print(e.reason())
return ""
src = response.read().decode('utf-8')
return json.JSONDecoder().decode(src)
</code></pre>
<p>You can copy and paste your def from our text editor to the repl and make
sure it compiles.</p>
</section>
<script type="text/javascript" src="js/keyboard-nav.js"></script>
</body>
</html>