davisoski
davisoski

Reputation: 767

Update resource in CKAN using python

I'm trying to update a resource in one instace of CKAN. I'm using demo.ckan.org to make some test.

I'm able to create and update a resource for a dataset using curl, but using python I'm not be able to do this.

My start point is this link: http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api

This is the code:

import requests
requests.post('http://0.0.0.0:5000/api/action/resource_create',
              data={"package_id":"my_dataset"},
              headers={"X-CKAN-API-Key": "21a47217-6d7b-49c5-88f9-72ebd5a4d4bb"},
              files=[('upload', file('/path/to/file/to/upload.csv'))])

My curl code works ok, so I have tried to adapt it:

curl -X POST http://demo.ckan.org/api/3/action/resource_update  -d '{"id":"5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48", "url": "http://82.98.156.2/ckan_api/public_html/restaurantes.geojson", "name": "Better Restaurants", "format":"GEOJSON", "description":"Description of the resource"}' -H "Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"

This should be the code in python:

resource_dict = {
    'id': '5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48',
    'name':'REstaurantes con PYTHON',
    'url':'http://82.98.156.2/ckan_api/public_html/restaurantes.geojson',
    'description':'Description in  PYTHON'
}
resource_dict = urllib.quote(json.dumps(resource_dict))
requests.post('http://demo.ckan.org/api/3/action/resource_update',
              data=resource_dict,
              headers={"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"})

I have found this old link: Create CKAN dataset using CKAN API and Python Requests library

At the end it suggest to add some information, but I can figure out to do it.

Any suggestion???

Upvotes: 2

Views: 2178

Answers (2)

D Read
D Read

Reputation: 3224

Don't bother with python-requests - it's easiest in python to use the excellent ckanapi library. e.g.

import ckanapi
ckan = ckanapi.RemoteCKAN('http://demo.ckan.org/', apikey='97caad21-8632-4372-98fe-a24cdcaa90dc', user_agent='ckanapi so test')
resource_dict = {
    'id': '5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48',
    'package_id': 'cdcf576d-0b09-4df0-a506-61a7142d2b8f',
    'name':'Restaurantes con PYTHON',
    'url':'http://82.98.156.2/ckan_api/public_html/restaurantes.geojson',
    'description':'Description in  PYTHON',
    'format':'GEOJSON'
}
ckan.action.resource_update(**resource_dict)

Upvotes: 6

Andersson
Andersson

Reputation: 52665

This seem to work:

resource_dict = {'id': '5b75fdf2-df9c-4a4f-bb28-d78ea7bc4e48',
                 'name':'REstaurantes con PYTHON',
                 'url':'http://82.98.156.2/ckan_api/public_html/restaurantes.geojson',
                 'description':'Description in  PYTHON'}
requests.post('http://demo.ckan.org/api/3/action/resource_update',
          json=resource_dict,
          headers={"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"})

At least state_code is 200and I got "success": true in response.

Please note that {"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"} in your code is data type class 'set' while headers should get data of class 'dict', like "Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"}

Upvotes: 1

Related Questions