REST API in Google App Engine + Python?

前端 未结 3 1137
时光说笑
时光说笑 2020-12-23 22:28

How create a RESTful API using Google App Engine with Python? I\'ve tried using Cloud Endpoints, but the documentation does not focus on a RESTful API. Is there something si

3条回答
  •  不思量自难忘°
    2020-12-23 23:05

    https://github.com/budowski/rest_gae

    I've created a full-fledged REST API for NDB models over webapp2. Includes permissions handling and much much more.

    Would love to hear your thoughts:

    class MyModel(ndb.Model):
      property1 = ndb.StringProperty()
      property2 = ndb.StringProperty()
      owner = ndb.KeyPropertyProperty(kind='User')
    
      class RESTMeta:
        user_owner_property = 'owner' # When a new instance is created, this property will be set to the logged-in user
        include_output_properties = ['property1'] # Only include these properties for output
    
    app = webapp2.WSGIApplication([
        # Wraps MyModel with full REST API (GET/POST/PUT/DELETE)
        RESTHandler(
          '/api/mymodel', # The base URL for this model's endpoints
          MyModel, # The model to wrap
          permissions={
            'GET': PERMISSION_ANYONE,
            'POST': PERMISSION_LOGGED_IN_USER,
            'PUT': PERMISSION_OWNER_USER,
            'DELETE': PERMISSION_ADMIN
          },
    
          # Will be called for every PUT, right before the model is saved (also supports callbacks for GET/POST/DELETE)
          put_callback=lambda model, data: model
        ),
    
        # Optional REST API for user management
        UserRESTHandler(
            '/api/users',
            user_model=MyUser, # You can extend it with your own custom user class
            user_details_permission=PERMISSION_OWNER_USER,
            verify_email_address=True,
            verification_email={
                'sender': 'John Doe ',
                'subject': 'Verify your email address',
                'body_text': 'Click here {{ user.full_name }}: {{ verification_url }}',
                'body_html': 'Click here {{ user.full_name }}'
                },
            verification_successful_url='/verification_successful',
            verification_failed_url='/verification_failed',
            reset_password_url='/reset_password',
            reset_password_email={
                'sender': 'John Doe ',
                'subject': 'Please reset your password',
                'body_text': 'Reset here: {{ verification_url }}',
                'body_html': 'Click here to reset'
                },
            )
    ], debug=True, config=config)
    

提交回复
热议问题