Search code examples
pythonrequestwebapp2

How to get all parameters into a data structure (list/dict/etc) in webapp2


I'm writing a simple webapp2 script that takes in the parameters passed to it and spits out the result. Eventually I would like to pass those parameters to specific functions, depending on what kind of request is being made.

Code:

class partyHandler(webapp2.RequestHandler):
    def get(self, requestType):
        PROCESS = partyRequests();
        self.response.headers['Content-Type'] = 'text/plain'

    #Here's where I would normally request the paramters,
    #There's too many to list
    #EX:
    # name = cgi.escape(self.request.get('name'))
    #
    options = {
        'create':   PROCESS.createParty,
        'retrieve': PROCESS.retrieveParty,
        'update':   PROCESS.updateParty,
        'delete':   PROCESS.deleteParty,
        'join':     PROCESS.joinParty,
        'broadcast':PROCESS.broadcastParty,
        'leave':    PROCESS.leaveParty,
    }
    #THE PARAMETERS WOULD BE PASSED TO ANY FUNCTION ABOVE DEPENDING ON requestType.
    return self.response.write(options[requestType](<PARAMS>))

app = webapp2.WSGIApplication([
('/party/(create|retrieve|update|delete|broadcast|join|leave)/', partyHandler)
], debug=True)

As you can see, rather than make redundant requests for parameter values I might not need each time a request to the script is made, is there a way to collect all the parameters into a data structure (list/dictionary) and pass that to the functions?

EDIT

To clarify, what I mean is function create takes params:

name
partyImg
latitudeRestaurant
longitudeRestaurant
address

function retrieve takes params:

partyId

My earlier approach was doing this:

#IF I'M CALLING retrieve, THESE ARE POINTLESS
name = cgi.escape(self.request.get('name'))
partyImg = cgi.escape(self.request.get('partyImg'))
latitudeRestaurant = cgi.escape(self.request.get('latitudeRestaurant'))
longitudeRestaurant = cgi.escape(self.request.get('longitudeRestaurant'))
address = cgi.escape(self.request.get('address'))

#IF I'M CALLING create, THESE ARE POINTLESS
partyId = cgi.escape(self.request.get('partyId'))


options = {
    'create':   PROCESS.createParty(name, partyImg,latitudeRestaurant,longitudeRestaurant,address),
    'retrieve': PROCESS.retrieveParty(partyId)
}

Instead of requesting individual params, I'd like to put all the params into a data structure and pass that to a function.


Solution

  • Looking at the webapp2 documentation for requests, I found out this works:

    class partyHandler(webapp2.RequestHandler):
        def get(self, requestType):
            PROCESS = partyRequests();
            self.response.headers['Content-Type'] = 'text/plain'
    
        options = {
            'create':   PROCESS.createParty,
            'retrieve': PROCESS.retrieveParty,
            'update':   PROCESS.updateParty,
            'delete':   PROCESS.deleteParty,
            'join':     PROCESS.joinParty,
            'broadcast':PROCESS.broadcastParty,
            'leave':    PROCESS.leaveParty
        }
        self.response.write(options[requestType](self.request.GET)) <--THIS WORKS
    

    When those methods are called, it looks like this:

    def create(self, args):
        username = args['username']
        ...