video about new RESTful APIs

385 views
Skip to first unread message

Massimo Di Pierro

unread,
Mar 16, 2011, 7:32:45 PM3/16/11
to web2py-users

contatog...@gmail.com

unread,
Mar 16, 2011, 7:38:36 PM3/16/11
to web...@googlegroups.com
Good!! \0/
_____________________________________________
Gilson Filho


2011/3/16 Massimo Di Pierro <massimo....@gmail.com>
http://vimeo.com/21133657

contatog...@gmail.com

unread,
Mar 16, 2011, 7:53:32 PM3/16/11
to web...@googlegroups.com
- The patterns is storaged in the controller?

Anthony

unread,
Mar 16, 2011, 10:42:44 PM3/16/11
to web...@googlegroups.com
On Wednesday, March 16, 2011 7:32:45 PM UTC-4, Massimo Di Pierro wrote:
http://vimeo.com/21133657
 
Cool. I like the automated presentation system -- now you just need to add a spell checker (and maybe a voice that reads English with an Italian accent). :D
 
Anthony

Massimo Di Pierro

unread,
Mar 17, 2011, 12:42:43 AM3/17/11
to web2py-users
Or the model. It does not matter.

On Mar 16, 6:53 pm, "contatogilson...@gmail.com"
<contatogilson...@gmail.com> wrote:
> - The patterns is storaged in the controller?
> _____________________________________________
> *Gilson Filho*
> *Web Developerhttp://gilsondev.com*

Massimo Di Pierro

unread,
Mar 17, 2011, 12:43:05 AM3/17/11
to web2py-users
LOL

mart

unread,
Mar 17, 2011, 1:33:01 AM3/17/11
to web2py-users
I'm impressed! (and floored) - thanks for this!

So, I didn't realize the other day that web2py does some of the work
(that lazy option is sounding sweet to me!). Does it log any of it?
thinking for reference. I've been obviously been doing something wrong
in the last few days, and I think there was a spot that came by
quickly that may hold the answer to my mistakes :)

Would you think this would be a great way to trigger remote events?
I'm thinking triggers and dedicated queues...
Im thinking that if we us something like this, where something like
"field watchers' would pick up on specific update in the DB, it could
trigger some event (like automation), and I can get web2py to
represent that dynamically via a web page I have open? This would do
wonders for real time reporting (and interaction)... (unless I am
missing the point)

Thanks again!
Mart :)


On Mar 17, 12:43 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Massimo Di Pierro

unread,
Mar 17, 2011, 3:58:40 PM3/17/11
to 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

David J.

unread,
Mar 17, 2011, 4:05:44 PM3/17/11
to web...@googlegroups.com
For clarification; This gets put in the model code not controller code
correct?

So for example in "db.py"

Thanks.

Massimo Di Pierro

unread,
Mar 17, 2011, 4:26:15 PM3/17/11
to web2py-users
No. This is an action

@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()

ix exposes the APIs so it goes in controller. If instead of 'auto' you
choose to define your own patterns, perhaps you should that in a
model:

APIPATTERNS=[
...
]

and then pass APIPATTERNS instead of 'auto'

Massimo

mart

unread,
Mar 17, 2011, 5:15:30 PM3/17/11
to web2py-users
SWEET! I'm not even going to bother fixing the mess I made :) This
looks awesome!

I'll be spending a good chunk of time on this later this evening and
will post results/feedback and anything else you'd like to see or have
me test.


Thanks
Mart :)

On Mar 17, 4:26 pm, Massimo Di Pierro <massimo.dipie...@gmail.com>

Kevin Ivarsen

unread,
Mar 17, 2011, 5:27:07 PM3/17/11
to web2py-users
I was confused by this as well. In the message above, I think

"simply add this to your model"

should read

"simply add this to your *controller*"

which of course makes much more sense.

Adnan Smajlovic

unread,
Mar 17, 2011, 6:02:47 PM3/17/11
to web...@googlegroups.com
downloaded the version 1.94.3, and included the api in the controller.

it responds well, but returns only the field names. when i include the field name, then it returns: record not found

as far as I checked, the record with the same id is in the database...

Massimo Di Pierro

unread,
Mar 17, 2011, 7:38:14 PM3/17/11
to web2py-users
Please access the /patterns url, tell me which pattern you are suing
and what url you are visiting.

Adnan Smajlovic

unread,
Mar 17, 2011, 8:08:46 PM3/17/11
to web...@googlegroups.com
My apology. 

Tested again after seeing the patterns, and all works perfect, and as expected... 


Keith Pettit

unread,
Mar 22, 2011, 11:39:14 PM3/22/11
to web...@googlegroups.com, Massimo Di Pierro
Ah this is great.  I love the video and the examples on this post work nice.  By the way which voice app is that you use for your recordings.  Cepstral maybe.  It's a very pleasant voice?

Question:  What is the difference between RESTful APIs and CRUD?

I've been going through the web2py book and now I'm trying RESTful API's and it seems like the same thing.  At least from what I've tried so far.  What is the difference and why would I want to use one method over another?

Thanks,

-Keith

Massimo Di Pierro

unread,
Mar 22, 2011, 11:46:39 PM3/22/11
to web...@googlegroups.com
I use Mac "say". It comes with it.

CRUD mostly makes forms, selects and search tables.

RESTful APIs are new. Eventually CRUD should also expose them.

massimo


On Mar 22, 2011, at 10:39 PM, Keith Pettit wrote:

> RESTful APIs and CRUD

Jonathan Lundell

unread,
Mar 23, 2011, 12:49:35 AM3/23/11
to web...@googlegroups.com
On Mar 22, 2011, at 8:46 PM, Massimo Di Pierro wrote:
>
> I use Mac "say". It comes with it.

Alex.

mikech

unread,
Mar 25, 2011, 1:08:01 PM3/25/11
to web...@googlegroups.com, Massimo Di Pierro
Very good and very clear.  I wanted to post this to Dzone, but noticed that it's a proposal.  Should I wait until the final format is set?

Massimo Di Pierro

unread,
Mar 25, 2011, 1:22:02 PM3/25/11
to web2py-users
It is in stable now. We may make minor changes but not major ones. I'd
say post it.
Reply all
Reply to author
Forward
0 new messages