Possible to register anonymous method for application session?

#1

Greetings,

I have an api which handles the execution of any rpc invocations (via the lovely crossbar:), so I can see my ApplicationSession class is going to be mostly boilerplate code consisting of registering rpc methods with api methods, like this:

class SensorAppSession(ApplicationSession):

@inlineCallbacks

def onJoin(self, details):

    self.api = api.Api(self)

    def get_sensor_config():

        response = self.api.call('get_sensor_config', self.context)

        return response.encode()

reg_config = yield self.api.register(get_sensor_config)

There’s a helper method in the api to use a convention based approach:

class Api(object):

def register(self, method):

name = ‘io.github.si618.pi-time.{}’.format(method.name)

return self.session.register(method, name)

What would be nice is to do this:

class Api(object):

def auto_register(self, method, context, params=None):

    reg = lambda name, context, params: self.call(method, context, params).encode()

    name = 'io.github.si618.pi-time.{}'.format(method)

    return self.session.register(reg, name)

Then my app session code just becomes a single line:

reg_config = yield self.api.auto_register(‘get_sensor_config’, self.context)

The idea being the registration process creates the anonymous method to make the rpc call via the api, as well as return and json encode the response (since this process is the same for all api methods)

Getting a runtime error back from the client, so I’m just not sure if my py-fu is not strong enough, or it’s not possible in autobahn…

No big deal though, would just be nice to keep the line count down…

cheers, si

0 Likes

#2

blush

Never mind, it was my weak py-fu, I wasn’t inspecting the client-side error message fully, which told me the lambda was incorrect:

class Api(object):

def __init__(self, config_file, app_session=None):

    ...

    self.session = app_session

def auto_register(self, method, context, params=None):

    reg = lambda: self.call(method, context, params).encode()

    name = 'io.github.si618.pi-time.{}'.format(method)

    return self.session.register(reg, name)

class SensorAppSession(ApplicationSession):

@inlineCallbacks

def onJoin(self, details):

    ...

    self.api = api.Api(config_file=config_file, app_session=self)

    reg_options = yield self.api.auto_register('get_sensor_options', self.context)

Which is working nicely :slight_smile:

As mentioned in a previous email thread last week, I’m new to python and autobahn/crossbar, so guidance welcome if this is considered a bad idea…

cheers, si

0 Likes

#3

*blush*

Never mind, it was my weak py-fu, I wasn't inspecting the client-side
error message fully, which told me the lambda was incorrect:

class Api(object):
     def __init__(self, config_file, app_session=None):
         ...
         self.session = app_session

     def auto_register(self, method, context, params=None):
         reg = lambda: self.call(method, context, params).encode()
         name = 'io.github.si618.pi-time.{}'.format(method)
         return self.session.register(reg, name)

class SensorAppSession(ApplicationSession):

     @inlineCallbacks
     def onJoin(self, details):
         ...
         self.api = api.Api(config_file=config_file, app_session=self)

         reg_options = yield
self.api.auto_register('get_sensor_options', self.context)

Which is working nicely :slight_smile:

As mentioned in a previous email thread last week, I'm new to python and
autobahn/crossbar, so guidance welcome if this is considered a bad idea...

You can use decorators to expose procedures:

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/basic/rpc/decorators/backend.py

This is the Pythonic way of doing stuff in declarative style ...

···

Am 15.09.2014 09:54, schrieb Si:

cheers, si

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/ee50163b-ff8f-49d9-b265-f5e292ec5b8a%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/ee50163b-ff8f-49d9-b265-f5e292ec5b8a%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#4

You can use decorators to expose procedures:

https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/basic/rpc/decorators/backend.py

This is the Pythonic way of doing stuff in declarative style …

Oh, I see, thanks, that’s even easier.

Got it working after figuring out I had to mark the decorator uri as unicode otherwise registration would fail.

@wamp.register(‘foo’)

def foo…

Throws:

File “C:\Python27\Lib\site-packages\autobahn\wamp\uri.py”, line 78, in init

assert(type(uri) == six.text_type)

But this works:

@wamp.register(u’foo’)

def foo…

Not sure why as the file is encoded as UTF-8, maybe a python 2 vs 3 thing?

cheers, si

0 Likes

#5

But this works:

@wamp.register(u'foo')
def foo...

Not sure why as the file is encoded as UTF-8, maybe a python 2 vs 3 thing?

Yes. Using explicit Unicode literals makes the code compatible with both Python 2 and 3.

0 Likes