4

I am coming from a Java REST background to Python on Google App Engine's. I need some help using webapp2 with path-parameters. Below is an example of how Java would read a request. Will someone please translate the code into how python would read it with webapp2?

// URL: my_dogs/user_id/{user_id}/dog_name/{a_name}/breed/{breed}/{weight}

@Path("my_dogs/user_id/{user_id}/dog_name/{a_name}/breed/{breed}/{weight}")
public Response getMyDog(
    @PathParam("user_id") Integer id,
    @PathParam("a_name") String name,
    @PathParam("breed") String breed,
    @PathParam("weight") String weight
){

//the variables are: id, name, breed, weight.
///use them somehow

}

I have already gone over the examples on google ( https://developers.google.com/appengine/docs/python/gettingstartedpython27/usingwebapp ). But I don't know how to extend the simple

app = webapp2.WSGIApplication([('/', MainPage),
                           ('/sign', Guestbook)],
                          debug=True)
kasavbere
  • 5,873
  • 14
  • 49
  • 72
  • How do I change the following to look like mine? `webapp2.Route(r'/products', handler='handlers.ProductsHandler', name='products-list', methods=['GET'])` – kasavbere Dec 03 '12 at 03:16

1 Answers1

5

Have a look at URI routing in webapp2. Here you can match / route an URI and get the arguments. These keyword arguments are passed to your handler : http://webapp2.readthedocs.io/en/latest/guide/routing.html#the-url-template

Here is a helloworld example with one argument {action} :

#!/usr/bin/python
# -*- coding: utf-8 -*-

import webapp2

class ActionPage(webapp2.RequestHandler):

    def get(self, action):

        self.response.headers['Content-Type'] = 'text/plain'        
        self.response.out.write('Action, ' + action)

class MainPage(webapp2.RequestHandler):

    def get(self):

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, webapp2 World!')

app = webapp2.WSGIApplication([
        webapp2.Route(r'/<action:(start|failed)>', handler=ActionPage),
        webapp2.Route(r'/', handler=MainPage),                    
], debug=True)

And your app.yaml:

application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: false

handlers:
- url: (.*)
  script: helloworld.app

libraries:
- name: webapp2
  version: latest

This works fine in the SDK when I try

http://localhost:8080/start   # result: Action, start
or
http://localhost:8080         # result: Hello, webapp2 World!
FelixEnescu
  • 4,664
  • 2
  • 33
  • 34
voscausa
  • 11,253
  • 2
  • 39
  • 67
  • I read that link before posting. For some reason I am not getting it. So I figure having someone parse my own example for me would help. – kasavbere Dec 03 '12 at 03:13
  • How would I modify this? `webapp2.Route(r'/products', handler='handlers.ProductsHandler', name='products-list', methods=['GET'])` – kasavbere Dec 03 '12 at 03:16
  • I have given you a piece of my own code, so I do not make a mistake. Now you can figure out how to handle your own path with keyword arguments. – voscausa Dec 03 '12 at 03:45
  • Thanks for helping. I try your path with the helloworld example on the google site. Basically the parameters do nothing. Still the path should work, but it does not work. The link to the example: `https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld`. – kasavbere Dec 03 '12 at 03:59
  • of course I change to `handler=helloworld.MainPage`; then I try a few other different things: Nothing. – kasavbere Dec 03 '12 at 04:01
  • I habe added the helloworld example and included the app.yaml – voscausa Dec 03 '12 at 11:20