forked from rsdoiel/archivesspace-api-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path63-ArchivesSpace-API-Workshop.html
More file actions
113 lines (99 loc) · 3.93 KB
/
63-ArchivesSpace-API-Workshop.html
File metadata and controls
113 lines (99 loc) · 3.93 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!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="62-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="64-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>5. Working with Agents</h1>
<h2>create_agent implementation</h2>
<p>At the end of our imports block add</p>
<pre><code class="language-python"> import login
</code></pre>
<p>In our definition section add</p>
<pre><code class="language-python"> def agent_type_path(agent_type):
'''Map the agent type to a partial path'''
# agent_person agent_corporate_entity agent_software agent_family user
m = {
'agent_person': 'people',
'agent_corporate_entity': 'corporate_entities',
'agent_software': 'software',
'agent_family': 'families',
'user': 'user'
}
return '/agents/'+m[agent_type]
def create_agent(api_url, auth_token, agent_model):
'''create an agent and return the new agent record'''
data = json.JSONEncoder().encode(agent_model).encode('utf-8')
url = api_url+agent_type_path(agent_model['agent_type'])
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 in our <em>if</em> block we need some test code.</p>
<pre><code class="language-python"> # check mapping of agent type (e.g. agent_person) to path
print('Check that our mapping of agent type to path works')
url_test = agent_type_path('agent_person')
if url_test != '/agents/people':
print('ERROR: expected .../agents/people, found ', url_test)
sys.exit(0)
else:
print('agent_type_path() OK')
print('Testing create_agent')
# Here's our minimal fields
primary_name = input('Primary name (e.g. family name): ')
rest_of_name = input('Rest of name (e.g. first name): ')
agent_type = 'agent_person'
source = 'local'
rules = 'local'
# Our minimal agent record includes a :name_person and a :agent_person
# model
name_model = {
'primary_name': primary_name,
'rest_of_name': rest_of_name,
'name_order': 'inverted',
'jsonmodel_type': 'name_person',
'source': source,
'rules': rules,
'sort_name': primary_name+', '+rest_of_name,
'is_display_name': True,
}
agent_model = {
'jsonmodel_type': agent_type,
'title': primary_name+', '+rest_of_name,
'is_link_to_be_published': False,
'agent_type': agent_type,
'publish': False,
'display_name': name_model,
'names':[
name_model
]
}
# Now that we have a minimal record lets make a request
print("The minimum payload looks like ", json.dumps(agent_model, indent=4))
result = create_agent(api_url, auth_token, agent_model)
agent_id = result['id']
print('agent created response', json.dumps(result, 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>