*controllers/api.py*
#from gluon.tools import Service
#service = Service()
def call():
#session.forget()
return service()
@service.run
def service_call(table_name, id):
if id.isdigit() and int(id) > 0:
content = db[table_name](id)
else:
content = db(db[table_name]['id'] > 0).select()
return dict(content = content)
@request.restful()
def parse_as_rest():
response.view = 'generic.' + request.extension
def GET(*args, **vars):
patterns = 'auto'
parser = db.parse_as_rest(patterns, args, vars)
if parser.status == 200:
content = parser.response
return response.json(content)
else:
raise HTTP(parser.status, parser.error)
def POST(table_name, **vars):
return dict(db[table_name].validate_and_insert(**vars) )
def PUT(table_name, record_id, **vars):
return dict(db(db[table_name]._id == record_id).validate_and_update
(**vars) )
def DELETE(table_name, record_id):
return db(db[table_name]._id == record_id).delete()
return locals()
*access*:
http://127.0.0.1:8000/api/api/parse_as_rest/auth_event.json
*traceback error:*
Traceback (most recent call last):
File "/Users/MacBookPro/project/python/web2py/gluon/restricted.py", line
219, in restricted
exec(ccode, environment)
File
"/Users/MacBookPro/project/python/web2py/applications/test/controllers/api.py",
line 11, in <module>
@service.run
NameError: name 'service' is not defined
*question:*
1. why the error is occured while accessing the* parse_as_rest()* function?
i know the solution is uncomment the import and initiate a service object
2. let say comment the #@service.run or uncomment the import and initiate a
service object, when accessing the url :
http://127.0.0.1:8000/api/api/parse_as_rest/auth_event.json
*result:*
*no matching pattern*
the same thing, while accessing auth_group, auth_user
3. should *session.forget()* been used in call function? what is the best
practice (comment it or not)
4. let say plan to use web2py as an api server (define table in models or
modules and create controller like an example above), is it best practice
to use the minimalist web2py?
http://web2py.com/books/default/chapter/29/14/other-recipes#Building-a-minimalist-web2py
5. interest in the syntax *validate_and_insert(**vars)*, what is the logic
behind that? plan to learn it and use with another micro framework, e.g.
slim or express to learn build an api server
any idea?
thx and best regards,
stifan
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.