No changes to the imports but we’ll add a list_accessions function in our definition section.
def list_accessions(api_url, auth_token, repo_id):
'''List all the accessions for a given repo_id'''
data = urllib.parse.urlencode({'all_ids': True}).encode('utf-8')
url = api_url+'/repositories/'+str(repo_id)+'/accessions'
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)
And our test in the if block
# Test list_accessions
print('Test list_accessions()')
accession_ids = list_accessions(api_url, auth_token, repo_id)
print('Accession IDS', json.dumps(accession_ids, indent=4))
Full listing accession.py