Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Last active September 27, 2016 15:22
Show Gist options
  • Save pdonorio/06264db86e56aa76f857ae6a14783a09 to your computer and use it in GitHub Desktop.
Save pdonorio/06264db86e56aa76f857ae6a14783a09 to your computer and use it in GitHub Desktop.
Internally parse request parameters

There are two kinds of paramters we may get from any request:

  1. normal parameters, taken from the URL/headers of the request
  2. JSON parameters, taken inside the body of the request

The first kind is the most tipical. They require an explicit declaration before the request kicks in, for which we created the method add_endpoint_parameter. Without the declaration, they won't be parsable even if the client sends them. They should be preferred to JSON parameters.

The second category is the most elastic. They are JSON data which does not have limits in size and are better encoded. They can be as many as the user wants and we don't have to declare which one we receive on server side.

I parse the two kinds when calling the internal get_input method. To avoid collisions the first parameters are preferred, if using the same names on both parameters we label with '_json' the key.

Examples, based on the endpoint above:


http GET $SERVER/api/test?test=1

http GET $SERVER/api/test test=2

http GET $SERVER/api/test?test=1 test=2

http GET $SERVER/api/test?anothername=1 anothername=2

Final note: the most important and most used method - the GET method - should not be using the JSON parameters from REST specifications, because it should not need the body to be received to fullfil the request. They do work in our prototype but the web discourages it. It's up to us I guess.

from ..base import ExtendedApiResource
class MyTest(ExtendedApiResource):
@decorate.add_endpoint_parameter('test')
@decorate.apimethod
def get(self, id=None):
"""
This works for all methods: GET, POST, PUT, PATCH, DELETE
"""
return self.get_input()
# or
return self.get_input(single_parameter='test')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment