5. Working with Agents

list_agents implementation

  1. Add our list_agents() to our def section
  2. Update our tests in our if block

In our definition section add

    def list_agents(api_url, auth_token, agent_type):
       '''List all the agent ids of a given type'''
       data = urllib.parse.urlencode({'all_ids': True}).encode('utf-8')
       url = api_url+agent_type_path(agent_type)
       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)

In our if test block add

        # Test list_agents(), requires api_url, auth_token and agent_type
        print('Test list_agents()')
        agent_ids = list_agents(api_url, auth_token, 'agent_person')
        if len(agent_ids) < 1:
            print('ERROR: should have at least one agent!')
            sys.exit(0)
        print('agent ids ->', json.dumps(agent_ids, indent = 4))

Full listing agent.py