Is it possible to add a sub route in Python Pyramids Cornice (Rest inspired)

307 views Asked by At

I am using Cornice and Pyramids to build an API. Most of operation are REST operation so it is very adapted. But for some of them I don't want to follow REST principles.

For exemple in this class using cornice : How can we expose an action under : /gianluca/element/{id}/action.

I know I can had another class with path: /gianluca/element/{id}/action with a get or a post function. But I just would like to had this function in a clean way.

from cornice.resource import resource
from pyramid.security import Allow
from pyramid.security import Everyone

@resource(collection_path='/gianluca/element', path='/gianluca/element/{id}')
class MyElement(object):

    def __init__(self, request, context=None):
        self.request = request

    def __acl__(self):
        return [(Allow, Everyone, 'everything')]

    def collection_get(self):
        return {'collection': 'all collection'}

    def get(self):
        return "Element id : " + self.request.matchdict['id'] 

    # TODO
    def action(self):
        # elementLib.launchElement() 
        # Just a standart web service not in rest but i would like to have it under :
        # /gianluca/element/{id}/action
        return True
1

There are 1 answers

0
cole On

For such purposes you should use the cornice.Service API. The cornice.resource is for pure REST.