Search code examples
djangoapidjango-viewsactivecollab

Deleting object in django and API call


I am trying to delete a client object in my program and then also delete the object in activeCollab using the API provided. I can delete the object but I keep getting a 404 error when it calls the API. I did a print for c.id and I am getting the correct ID, and if I replace ':company_id' in the req statement with the actual ID of the client, it works.

Here is my code for the delete:

def deleteClient(request, client_id):
   c = get_object_or_404(Clients, pk = client_id)
   #adding the params for the request to the aC API
   params = urllib.urlencode({
     'submitted':'submitted',
     'company[id]': c.id,   
   })
   #make the request
   req = urllib2.Request("http://website_url/public/api.php?path_info=/people /:company_id/delete&token=XXXXXXXXXXXXXXXXXXXX", params)
   f = urllib2.urlopen(req)
   print f.read()
   c.delete()
   return HttpResponseRedirect('/clients/')

Thanks everyone.

Oh here is the link to the API documentation for the delete: http://www.activecollab.com/docs/manuals/developers/api/companies-and-users


Solution

  • From the docs it appears that :company_id is supposed to be replaced by the actual company id. This replacement won't happen automatically. Currently you are sending the company id in the POST parameters (which the API isn't expecting) and you are sending the literal value ':company_id' in the query string.

    Try something like:

    url_params=dict(path_info="/people/%s/delete" % c.id, token=MY_API_TOKEN)
    data_params=dict(submitted=submitted)
    req = urllib2.Request(
        "http://example.com/public/api.php?%s" % urllib.urlencode(url_params), 
        urllib.urlencode(data_params)
        )
    

    Of course, because you are targeting this api.php script, I can't tell if that script is supposed to do some magic replacement. But given that it works when you manually replace the :company_id with the actual value, this is the best bet, I think.