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
/
49-ArchivesSpace-API-Workshop.html
69 lines (58 loc) · 2.51 KB
/
49-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
<!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="48-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="50-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>4. Repositories</h1>
<h2>Update Repository</h2>
<p>What functions have we implemented that are similar? What does the documentation
suggest?</p>
<ol>
<li>copy <em>create_repo</em> function to <em>update_repo</em> adding it after <em>list_repo</em></li>
<li>We’ll also update our tests at the end of the file</li>
</ol>
<p>Your code should wind up looking something like this.</p>
<p>In the definition section add</p>
<pre><code class="language-python"> def update_repo(api_url, auth_token, repo_id, repo):
data = json.JSONEncoder().encode(repo).encode('utf-8')
req = urllib.request.Request(
url = api_url+'/repositories/'+str(repo_id),
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 None
except urllib.error.URLError as e:
print(e.reason())
return None
src = response.read().decode('utf-8')
return json.JSONDecoder().decode(src)
</code></pre>
<p>In the test <em>if</em> block add</p>
<pre><code class="language-python"> # Test for update_repo()
repos = list_repos(api_url, auth_token)
print('Pick a repository id to update', json.dumps(repos, indent=4))
repo_id = int(input('Repository numeric id: '))
print('Getting repository record', repo_id)
repo = list_repo(api_url, auth_token, repo_id)
repo["name"] = input('old name is'+repo['name']+', provide a new name: ')
print('Now we update')
result = update_repo(api_url, auth_token, repo_id, repo)
print("Result is", result)
</code></pre>
<p>Run this updated version and see how it works. Note you can also use the other
methods in the module like <em>list_repos()</em> as well as <em>list_repo()</em>.</p>
<p>Full listing <a href="repo.py">repo.py</a></p>
</section>
<script type="text/javascript" src="js/keyboard-nav.js"></script>
</body>
</html>