2013-10-15 68 views
18

如何使用Google App Engine和Python创建RESTful API?我尝试过使用云端点,但文档不关注RESTful API。 GAE有没有类似于django-tastypie的东西?Google App Engine + Python中的REST API?

+0

尝试采取看看ProtoRPC的服务。端点建立在它们之上,并且它们写得不错。 – grim

+0

你可以在GAE上使用Django,这样可以直接为你工作。 –

+0

你可以用不同的微型框架构建平静的api。我个人不使用webapp(2),而是使用bobo来处理这类应用程序。 –

回答

11

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

AppEngine上休息服务器(不是基于端点)

投入式服务器,它通过REST API没有暴露你的数据模型谷歌App Engine应用程序加班。

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

另一种是基于端点

通过扩展ndb.Model类提供的功能和终端库,这个库可以让你直接在模型实体交互的API方法而不是ProtoRPC请求。例如,而不是:

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

EDIT1:

我写了端点一个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

+1

太棒了!我正在使用Appengine Rest Server。但验证者和授权人必须手动实施。你能指点我一些资源吗? – theG33k

1

https://github.com/mevinbabuc/Restify

这是我由轻质模块,它的作用就像一个REST接口为应用服务引擎。 您只需在ReSTify/models.py中定义模型即可。

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

要开始人,你要做的就是

import webapp2 

import ReSTify 

application = webapp2.WSGIApplication(
    [ 
     ('/api/.*', ReSTify.ReST), 
     ], 
    debug=True) 
9

https://github.com/budowski/rest_gae

我已经创建了NDB车型在webapp2的一个全面的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 <[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) 
+0

这很棒!您选择不利用终端的任何原因? – wprater

+0

@wprater,https://code.google.com/p/googleappengine/issues/detail?id=9384这可能是其中一个原因... – opensourcegeek

+0

@opensourcegeek我结束了使用这个(https:// github .com/GoogleCloudPlatform/endpoints-proto-datastore),因为我想使用端点,但是这个库看起来也很棒! – wprater