我正在尝试在CKAN的一个实例中更新资源。我正在使用demo.ckan.org进行一些测试。
我可以使用 curl 为数据集创建和更新资源,但是使用python我无法做到这一点。
我的起点是这个链接: http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api
这是代码:
idMap
我的卷曲代码运行正常 ,所以我尝试调整它:
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'))])
这应该是python中的代码:
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"
我找到了这个旧链接: Create CKAN dataset using CKAN API and Python Requests library
最后它建议添加一些信息,但我可以想出去做。
任何建议???
答案 0 :(得分:5)
不要打扰python请求 - 在python中使用优秀的ckanapi库是最简单的。 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)
答案 1 :(得分:1)
这似乎有效:
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"})
至少state_code
为200
,我得到"success": true
作为回应。
请注意,代码中的{"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"}
数据类型为class 'set'
,而headers
应获取class 'dict'
的数据,如"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"}