Search code examples
pythonrestcherrypy

Cherrypy REST tutorial returns TypeError: _expose() takes exactly 1 argument


Trying to follow tutorial 7 on cherrypy documentation page for creating a REST style API. Copy-pasted code from tutorial,

import random
import string

import cherrypy

@cherrypy.expose
class StringGeneratorWebService(object):

    @cherrypy.tools.accept(media='text/plain')
    def GET(self):
        return cherrypy.session['mystring']

    def POST(self, length=8):
        some_string = ''.join(random.sample(string.hexdigits, int(length)))
        cherrypy.session['mystring'] = some_string
        return some_string

    def PUT(self, another_string):
        cherrypy.session['mystring'] = another_string

    def DELETE(self):
        cherrypy.session.pop('mystring', None)


if __name__ == '__main__':
    conf = {
        '/': {
            'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
            'tools.sessions.on': True,
            'tools.response_headers.on': True,
            'tools.response_headers.headers': [('Content-Type',    'text/plain')],
        }
    }
    cherrypy.quickstart(StringGeneratorWebService(), '/', conf)

but when run am given the error

File "H:/researchInstrumentCatalog/dqapi/core/test.py", line 36, in <module>
cherrypy.quickstart(test(), '/', conf)
TypeError: expose_() takes exactly 1 argument (0 given) 

I am running cherrypy 3.8, thus this question has not been helpful


Solution

  • In the question that you linked, I mentioned that it was added that the support to decorate classes with cherrypy.expose was added in CherryPy version 6.

    You're are using 3.8.

    Upgrade CherryPy to any version after 6.0, or just don't use the expose decorated and set a property of exposed = True.

    class StringGeneratorWebService(object):
    
        # this is the attribute that configured the expose decorator
        exposed = True
    
        @cherrypy.tools.accept(media='text/plain')
        def GET(self):
            return cherrypy.session['mystring']
    
        def POST(self, length=8):
            some_string = ''.join(random.sample(string.hexdigits, int(length)))
            cherrypy.session['mystring'] = some_string
            return some_string
    
        def PUT(self, another_string):
            cherrypy.session['mystring'] = another_string
    
        def DELETE(self):
            cherrypy.session.pop('mystring', None)