4. Repositories

Notice how similar it is to our login function. Also how we add the header to pass along our auth_token with our request. Some of the important differences are

    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)

You can copy and paste your def from our text editor to the repl and make sure it compiles.