2016-12-21 49 views
1

我试图更新CKAN的一个实例中的资源。我正在使用demo.ckan.org进行一些测试。使用python更新CKAN中的资源

我能够使用curl创建和更新数据集的资源,但使用python我无法做到这一点。

我的起点是此链接: http://docs.ckan.org/en/latest/maintaining/filestore.html#filestore-api

这是代码:

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'))]) 

我卷曲代码工作正常,所以我试图适应它:

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" 

这应该是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"}) 

我发现这个老链接: Create CKAN dataset using CKAN API and Python Requests library

在结束其建议添加一些信息,但我可以想出去做。

任何建议???

回答

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_code200,我响应了"success": true

请注意,{"Authorization: 97caad21-8632-4372-98fe-a24cdcaa90dc"}在你的代码的数据类型为class 'set'headers应该得到的class 'dict'数据,如"Authorization": "97caad21-8632-4372-98fe-a24cdcaa90dc"}

+0

真棒!!,感谢安德森 – davisoski

2

不要蟒蛇,请求麻烦 - 这是最容易在Python中使用优秀的ckanapi库。例如

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) 
+0

伟大的作品!,谢谢 – davisoski

+1

大,请使用赞成票,并选择最佳答案即可。 –

+0

我正在计算查看哪个选项更快但两者相同的时间 – davisoski