5. Working with Agents

Delete an Agent

The delete agent should look familiar compared to delete_repo(), We need agent_type in addition to agent_id.

In the definition section add

    def delete_agent(api_url, auth_token, agent_type, agent_id):
       '''delete an agent by agent_type and agent_id'''
       url = api_url+agent_type_path(agent_type)+'/'+str(agent_id)
       req = urllib.request.Request(
          url = url,
          data = None,
          headers = {'X-ArchivesSpace-Session': auth_token},
          method = 'DELETE')
       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 the test if block add

       # Test delete_agent()
       print('Testing delete_repo()')
       agent_id = int(input('Agent numeric id to delete: '))
       result = delete_agent(api_url, auth_token, agent_type, agent_id)
       print('Result is', json.dumps(result, indent=4))

Full listing agent.py