Expose db.auth_user table via rest api

32 views
Skip to first unread message

Kevin Keller

unread,
Feb 18, 2020, 1:01:14 PM2/18/20
to web2py-users
When creating a simple rest api with web2py:

@request.restful()
def api():
    
    def GET(*args, **vars):
        parser=db.parse_as_rest('auto',args,vars)
        return dict(content=parser.response)
    
    def POST(table_name,**vars):
        return dict(db[table_name].validate_and_insert(**vars))
    
    return locals()

I can get access via rest api to any table but auth_users.

Is there a way I can enable this? 

I would like to provision new users via rest api to my web2py app. 

Authentication is done via external IDP via OIDC. 

So I basically just need the firstname, lastname and email to be provisioned via rest. 

Authentication to provision is at the moment an after thought. 

I just need to get this working asap. 

Thank you!


Kevin Keller

unread,
Feb 18, 2020, 1:17:25 PM2/18/20
to web2py-users
I tried a custom pattern with the get method, but still dont get the users from auth_user


@request.restful()
def api():
    
    def GET(*args, **vars):
        patterns = [
            "/users[auth_user]"
            ]
        parser=db.parse_as_rest(patterns,args,vars)

Kevin Keller

unread,
Feb 18, 2020, 1:45:43 PM2/18/20
to web2py-users
The read request works now.. just forgot to add a user. 

When I try to post a user into auth_user however I get this: 

Using python 3.8 on ubuntu 18

image.png

--
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 web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/b2b4c86e-89f1-4639-a9af-104a01f87776%40googlegroups.com.

Kevin Keller

unread,
Feb 18, 2020, 2:02:20 PM2/18/20
to web2py-users
Even when  I manually try to convert the post data I get the error:

def POST(table_name,**vars):
        restdata=request.vars
        
        return dict(db[table_name].validate_and_insert(restdata.decode("utf-8")))
    

On Tuesday, 18 February 2020 10:01:14 UTC-8, Kevin Keller wrote:

Kevin Keller

unread,
Feb 18, 2020, 2:47:22 PM2/18/20
to web2py-users
I didnt know I need to post the data as form data. 

Now it works. 

How I can post a JSON payload instead and use that payload for insert_and_validate? 

I tried this:

def POST(table_name,**vars):
        import json
        data=json.loads(request.body.read())
        data=json.dumps(data)
        return dict(db[table_name].validate_and_insert(data))

Kevin Keller

unread,
Feb 18, 2020, 3:23:58 PM2/18/20
to web2py-users

Found the answer here:


def api():
 from gluon.serializers import json
 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)
     #data = gluon.contrib.simplejson.loads(request.body.read())
     return json(db[table_name].validate_and_insert(**vars))
     return dict()
 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()
 def OPTIONS(*args,**vars):
     print "OPTION called"
     return True
 return dict(GET=GET,POST=POST,PUT=PUT,DELETE=DELETE,OPTIONS=OPTIONS)
Reply all
Reply to author
Forward
0 new messages