plone / plone.rest

Plone support for HTTP verbs (GET, POST, PUT, DELETE, ...)
https://pypi.org/project/plone.rest/
11 stars 5 forks source link

Issues with named services #37

Closed zopyx closed 8 years ago

zopyx commented 8 years ago

The changelog notes that it would be possible to use named services containing one or more slashes. I doubt that this is the case. First the unittests only test for named services without slashes.

My own test service

https://github.com/xml-director/xmldirector.crex/blob/master/xmldirector/crex/browser/configure.zcml#L140-L145

gives a 404 with a related unittest.

https://github.com/xml-director/xmldirector.crex/blob/master/xmldirector/crex/tests/test_api.py#L259-L268

Anyway...not sure how complicated it would be to provide a more flexible configuration for paths/named services as it is working in

https://github.com/collective/plone.jsonapi.core

In particular the definition of routes is implemented on top of 'werkzeug'

http://werkzeug.pocoo.org/docs/0.11/routing/#rule-format

buchi commented 8 years ago

It's possible to handle additional path segments after the service name but there is no default implementation for that. Instead the idea is that a service implementation should handle that itself.

Basically you have to implement IPublishTraverse in your service. Here's an example:

from plone.rest import Service
from zope.publisher.interfaces import IPublishTraverse
from zope.interface import implements

class NamedGet(Service):

    implements(IPublishTraverse)

    def __init__(self, context, request):
        super(NamedGet, self).__init__(context, request)
        self.params = []

    def publishTraverse(self, request, name):
        self.params.append(name)
        return self

    def render(self):
        return {'service': 'named get', 'params': self.params}

Additional path segments are stored ins self.params. There should be also some handling of invalid params e.g. by raising NotFound.

I'm not sure if we want to provide a basic implementation in plone.rest as there's always some additional code needed to handle those additional path segments. At least we should add an example in the documentation and tests.

zopyx commented 8 years ago

I am not demanding anything but I would except this level of support ootb.

2015-12-22 9:18 GMT+01:00 Thomas Buchberger notifications@github.com:

It's possible to handle additional path segments after the service name but there is no default implementation for that. Instead the idea is that a service implementation should handle that itself.

Basically you have to implement IPublishTraverse in your service. Here's an example:

from plone.rest import Servicefrom zope.publisher.interfaces import IPublishTraversefrom zope.interface import implements class NamedGet(Service):

implements(IPublishTraverse)

def __init__(self, context, request):
    super(NamedGet, self).__init__(context, request)
    self.params = []

def publishTraverse(self, request, name):
    self.params.append(name)
    return self

def render(self):
    return {'service': 'named get', 'params': self.params}

Additional path segments are stored ins self.params. There should be also some handling of invalid params e.g. by raising NotFound.

I'm not sure if we want to provide a basic implementation in plone.rest as there's always some additional code needed to handle those additional path segments. At least we should add an example in the documentation and tests.

— Reply to this email directly or view it on GitHub https://github.com/plone/plone.rest/issues/37#issuecomment-166550896.