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
/
66-ArchivesSpace-API-Workshop.html
61 lines (53 loc) · 2.04 KB
/
66-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
<!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="65-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="67-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>5. Working with Agents</h1>
<h2>list_agents implementation</h2>
<ol>
<li>Add our list_agents() to our <em>def</em> section</li>
<li>Update our tests in our <em>if</em> block</li>
</ol>
<p>In our definition section add</p>
<pre><code class="language-python"> def list_agents(api_url, auth_token, agent_type):
'''List all the agent ids of a given type'''
data = urllib.parse.urlencode({'all_ids': True}).encode('utf-8')
url = api_url+agent_type_path(agent_type)
req = urllib.request.Request(
url = url,
data = data,
headers = {'X-ArchivesSpace-Session': auth_token},
method = 'GET')
try:
response = urllib.request.urlopen(req)
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>In our <em>if</em> test block add</p>
<pre><code class="language-python"> # Test list_agents(), requires api_url, auth_token and agent_type
print('Test list_agents()')
agent_ids = list_agents(api_url, auth_token, 'agent_person')
if len(agent_ids) < 1:
print('ERROR: should have at least one agent!')
sys.exit(0)
print('agent ids ->', json.dumps(agent_ids, indent = 4))
</code></pre>
<p>Full listing <a href="agent.py">agent.py</a></p>
</section>
<script type="text/javascript" src="js/keyboard-nav.js"></script>
</body>
</html>