Update resource in CKAN using python

吃可爱长大的小学妹 提交于 2019-12-24 05:45:13

问题


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???


回答1:


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)



回答2:


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"}



来源:https://stackoverflow.com/questions/41270003/update-resource-in-ckan-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!