4. Repositories

Update Repository

What functions have we implemented that are similar? What does the documentation suggest?

  1. copy create_repo function to update_repo adding it after list_repo
  2. We’ll also update our tests at the end of the file

Your code should wind up looking something like this.

In the definition section add

    def update_repo(api_url, auth_token, repo_id, repo):
        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)

In the test if block add

        # Test for update_repo()
        repos = list_repos(api_url, auth_token)
        print('Pick a repository id to update', json.dumps(repos, indent=4))
        repo_id = int(input('Repository numeric id: '))
        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", result)

Run this updated version and see how it works. Note you can also use the other methods in the module like list_repos() as well as list_repo().

Full listing repo.py