2013-10-15 54 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