Google Groups

Re: video about new RESTful APIs


Massimo Di Pierro Mar 17, 2011 12:58 PM
Posted in group: web2py-users
This is now in trunk. Some API changes are still possible.

Right now it supports fiels of type, id, integer, double, date,
datetime, time, boolean, reference, list and string fields with "slug"
in the name.

If you have a model like this

db.define_table('person',Field('name'),Field('birthdate','date'),Field('age','integer'),Field('married','boolean'),Field('tags','list:string'))
db.define_table('dog',Field('name'),Field('owner',db.person),Field('info','text'))

simply add this to your model

@request.restful()
def api():
    def GET(*args,**vars):
        parsed = db.parse_as_rest('auto',args,vars)
        if parsed.status==200: return dict(content=parsed.response)
        raise HTTP(parsed.status,parsed.error)
    return locals()

and it will expose APIs like

/yourapp/default/api/person/id/5
/yourapp/default/api/person/married/true
/yourapp/default/api/person/birthdate/2008
/yourapp/default/api/person/age/10/30     # notice it is a range
/yourapp/default/api/person/id/5/name     # notice a field
/yourapp/default/api/person/id/5/dog      # notice a reference (his
dogs)

Here is a complete list of the APIs automatically generated.

/person/id/{person.id}
/person/id/{person.id}/:field
/person/id/{person.id}/dog[dog.owner]
/person/id/{person.id}/dog[dog.owner]/id/{dog.id}
/person/id/{person.id}/dog[dog.owner]/id/{dog.id}/:field
/person/id/{person.id}/dog[dog.owner]/owner/{dog.owner}
/person/id/{person.id}/dog[dog.owner]/owner/{dog.owner}/:field
/person/name/dog[dog.owner]
/person/name/dog[dog.owner]/id/{dog.id}
/person/name/dog[dog.owner]/id/{dog.id}/:field
/person/name/dog[dog.owner]/owner/{dog.owner}
/person/name/dog[dog.owner]/owner/{dog.owner}/:field
/person/birthdate/{person.birthdate.year}
/person/birthdate/{person.birthdate.year}/:field
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}
/person/birthdate/{person.birthdate.year}/
{person.birthdate.month}/:field
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/:field
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/dog[dog.owner]
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/dog[dog.owner]/id/{dog.id}
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/dog[dog.owner]/id/{dog.id}/:field
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/dog[dog.owner]/owner/{dog.owner}
/person/birthdate/{person.birthdate.year}/{person.birthdate.month}/
{person.birthdate.day}/dog[dog.owner]/owner/{dog.owner}/:field
/person/age/{person.age.ge}/{person.age.lt}
/person/age/{person.age.ge}/{person.age.lt}/:field
/person/age/{person.age.ge}/{person.age.lt}/dog[dog.owner]
/person/age/{person.age.ge}/{person.age.lt}/dog[dog.owner]/id/{dog.id}
/person/age/{person.age.ge}/{person.age.lt}/dog[dog.owner]/id/
{dog.id}/:field
/person/age/{person.age.ge}/{person.age.lt}/dog[dog.owner]/owner/
{dog.owner}
/person/age/{person.age.ge}/{person.age.lt}/dog[dog.owner]/owner/
{dog.owner}/:field
/person/married/{person.married}
/person/married/{person.married}/:field
/person/married/{person.married}/dog[dog.owner]
/person/married/{person.married}/dog[dog.owner]/id/{dog.id}
/person/married/{person.married}/dog[dog.owner]/id/{dog.id}/:field
/person/married/{person.married}/dog[dog.owner]/owner/{dog.owner}
/person/married/{person.married}/dog[dog.owner]/owner/
{dog.owner}/:field
/person/tags/{person.tags.contains}
/person/tags/{person.tags.contains}/:field
/person/tags/{person.tags.contains}/dog[dog.owner]
/person/tags/{person.tags.contains}/dog[dog.owner]/id/{dog.id}
/person/tags/{person.tags.contains}/dog[dog.owner]/id/{dog.id}/:field
/person/tags/{person.tags.contains}/dog[dog.owner]/owner/{dog.owner}
/person/tags/{person.tags.contains}/dog[dog.owner]/owner/
{dog.owner}/:field
/dog/id/{dog.id}
/dog/id/{dog.id}/:field
/dog/owner/{dog.owner}
/dog/owner/{dog.owner}/:field

You can cut and paste the output of

/yourapp/default/api/patterns

back in the code as patterns and change them.


You can disable some by making the corresponding fields readable=False
in the api action.

Hope this is useful. Please send comments.


On Mar 16, 6:32 pm, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:
> http://vimeo.com/21133657