Python webapp2+;RESTful API

Python webapp2+;RESTful API,python,google-app-engine,webapp2,Python,Google App Engine,Webapp2,我正在谷歌应用引擎+Python+webapp2上构建。构建现代web应用程序的一部分需要restful API。我知道我可以用Flask实现这一点,但是我想探索在webapp2上构建RESTAPI的可能性 在webapp2上,请求的处理方式如下: app = webapp2.WSGIApplication([ ('/post/new', CreatePost), ('/post/([a-z0-9]+)', ViewPost), ('/post/([a-z0-9]+)/e

我正在谷歌应用引擎+Python+webapp2上构建。构建现代web应用程序的一部分需要restful API。我知道我可以用
Flask
实现这一点,但是我想探索在
webapp2
上构建RESTAPI的可能性

在webapp2上,请求的处理方式如下:

app = webapp2.WSGIApplication([
    ('/post/new', CreatePost),
    ('/post/([a-z0-9]+)', ViewPost),
    ('/post/([a-z0-9]+)/edit', EditPost),
    ('/post/([a-z0-9]+)/delete', DeletePost)
])
class PostHandler(webapp2.RequestHandler):
    def get(self, post_id=None):
        if post_id:
            # handle Fetching a single post object
        else:
            # handle Queries

    def post(self, post_id=None):
        if post_id:
            self.abort(405)
        # handle creating a single post object

    def put(self, post_id=None):
        if post_id:
            # handle updating a single post object
        else:
            self.abort(405)


    def delete(self, post_id=None):
        if post_id:
            # handle deleting a single post object
        else:
            self.abort(405)


app = webapp2.WSGIApplication([
    ('/post/<post_id>/', PostHandler),
    ('/post/', PostHandler),
])
注:
([a-z0-9]+)
是一个正则表达式,表示
post\u id

上述请求处理程序不遵循RESTful模式,因为请求
方法
是在路径(/delete、/edit、/new)中指定的,而不是在请求头中指定的

解决方案是创建一个接收所有请求类型的处理程序类吗?例如:

class PostHandler(webapp2.RequestHandler):
    def get(self):
        # handle GET requests

    def post(self):
        # handle POST requests

    def put(self):
        # handle PUT requests

    def delete(self):
        # handle DELETE requests


app = webapp2.WSGIApplication([
    ('/post/?', PostHandler)
])
在这种情况下,所有的
/post
路径都由PostHandler处理。此模式中不再使用
post_id
,因为它将在请求主体中提交


这是使用webapp2构建REST API的正确方法吗?

您走的是正确的道路,但您应该继续在url中处理
post\u id
,并按如下方式操作:

app = webapp2.WSGIApplication([
    ('/post/new', CreatePost),
    ('/post/([a-z0-9]+)', ViewPost),
    ('/post/([a-z0-9]+)/edit', EditPost),
    ('/post/([a-z0-9]+)/delete', DeletePost)
])
class PostHandler(webapp2.RequestHandler):
    def get(self, post_id=None):
        if post_id:
            # handle Fetching a single post object
        else:
            # handle Queries

    def post(self, post_id=None):
        if post_id:
            self.abort(405)
        # handle creating a single post object

    def put(self, post_id=None):
        if post_id:
            # handle updating a single post object
        else:
            self.abort(405)


    def delete(self, post_id=None):
        if post_id:
            # handle deleting a single post object
        else:
            self.abort(405)


app = webapp2.WSGIApplication([
    ('/post/<post_id>/', PostHandler),
    ('/post/', PostHandler),
])
类PostHandler(webapp2.RequestHandler):
def get(自我,post_id=None):
如果是post_id:
#获取单个post对象的句柄
其他:
#处理查询
def post(自身,post_id=无):
如果是post_id:
自动中止(405)
#创建单个post对象的句柄
def put(自身、post_id=无):
如果是post_id:
#处理更新单个post对象
其他:
自动中止(405)
def delete(self,post_id=None):
如果是post_id:
#处理删除单个post对象
其他:
自动中止(405)
app=webapp2.WSGIApplication([
(“/post//”,PostHandler),
(“/post/”,PostHandler),
])

此外,将HTTP谓词放在请求负载中(如建议的
voscausa
)不符合RESTful API设计。

应该扩展
webapp2.RequestHandler
,并将该扩展用作
RESTfulHandler
的新基类,然后还可以出于特定目的扩展
RESTfulHandler
RESTfulPostHandler
。通过这种方式,我们还可以处理新的方法或工作,例如头中的jwt(JSON web令牌)、授权和希望在头中处理的其他属性


顺便说一句,“post”可能不是项目/对象的最佳名称,因为它很容易与http动词“post”混淆。如果我是您,我会将其重命名为“item”或类似名称,并使用扩展webapp2请求处理程序的
RESTfulItemHandler
。您希望在处理程序之间共享许多内容,这样可以方便地在基类中共享功能。

为什么不使用包含操作(create..delete)和数据的JSON请求呢。它易于扩展,也有利于您的js客户端。github“RestAPI for Webapp2”@voscausa上有一个相关项目,只是想澄清一下,您的意思是用JSON发送请求,其中一个属性是方法名吗?示例:
{method:“DELETE”,数据:{…},…}
@NiklasRosencrantz,ty用于建议。如果我可以不使用库,我就避免使用库,除非没有库太复杂。@puoygae IIUC,只有通过在头中指定http方法(“method=PUT”和类似方式)来实际使用它,这才是REST。创建一个处理扩展webapp2.RequestHandler的特殊类是一个好主意。没有想到这一点。