開發手冊
CKAN API 說明
ckan 主要使用 restful api 模式進行,進行呼叫時可以用 http/https 送出需求,ckan 主機會回傳 json 格式字串,例如呼叫 group_list API 的時候,以瀏覽器輸入
https://scidm.nchc.org.tw/api/3/action/group_list
可以取得回傳值:
{
"help": "https://scidm.nchc.org.tw/api/3/action/help_show?name=group_list",
"result": [
"nchc",
"most",
"privateset"
],
"success": true
}
回傳內容包含有 'help', 'result', 'success' ,其中說明如下:
- help : 顯示相關 API 文件
- success : 回傳狀態, true 表示成功,false 表示失敗
- result : API 輸出內容
- error : 當回傳值 success 為 false 時,會有 error 字串顯示錯誤訊息
例如:
{
"help": "Creates a package",
"success": false,
"error": {
"message": "Access denied",
"__type": "Authorization Error"
}
}
Python + urllib 進行開發
範例:
#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint
# Make the HTTP request.
response = urllib2.urlopen('https://scidm.nchc.org.tw/api/3/action/group_list', data_string)
assert response.code == 200
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.read())
# Check the contents of the response.
assert response_dict['success'] is True
result = response_dict['result']
pprint.pprint(result)
Python + ckanapi 進行開發
需要先以 pip 安裝 ckanapi,其主要是為了ckan而設計的一系列函式庫,能夠提供開發者以較好的可讀性、除錯能力、支援性等優點,安裝與使用方式如下:
pip install ckanapi
範例:
from ckanapi import RemoteCKAN
ua = 'ckanapiexample/1.0 (+http://example.com/my/website)'
demo = RemoteCKAN('https://scidm.nchc.org.tw', user_agent=ua)
groups = demo.action.group_list(id='nchc')
print groups