you can also post this in web2pyslices.com http://zerp.ly/rochacbruno Em 02/08/2012 13:30, "Dirk Krause" <dir...@googlemail.com> escreveu:
> Hi, > > I tried to pull together the different sources for restful services, > including various hints in this group and of course the excellent web2py > documentation. If this is the wrong place or info, feel free to move or > delete it. > > Here's how you create a restful web2py application in under 2 minutes: > - create a wizard application called 'RT' > - add a table named 'entries' and add a field 'entry wiki' > - finish the wizard with all the default values > - register a user 'user' with password 'pass' > now you have an application with 10 random entries. > > The next two step are required due to a bug that I filed before ( > http://goo.gl/CPO0G) > - go to the design page of the app and change the table name from > 't_entries' to 'entries' > in db_wizard.py (4 times) and in default.py (1 time) > - check that the application is still working. you notice that the table > now is empty since we changed > the name of the table. > - Add a couple of entries. > > - now add the RESTful services to the controller as described in > http://goo.gl/iITNd ('parse_as_rest') > or here: http://goo.gl/ltfa2 > > @request.restful() > def api(): > response.view = 'generic.'+request.extension > def GET(*args,**vars): > patterns = 'auto' > parser = db.parse_as_rest(patterns,args,vars) > if parser.status == 200: > return dict(content=parser.response) > else: > raise HTTP(parser.status,parser.error) > def POST(table_name,**vars): > return db[table_name].validate_and_insert(**vars) > def PUT(table_name,record_id,**vars): > return db(db[table_name]._id==record_id).update(**vars) > def DELETE(table_name,record_id): > return db(db[table_name]._id==record_id).delete() > return locals() > > > - In the browser: > you get all entries with > http://127.0.0.1:8000/RT/default/api/entries.json > > you get the just the second entry with > http://127.0.0.1:8000/RT/default/api/entries/id/2.json > > you get all auto-generated patterns with > http://127.0.0.1:8000/RT/default/api/patterns.json > > That's as far as we get with GET in a browser. > > To make things more realistic, we add basic user authentication and try > some POSTs with curl. > - Add > auth.settings.allow_basic_login = True > @auth.requires_login() > > above @request.restful. Authentification now is mandatory for the REST > interface. > > - get the first entry > curl --user user:pass > http://127.0.0.1:8000/RT/default/api/entries/id/1.json > > - post a new entry > curl --user user:pass -d "f_entry=something" > http://127.0.0.1:8000/RT/default/api/entries.json > > - delete the first entry > curl -X DELETE --user user:pass > http://127.0.0.1:8000/RT/default/api/entries/1.json > > > Same thing from python: > > import requests > from requests.auth import HTTPBasicAuth > payload = {'f_entry': 'somevalue'} > auth=HTTPBasicAuth('user', 'pass') > r = requests.post("http://127.0.0.1:8000/RT/default/api/entries.json",data > =payload, auth=auth) > r = requests.delete("http://127.0.0.1:8000/RT/default/api/entries/1.json",data > =payload, auth=auth) > > -- > > > > --