REST API in Google App Engine + Python?

18,792

Solution 1

The RESTful api can be build based on EndPoint API. There are some tools can help you make things easier:

appengine rest server (not based on endpoints)

Drop-in server for Google App Engine applications which exposes your data model via a REST API with no extra work.

https://code.google.com/p/appengine-rest-server/

Another one is based on endpoints

By extending the functionality provided by ndb.Model class and the endpoints library, this library allows you to directly interact with model entities in your API methods rather than ProtoRPC requests. For example, instead of:

https://github.com/GoogleCloudPlatform/endpoints-proto-datastore

EDIT1:

I wrote a RESTFul api generator for endpoints.

# generate restful api in one line
BigDataLab = EndpointRestBuilder(GPCode).build(
    api_name="BigDataLab",
    name="bigdatalab",
    version="v1",
    description="My Little Api"
)

repo: https://github.com/Tagtoo/endpoints-proto-datastore-rest

Solution 2

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 <[email protected]>',
            'subject': 'Verify your email address',
            'body_text': 'Click here {{ user.full_name }}: {{ verification_url }}',
            'body_html': '<a href="{{ verification_url }}">Click here</a> {{ user.full_name }}'
            },
        verification_successful_url='/verification_successful',
        verification_failed_url='/verification_failed',
        reset_password_url='/reset_password',
        reset_password_email={
            'sender': 'John Doe <[email protected]>',
            'subject': 'Please reset your password',
            'body_text': 'Reset here: {{ verification_url }}',
            'body_html': '<a href="{{ verification_url }}">Click here</a> to reset'
            },
        )
], debug=True, config=config)

Solution 3

https://github.com/mevinbabuc/Restify

It's a lightweight module that i made, which acts like a ReST interface for appengine. All you would have to do is just define the models in ReSTify/models.py .

You can also add in authentication to it easily without tweaking much.

To get started al you have to do is

import webapp2

import ReSTify

application = webapp2.WSGIApplication(
    [
        ('/api/.*', ReSTify.ReST),
        ],
    debug=True)
Share:
18,792

Related videos on Youtube

TeknasVaruas
Author by

TeknasVaruas

Co-founder, DeepSource • Full time geek • Passionate about helping developers ship good code • Talk to me about developer tools.

Updated on November 07, 2020

Comments

  • TeknasVaruas
    TeknasVaruas over 3 years

    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 similar to django-tastypie for GAE?

    • grim
      grim over 10 years
      Try taking a look at protorpc services. Endpoints are built on top of them and they're not too bad to write.
    • Paul Collingwood
      Paul Collingwood over 10 years
      you can use Django on GAE so that might work for you directly.
    • Tim Hoffman
      Tim Hoffman over 10 years
      you can build restful api's with heaps of different micro frameworks. I personally don't use webapp(2) but bobo for this sort of application.
    • psun
      psun over 7 years
      I'm also interested in this answer. Is this the case still that we need to use third party libraries listed in the answers or Google cloud endpoints does support REST now? Where in the google docs, is it saying that ProtoRPC is not RESTful?
  • TeknasVaruas
    TeknasVaruas over 10 years
    Great! I'm using the Appengine Rest Server. But the authenticator and authorizer have to be implemented manually. Can you point me to some resources for the same?
  • wprater
    wprater over 9 years
    this is excellent! any reason you choose not to leverage Endpoints?
  • opensourcegeek
    opensourcegeek about 9 years
    @wprater, code.google.com/p/googleappengine/issues/detail?id=9384 this could be one of the reasons...
  • wprater
    wprater about 9 years
    @opensourcegeek I ended up using this one (github.com/GoogleCloudPlatform/endpoints-proto-datastore), because I wanted to use endpoints, but this library looks great too!