[英]Update resource in CKAN using python
I'm trying to update a resource in one instace of CKAN. 我正在尝试在CKAN的一个实例中更新资源。 I'm using demo.ckan.org to make some test.
我正在使用demo.ckan.org进行测试。
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. 我可以使用curl创建和更新数据集的资源,但是使用python则无法做到这一点。
My start point is this link: http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api 我的起点是这个链接: 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代码可以正常运行 ,因此我尝试对其进行调整:
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: 这应该是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 我找到了这个旧链接: 使用CKAN API和Python Requests库创建CKAN数据集
At the end it suggest to add some information, but I can figure out to do it. 最后,它建议添加一些信息,但我可以弄清楚。
Any suggestion??? 有什么建议吗?
Don't bother with python-requests - it's easiest in python to use the excellent ckanapi library. 不用理会python请求-在python中使用出色的ckanapi库是最容易的。 eg
例如
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)
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 200
and I got "success": true
in response. 至少
state_code
是200
,我得到了"success": true
。
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"}
请注意,您代码中的
{"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"}
是数据类型class 'set'
而headers
应获取class 'dict'
数据,例如"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.