Giter VIP home page Giter VIP logo

ckanapi's Introduction

ckanapi

A thin wrapper around CKAN's action API

ckanapi may be used from within a plugin or separate from CKAN.

Making an API Request

import ckanapi
import pprint

demo = ckanapi.RemoteCKAN('http://demo.ckan.org')
groups = demo.action.group_list(id='data-explorer')
pprint.pprint(groups)

result:

{u'help': u'Return a list of the names of the site\'s ...
 u'result': [u'data-explorer',
             u'example-group',
             u'geo-examples',
             u'skeenawild'],
 u'success': True}

Failures are raised as exceptions just like when calling get_action from a plugin:

import ckanapi

demo = ckanapi.RemoteCKAN('http://demo.ckan.org', api_key='phony-key')
try:
    pkg = demo.action.package_create(name='my-dataset', title='not going to work')
except ckanapi.NotAuthorized:
    print 'denied'

result:

denied

A similar class is provided for accessing local CKAN instances from a plugin in the same way as remote CKAN instances. This class defaults to using the site user with full access.

import ckanapi

registry = ckanapi.LocalCKAN()
try:
    registry.action.package_create(name='my-dataset', title='this will work fine')
except ckanapi.ValidationError:
    print 'unless my-dataset already exists'

Customizing RemoteCKAN

The RemoteCKAN class may be passed a callable to use for making requests. This allows using a different library for the request:

import ckanapi
import requests

def requests_ftw(url, data, headers):
    r = requests.post(url, data, headers=headers)
    return r.status_code, r.text

demo = ckanapi.RemoteCKAN('http://demo.ckan.org', request_fn=requests_ftw)
groups = demo.action.group_list(id='data-explorer')

TestAppCKAN

A class is provided for making action requests to a paste.fixture.TestApp instance for use in CKAN tests:

import ckanapi
import paste.fixture

test_app = paste.fixture.TestApp(...)
demo = ckanapi.TestAppCKAN(test_app, api_key='my-test-key')
groups = demo.action.group_list(id='data-explorer')

ckanapi's People

Contributors

wardi avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.