6. Accessions

list_accession implementation

If we know the repo_id and accession_id we can list the details of an accession. Note the accession_id is the numeric value seen in the URL (or uri field of an accession record) and NOT related to the fields id_0, id_1, id_2, or id_3.

    def list_accession(api_url, auth_token, repo_id, accession_id):
        '''List an accession by repo_id and accession_id'''
        url = api_url+'/repositories/'+str(repo_id)+'/accessions/'+str(accession_id)
        req = urllib.request.Request(
             url = url,
             data = None,
             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_accession
        print('Test list_accession()')
        accession_id = int(input('Enter a numeric accession id: '))
        accession_model = list_accession(api_url, auth_token, repo_id, accession_id)
        print('Accession model', json.dumps(accession_model, indent=4))

Full listing accession.py