20

如何使用带有 Python 的 Google App Engine 创建 RESTful API?我尝试过使用 Cloud Endpoints,但文档并不关注 RESTful API。有没有类似于 GAE 的 django-tastypie 的东西?

4

3 回答 3

12

RESTful api 可以基于 EndPoint API 构建。有一些工具可以帮助您使事情变得更容易:

appengine 休息服务器(不基于端点)

用于 Google App Engine 应用程序的插入式服务器,无需额外工作即可通过 REST API 公开您的数据模型。

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

另一种是基于端点

通过扩展 ndb.Model 类和端点库提供的功能,该库允许您直接与 API 方法中的模型实体进行交互,而不是 ProtoRPC 请求。例如,而不是:

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

编辑1:

我为端点编写了一个 RESTFul api 生成器。

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

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

于 2013-10-15T11:09:37.540 回答
9

https://github.com/budowski/rest_gae

我已经通过 webapp2 为 NDB 模型创建了一个成熟的 REST API。包括权限处理等等。

很想听听你的想法:

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 <john@doe.com>',
            '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 <john@doe.com>',
            '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)
于 2014-01-30T01:49:24.180 回答
2

https://github.com/mevinbabuc/Restify

这是我制作的一个轻量级模块,它的作用类似于 appengine 的 ReST 接口。您所要做的就是在 ReSTify/models.py 中定义模型。

您还可以轻松添加身份验证,而无需进行太多调整。

要开始,您必须做的是

import webapp2

import ReSTify

application = webapp2.WSGIApplication(
    [
        ('/api/.*', ReSTify.ReST),
        ],
    debug=True)
于 2014-01-08T12:28:35.383 回答