6. Accessions

create_accession implementation

This process works the same as for agent so I’m jumping straight to the code.

This import block is the same one we used with agents.

    #!/usr/bin/env python3
    import urllib.request
    import urllib.parse
    import urllib.error
    import json
    # Local modules
    import login

In our definition section we’ll create a create_accession function.

    def create_accession(api_url, auth_token, repo_id, accession_model):
        '''create an accession returning a status object'''
        data = json.JSONEncoder().encode(accession_model).encode('utf-8')
        url = api_url+'/repositories/'+str(repo_id)+'/accessions'
        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)

Finally our tests, just like before go in our closing if block

    if __name__ == "__main__":
        import getpass
        import datetime
        api_url = input('ArchivesSpace API URL: ')
        username = input('ArchivesSpace username: ')
        password = getpass.getpass('ArchivesSpace password: ')
        auth_token = login.login(api_url, username, password)
    
        # Test create_acccession
        print("Test create_accession()")
        repo_id = int(input('What is the repository id (numeric): '))
    
        print('Please provide the accession fields request')
        title = input('title: ')
    
        # This is the minimal Accession record
        accession_model = {
            'title': title,
            'id_0': 'test_'+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            'accession_date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        }
        result = create_accession(api_url, auth_token, repo_id, accession_model)
        print('Create accession result', json.dumps(result, indent=4))

Full listing accession.py