开发者

A dynamic URL implementation for a RESTful API in Google App Engine

开发者 https://www.devze.com 2023-02-20 07:27 出处:网络
A client application will send a message using a GET method to http://server/user/USER_ID/history in order to receive a JSON reply containing the usage history of the user. (may it be a list of songs

A client application will send a message using a GET method to http://server/user/USER_ID/history in order to receive a JSON reply containing the usage history of the user. (may it be a list of songs listened to, purchases, etc)

I can get as far as to handling http://server/user/USER_ID/ via the following snippet:

application = webapp.WSGIApplication(
        [('/', BrowserTests),
         ('/user/([^/]+)?', UserHandler),
        ], debug=True)

Which I consume in UserHandler via:

user_id = str(urllib.unquote(resource))

And thus I can carry out the work for htt开发者_开发问答p://server/user/USER_ID. But how can I go a step further and consume history?

I tried looking up on StackOverflow and in the documentation, but I guess I don't know exactly what to ask for to receive an answer.


You could go with something like this:

application = webapp.WSGIApplication([
    ('/user/([^/]+)/([^/]+)', UserHandler),
    ], debug=True)


class UserHandler(webapp.RequestHandler):
  def get(self, user_id, action_to_consume):
    self.response.out.write("Action %s" % action_to_consume)#Should print History


Add a second parameter

application = webapp.WSGIApplication(
        [('/', BrowserTests),
         ('/user/([^/]+)/([^/]+)$', UserHandler),
        ], debug=True)

class UserHandler(webapp.RequestHandler):
    def get(self, userid, history):


Alternately to @Jose and @systempuntoout's answers, if you want a separate handler for each action:

application = webapp.WSGIApplication([
    ('/', BrowserTests),
    ('/user/([^/]+)', UserHandler),
    ('/user/([^/]+)/history', HistoryHandler),
], debug=True)

class UserHandler(webapp.RequestHandler):
  def get(self, userid):
    pass

class HistoryHandler(webapp.RequestHandler):
  def get(self, userid):
    pass
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号