Skip to content

Add request parameter access #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

madsmtm
Copy link

@madsmtm madsmtm commented Nov 23, 2018

I'd like to suggest adding Request.request_parameters and Resource.params, which follows the idea of Resource.data. This would allow you to easily create endpoints that have e.g. optional filtering.

Example usage (untested):

data = ["one", "two", "three"]
class MyResource(Resource):
    def detail(self, pk):
        return data[int(pk)]
    def list(self):
        if "slice" in self.params:
           return data[slice(*map(int, self.params["slice"]))]
        return data

Which would allow GET /resource/?slice=1,3 to return data[1, 3] (["two", "three"]).

If this gets approved, I'll update the docs and test suite

@@ -128,6 +128,9 @@ def request_method(self):
def request_body(self):
return self.request.body

def request_parameters(self):
return self.request.query_arguments
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is incomplete, HTTPServerRequest.query_arguments returns a mapping that points to bytes, not str. I'm not sure how to cleanly fix this, we could maybe use RequestHandler.decode_argument, but this would cause us to automatically parse the arguments before somebody actually needs them. Another option is to add a helper class around RequestHandler.get_query_argument with __getitem__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant