我想在app引擎上构建REST Web服务。目前我有这个:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class UsersHandler(webapp.RequestHandler):
def get(self, name):
self.response.out.write('Hello '+ name+'!')
def main():
util.run_wsgi_app(application)
#Map url like /rest/users/johnsmith
application = webapp.WSGIApplication([(r'/rest/users/(.*)',UsersHandler)]
debug=True)
if __name__ == '__main__':
main()
我希望在访问路径/ rest / users时检索所有用户。我想我可以通过构建另一个处理程序来做到这一点,但我想知道是否可以在此处理程序中执行此操作。
答案 0 :(得分:14)
当然,您可以 - 将处理程序的get
方法更改为
def get(self, name=None):
if name is None:
"""deal with the /rest/users case"""
else:
# deal with the /rest/users/(.*) case
self.response.out.write('Hello '+ name+'!')
和你的申请
application = webapp.WSGIApplication([(r'/rest/users/(.*)', UsersHandler),
(r'/rest/users', UsersHandler)]
debug=True)
换句话说,将处理程序映射到您希望它处理的所有URL模式,并确保处理程序的get
方法可以轻松区分它们(通常通过其参数)。