简体   繁体   中英

How can I extend CKAN API?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM