Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend CKAN API?

Tags:

python

ckan

I would like to ask how can I extend CKAN's API by writing my own extension for CKAN. I could not find anything in the documentation. Could you give some simple example please?

like image 506
Kapucko Avatar asked Jan 20 '15 11:01

Kapucko


People also ask

What is dataset in Ckan?

For CKAN purposes, data is published in units called “datasets”. A dataset is a parcel of data - for example, it could be the crime statistics for a region, the spending figures for a government department, or temperature readings from various weather stations.


1 Answers

In the OP's defence, the documentation does seem to be a bit opaque. I've been looking at this in an attempt to get a custom API action for supplying JSON news feed to work, and finally came up with this:

import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit

# Required so that GET requests work
@toolkit.side_effect_free
def get_news(context,data_dict=None):
  # The actual custom API method
  return {"hello":"world"}


class CustomAPIPlugin(plugins.SingletonPlugin):
  plugins.implements(plugins.interfaces.IActions)

  def get_actions(self):
    # Registers the custom API method defined above
    return {'get_news': get_news}

The tutorial which describes creating an authentication plugin is here:

http://docs.ckan.org/en/latest/extensions/tutorial.html#creating-a-new-extension

What I've done is to plagiarise that, but using IActions rather than IAuthFunctions:

http://docs.ckan.org/en/latest/extensions/plugin-interfaces.html

It's working on an installation of CKAN 2.2.1.

like image 60
knirirr Avatar answered Nov 30 '22 14:11

knirirr