Web services with turbogears2

143 views
Skip to first unread message

damien....@free.fr

unread,
Mar 28, 2014, 12:22:10 PM3/28/14
to turbo...@googlegroups.com
Hi all,

I'm about to start a new project which is a web application mostly based on a server-side application implementing a REST/Json API and a database connection (through SQLAlchemy). I was thinking of using WSME for the webservice layer (formatting output + data validation) but it seems that it does not support TG2.

My question is: is there a simple way to implement a REST/Json api with TG2 (with data validation) ? If not, what framework would you recommend to do so ? I'm looking for a full-stack framework with features like user rights management (like TG2 do).

Thanks in advance.

Alessandro Molina

unread,
Mar 28, 2014, 1:45:15 PM3/28/14
to TurboGears .
Actually TG works pretty well for REST API due to built-in support for JSON, RestController and @validate

There is also a tutorial on rapid-prototyping REST API using the EasyCrudRestController which shows how to setup an autogenerated rest api with minimum effort: http://turbogears.readthedocs.org/en/latest/cookbook/Crud/restapi.html



--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+...@googlegroups.com.
To post to this group, send email to turbo...@googlegroups.com.
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.

damien....@free.fr

unread,
Mar 31, 2014, 5:22:11 AM3/31/14
to turbo...@googlegroups.com
Thanks for your answer, Alessandro. Another question about the @validate decorator. What I like with the WSME webservice layer is that it allows to define structures to be used for validation. For example, I create a class WikiPage with required properties, and when the client sends a request in json format eg { "title": "blabla", "content": "<html>this is the wiki page body </html>"}, WSME will try to match my model class with the json sctructure.

hm... actually, I looked at http://turbogears.org/2.1/docs/main/Validation.html
and I believe I can build classes like this:

WikiPageValidator(object):
todict(self):
return {
"title": validators.UnicodeString,
"content": validators.UnicodeString,
}

from formencode import validators

@expose('json')
@validate(validators=WikiPageValidator.todict())
def two_validators(self, a=None, b=None, *args):
errors = [{key, value} in pylons.c.form_errors.iteritems()]
values = pylons.c.form_values
return dict(a=a, b=b, errors=str(errors), values=str(values))


What do you think ? Is it a good way to use it ?

Damien

----- Mail original -----
De: "Alessandro Molina" <alessand...@gmail.com>
À: "TurboGears ." <turbo...@googlegroups.com>
Envoyé: Vendredi 28 Mars 2014 18:45:15
Objet: Re: [TurboGears] Web services with turbogears2

damien....@free.fr

unread,
Mar 31, 2014, 5:28:28 AM3/31/14
to turbo...@googlegroups.com
I fix the classes I gave as example:

# The validation model
WikiPageValidator(object):
todict(self):
return {
"title": validators.UnicodeString,
"content": validators.UnicodeString,
}

from formencode import validators

# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPageValidator.todict())
def post(self, title=None, content=None, *args):
errors = [{key, value} in pylons.c.form_errors.iteritems()]
values = pylons.c.form_values
return dict(a=a, b=b, errors=str(errors), values=str(values))

What would be wonderfull would be to define controllers with given Validator as controller parameters, eg:


# The validation model
WikiPage(object):
title = validators.UnicodeString
content = validators.UnicodeString


from formencode import validators

# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPage)
def post(self, workload = WikiPage()):
...
...
return dict(workload = workload)

Do you think it's easy to implement? Maybe there is already something that would act similarly to this ? Maybe a decorator that would map every attribute to the same class member of the given model ?

----- Mail original -----
De: "damien accorsi" <damien....@free.fr>
À: turbo...@googlegroups.com
Envoyé: Lundi 31 Mars 2014 11:22:11

Alessandro Molina

unread,
Mar 31, 2014, 8:28:05 AM3/31/14
to TurboGears .
On Mon, Mar 31, 2014 at 11:28 AM, <damien....@free.fr> wrote:
I fix the classes I gave as example:

# The validation model
WikiPageValidator(object):
  todict(self):
    return {
      "title": validators.UnicodeString,
      "content": validators.UnicodeString,
    }

from formencode import validators

# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPageValidator.todict())
def post(self, title=None, content=None, *args):
    errors = [{key, value} in pylons.c.form_errors.iteritems()]
    values =  pylons.c.form_values
    return dict(a=a, b=b, errors=str(errors), values=str(values))


Why not just inherit from a formencode.Schema? It would do the same thing without the need to call todict().
It's actually meant for that.

 
What would be wonderfull would be to define controllers with given Validator as controller parameters, eg:

# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPage)
def post(self, workload = WikiPage()):
    ...
    ...
    return dict(workload = workload)

Do you think it's easy to implement? Maybe there is already something that would act similarly to this ? Maybe a decorator that would map every attribute to the same class member of the given model ?

I didn't understand :D
Can you explain it again?

Craig Small

unread,
Mar 31, 2014, 8:09:06 PM3/31/14
to turbo...@googlegroups.com
On Fri, Mar 28, 2014 at 05:22:10PM +0100, damien....@free.fr wrote:
> My question is: is there a simple way to implement a REST/Json api with TG2 (with data validation) ? If not, what framework would you recommend to do so ? I'm looking for a full-stack framework with features like user rights management (like TG2 do).
Isn't that what the backend of things like jqgrid does already?
ie it calls http://server/blah/thing.json and you get all the data
nicely JSON formatted for the thing model.

Permissions and all the other TG goodies don't care what the output
format is for this request, so they just work.

- Craig

--
Craig Small (@smallsees) http://enc.com.au/ csmall at : enc.com.au
Debian GNU/Linux http://www.debian.org/ csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5

Damien Accorsi

unread,
Apr 1, 2014, 8:27:39 AM4/1/14
to turbo...@googlegroups.com
Le 01/04/2014 02:09, Craig Small a écrit :
> On Fri, Mar 28, 2014 at 05:22:10PM +0100, damien....@free.fr wrote:
>> My question is: is there a simple way to implement a REST/Json api with TG2 (with data validation) ? If not, what framework would you recommend to do so ? I'm looking for a full-stack framework with features like user rights management (like TG2 do).
> Isn't that what the backend of things like jqgrid does already?
> ie it calls http://server/blah/thing.json and you get all the data
> nicely JSON formatted for the thing model.
>
> Permissions and all the other TG goodies don't care what the output
> format is for this request, so they just work.
>
> - Craig
Alessandro pointed me to this resource :
http://turbogears.readthedocs.org/en/latest/cookbook/Crud/restapi.html
So... building rest/json is ok.

What about api auto-generated documentation?

Damien

Alessandro Molina

unread,
Apr 1, 2014, 8:39:36 AM4/1/14
to TurboGears

Far from being perfect but I use https://pypi.python.org/pypi/sphinxcontrib-jsoncall which autogenerates doc for all json controllers. Just remember to skip /admin

--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email to turbogears+unsubscribe@googlegroups.com.

Damien Accorsi

unread,
Apr 1, 2014, 8:49:34 AM4/1/14
to turbo...@googlegroups.com
Thanks, Alessandro. This is what I was looking for.

Damien
>> email to turbogears+...@googlegroups.com.

Christophe de Vienne

unread,
Apr 2, 2014, 11:43:03 AM4/2/14
to turbo...@googlegroups.com
Hi,

Adding TG2 support in WSME should be relatively easy, I just did not have time to do it.

If you want to give it a try, the pecan adapter should be a good start, but checkout the python-wsme group first, as some patches were recently proposed to support TG2.

Cheers,

Christophe

Damien Accorsi

unread,
May 30, 2014, 7:16:41 AM5/30/14
to turbo...@googlegroups.com
Hi all,

According to what you told me a few weeks ago, Alessandro, I'm about to
inspire from the following documentation for implementing a REST api :
http://turbogears.readthedocs.org/en/latest/cookbook/Crud/restapi.html

My question is: I'd like to define list of attributes (and potentially
renaming of attribute) to be exposed through my API. They are not fixed
for a given model, for example :

- in case of a select filler, I want User as { "user_id" : 4, "name":
"Jean Dujardin" }
- in case of showing user list I want User as { "user_id" : 4, "name":
"Jean Dujardin", "email" : "jean.d...@hotmail.com", "status":
"active" } etc, etc.

I also may need to rename some attributes like display_name to name.

There is a really easy way to do this using symfony2/php using @expose
annotations. I was asking myself if there is something similar in
turbogears ?

For example, my User Class would be :

class User(object):

@expose(["users", "user", "user_select_filler"])
user_id = Column(...)

@expose(["users", "user", "user_select_filler"])
display_name = Column(...)

@expose(["users", "user"])
email = Column(...)


and in my controller :

@exposeJson('user_select_filler')
def get_select_filler(self):
return DBSession.query(User).all()

and this would automatically expose a list of user with given properties
only : { user_id, display_name }

Something like this is possible ?
Reply all
Reply to author
Forward
0 new messages