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 path52-ArchivesSpace-API-Workshop.html
193 lines (174 loc) · 7.05 KB
/
52-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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!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="51-ArchivesSpace-API-Workshop.html" title="Previous slide">Prev</a>
<a id="next-slide" rel="nav" href="53-ArchivesSpace-API-Workshop.html" title="Next slide">Next</a>
</nav>
<section><h1>4 Repositories</h1>
<h2>Our complete second module</h2>
<p>Full listing <a href="repo.py">repo.py</a></p>
<pre><code class="language-python"> #!/usr/bin/env python3
import urllib.request
import urllib.parse
import urllib.error
import json
# Here's our own login module
import login
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)
def list_repos(api_url, auth_token):
'''List all the repositories, return the listing object'''
req = urllib.request.Request(
url = api_url+'/repositories',
data = None,
headers = {'X-ArchivesSpace-Session': auth_token})
try:
response = urllib.request.urlopen(req)
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)
def list_repo(api_url, auth_token, repo_id):
'''List all the repositories, return the listing object'''
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)
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)
def update_repo(api_url, auth_token, repo_id, repo):
'''This function sends a updates a repository via ArchivesSpace REST API'''
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)
def delete_repo(api_url, auth_token, repo_id):
'''Delete a repository via ArchivesSpace REST API, returns status code 200 on success'''
req = urllib.request.Request(
url = api_url+'/repositories/'+str(repo_id),
data = None,
headers = {'X-ArchivesSpace-Session': auth_token},
method = 'DELETE')
try:
response = urllib.request.urlopen(req)
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)
if __name__ == '__main__':
import getpass
# Test login
print("Testing login()")
api_url = input('ArchivesSpace API URL: ')
username = input('ArchivesSpace username: ')
password = getpass.getpass('ArchivesSpace password: ')
print('Logging in', api_url)
auth_token = login.login(api_url, username, password)
print("auth token", auth_token)
if auth_token != '':
print('Success!')
else:
print('Ooops! something went wrong')
sys.exit(1)
# Test create_repo
print('Testing create_repo()')
print('Create your first repository')
name = input('Repo name: ')
repo_code = input('Repo code: ')
repo = create_repo(api_url, auth_token, name, repo_code)
print(json.dumps(repo, indent=4))
print('Create a second repository')
name = input('Repo name: ')
repo_code = input('Repo code: ')
repo = create_repo(api_url, auth_token, name, repo_code)
print(json.dumps(repo, indent=4))
# Test list_repos
print('Testing list_repos()')
repos = list_repos(api_url, auth_token)
print('repositores list', json.dumps(repos, indent=4))
# Test list_repo
print('Testing list_repo()')
repo_id = int(input('Enter repo id: '))
repo = list_repo(api_url, auth_token, repo_id)
print('repository list', json.dumps(repo, indent=4))
# Test update_repo
print('Testing update_repo()')
repo_id = int(input('Repository numeric id to update: '))
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', json.dumps(result, indent=4))
# Test delete_repo
print('Testing delete_repo()')
repo_id = int(input('Repository numeric id to delete: '))
result = delete_repo(api_url, auth_token, repo_id)
print('Result is', json.dumps(result, indent=4))
# All done!
print('Success!')
</code></pre>
<p>Full listing <a href="repo.py">repo.py</a></p>
</section>
<script type="text/javascript" src="js/keyboard-nav.js"></script>
</body>
</html>