TG2 Webservice/XML-RPC

13 views
Skip to first unread message

Jd

unread,
Sep 12, 2009, 8:55:57 PM9/12/09
to TurboGears
Hi
Does anyone know how to go about exposing controller methods as
WebServices or XML-RPC with TG2 ?
We have ajax client that accesses data from TG2 application. Would
like to simply put some decorators/annotations to enable webservice/
xml-rpc.

There is a mention about SOAP/XML-RPC as follows...

"You could imagine using this capability to expose your application’s
resources for SOAP, or XML-RPC."

on the doc page...

http://turbogears.org/2.0/docs/main/ResponseTypes.html#custom-content-types

Can some one elaborate bit more.. and work involved. Better if someone
has already done some work along the line.

I have seen a project on google-code that works with TG1.. but it
seems it is not easy to port it to TG2 (according to the lead
developer).

Thanks
/Jd

Crusty

unread,
Sep 12, 2009, 9:22:38 PM9/12/09
to turbo...@googlegroups.com
Hi there,

you might wanna check this out:

http://turbogears.org/2.0/docs/modules/pylons/controllers_xmlrpc.html

An example of an XMLRPCController will follow, but one word of caution before:
As far as I know, TG2 does not support object dispatch for
XPCControllers, so you need to forward manually in your RootController
like so:

# --- root.py --- #
from myproject.controllers.xmlrpc import MyXMLRPCController

@expose()
def xmlrpc(self, *args, **kw):
return forward(MyXMLRPCController())



# --- xmlrpc.py --- #

from pylons.controllers import XMLRPCController
import xmlrpclib

class MyXMLRPCController(XMLRPCController):

def myfunc_here(self, param1=None):

if param1 is None:
return xmlrpclib.Fault(0, "Parameter Error")

return "Success"

greets,

Tom

Jd

unread,
Sep 13, 2009, 11:14:07 AM9/13/09
to TurboGears
Thanks for pointing this out.

-- Can this be improved / wrapped in a decorator ?
-- Can I use this with my existing controller methods.. that exposes
json. ?

@expose('json')
foo()
{
...
}

/Jd

On Sep 12, 6:22 pm, Crusty <crust...@gmail.com> wrote:
> Hi there,
>
> you might wanna check this out:
>
> http://turbogears.org/2.0/docs/modules/pylons/controllers_xmlrpc.html
>
> An example of an XMLRPCController will follow, but one word of caution before:
> As far as I know, TG2 does not support object dispatch for
> XPCControllers, so you need to forward manually in your RootController
> like so:
>
> # --- root.py --- #
> from myproject.controllers.xmlrpc import MyXMLRPCController
>
> @expose()
> def xmlrpc(self, *args, **kw):
>         return forward(MyXMLRPCController())
>
> # --- xmlrpc.py --- #
>
> from pylons.controllers import XMLRPCController
> import xmlrpclib
>
> class MyXMLRPCController(XMLRPCController):
>
>     def myfunc_here(self, param1=None):
>
>         if param1 is None:
>             return xmlrpclib.Fault(0, "Parameter Error")
>
>         return "Success"
>
> greets,
>
> Tom
>
> On Sun, Sep 13, 2009 at 2:55 AM, Jd <jdsw2...@yahoo.com> wrote:
>
> > Hi
> >   Does anyone know how to go about exposing controller methods as
> > WebServices or XML-RPC with TG2 ?
> >   We have ajax client that accesses data from TG2 application. Would
> > like to simply put some decorators/annotations to enable webservice/
> > xml-rpc.
>
> > There is a mention about SOAP/XML-RPC as follows...
>
> > "You could imagine using this capability to expose your application’s
> > resources for SOAP, or XML-RPC."
>
> > on the doc page...
>
> >http://turbogears.org/2.0/docs/main/ResponseTypes.html#custom-content...

Crusty

unread,
Sep 14, 2009, 4:28:31 AM9/14/09
to turbo...@googlegroups.com
Hey there,

well, as you said you want AJAX, i did not suggest JSON, because AJAX
is XML, and not Javascript, so of couse suggesting exposing as JSON
would be totally useless for your problem.
If JSON is an option for you, i definitly recommend it over
XMLRPCController because of its brevity (simply expose('json'))

And concerning the XMLRPCController, I dont believe that you can
prettify it with decorators, because it actually is kinda something
like a nested controller, with explicit forwarding. Good thing is,
you have most of your XMLRPC Code in one central, seperate spot.
Improves maintainability for the future ;)

greetings, tom

Diez B. Roggisch

unread,
Sep 14, 2009, 11:27:56 AM9/14/09
to turbo...@googlegroups.com
Jd schrieb:

> Thanks for pointing this out.
>
> -- Can this be improved / wrapped in a decorator ?

No. Because the action of a controller is mapped through an URL, and the
method of a XMLRPC-call is given as part of the request-body. And there
is no sensible way to map things onto each other.

> -- Can I use this with my existing controller methods.. that exposes
> json. ?
>
> @expose('json')
> foo()
> {
> ...
> }

You certainly can call a helper-method or function from both
implementations, thus not violating DRY-principles.

Diez

Jd

unread,
Sep 16, 2009, 4:15:07 PM9/16/09
to TurboGears
Sorry Tom, I was not accurate enough.

Thanks to both of you for clarifying.

Would something like following worth trying out ?
-- Root Controller -- dispatches to xmlrpc controller (as per Tom)
-- Use the xmlrpc to invoke method in root controller. i.e.
xmlrpc_controller.foo ==> root_controller.foo
-- Use some decorator similar to json for taking the return value and
converting it to XML-RPC response? May be some method from XMLRPC
Controller.

/Jd

Diez B. Roggisch

unread,
Sep 16, 2009, 4:22:34 PM9/16/09
to turbo...@googlegroups.com
Jd schrieb:

> Sorry Tom, I was not accurate enough.
>
> Thanks to both of you for clarifying.
>
> Would something like following worth trying out ?
> -- Root Controller -- dispatches to xmlrpc controller (as per Tom)
> -- Use the xmlrpc to invoke method in root controller. i.e.
> xmlrpc_controller.foo ==> root_controller.foo

Not really possible, as you don't get a hold on the root-controller
easily. If any.

Instead, delegate to some common subclass, or a bunch of functions.

> -- Use some decorator similar to json for taking the return value and
> converting it to XML-RPC response? May be some method from XMLRPC
> Controller.

What conversion? JSON-decorated actions return dicts, XMLRPC groks dicts.

Diez

Jd

unread,
Sep 17, 2009, 4:51:31 PM9/17/09
to TurboGears
Hi Diez,
Thanks for your input..

So your suggestion is to...

Have two controllers... one for json and another for xmlrpc.
And the controller methods from each controller would use a common
class(es) that exposes application specific methods.

for eg.

class Handler():
def foo(a,b,c):
return { "foo" : "bar" }

Right?

btw, found a related post here...
http://docs.pythonweb.org/display/pylonscookbook/Making+a+SOAP+Controller+with+Optio%27s+Soaplib

/Jd
Reply all
Reply to author
Forward
0 new messages