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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions restless/fl.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def _wrapper(*args, **kwargs):
def request_body(self):
return self.request.data

def request_parameters(self):
return self.request.args

def is_debug(self):
from flask import current_app
return current_app.debug
Expand Down
17 changes: 17 additions & 0 deletions restless/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(self, *args, **kwargs):
self.init_args = args
self.init_kwargs = kwargs
self.request = None
self.params = None
self.data = None
self.endpoint = None
self.status = 200
Expand Down Expand Up @@ -171,6 +172,21 @@ def request_body(self):
# By default, Django-esque.
return self.request.body

def request_parameters(self):
"""
Returns the current request URL parameters.

Useful for providing custom options on your resources.

If you're integrating with a new web framework, you might need to
override this method within your subclass.

:returns: The request parameters, parsed as a mapping
:rtype: dict
"""
# By default, Django-esque.
return self.request.GET

def build_response(self, data, status=200):
"""
Given some data, generates an HTTP response.
Expand Down Expand Up @@ -281,6 +297,7 @@ def handle(self, endpoint, *args, **kwargs):
if not self.is_authenticated():
raise Unauthorized()

self.params = self.request_parameters()
self.data = self.deserialize(method, endpoint, self.request_body())
view_method = getattr(self, self.http_methods[endpoint][method])
data = view_method(*args, **kwargs)
Expand Down
3 changes: 3 additions & 0 deletions restless/tnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


def build_response(self, data, status=OK):
if status == NO_CONTENT:
# Avoid crashing the client when it tries to parse nonexisting JSON.
Expand Down