falcon
falcon copied to clipboard
The pip version is not consistent with the documentation.
The second example in the README, in ThingsResource.on_get() (line 135), tries to set a item in the request.context dict, but running it raises the following exception:
Traceback (most recent call last):
File "/usr/lib64/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "api.py", line 111, in falcon.api.API.__call__ (falcon/api.c:1829)
File "api.py", line 96, in falcon.api.API.__call__ (falcon/api.c:1635)
File "api_helpers.py", line 262, in falcon.api_helpers._wrap_with_before.do_before (falcon/api_helpers.c:3405)
File "api_helpers.py", line 262, in falcon.api_helpers._wrap_with_before.do_before (falcon/api_helpers.c:3405)
File "/home/ysag/Virtualenvs/glyfix/lib/python2.7/site-packages/falcon/hooks.py", line 142, in do_after_one
responder(self, req, resp, **kwargs)
File "things2.py", line 135, in on_get
req.context['doc'] = result
AttributeError: 'Request' object has no attribute 'context'
I checked the source code for the Request class in falcon/request.py, and indeed, it doesn't even contain the token context.
[EDIT] Pasting the problematic code snippet:
class ThingsResource:
def __init__(self, db):
self.db = db
self.logger = logging.getLogger('thingsapp.' + __name__)
@falcon.after(serialize)
def on_get(self, req, resp, user_id):
marker = req.get_param('marker') or ''
limit = req.get_param_as_int('limit') or 50
try:
result = self.db.get_things(marker, limit)
except Exception as ex:
self.logger.error(ex)
description = ('Aliens have attacked our base! We will '
'be back as soon as we fight them off. '
'We appreciate your patience.')
raise falcon.HTTPServiceUnavailable(
'Service Outage',
description,
30)
# An alternative way of doing DRY serialization would be to
# create a custom class that inherits from falcon.Request. This
# class could, for example, have an additional 'doc' property
# that would serialize to JSON under the covers.
req.context['doc'] = result
resp.set_header('X-Powered-By', 'Small Furry Creatures')
resp.status = falcon.HTTP_200
@falcon.before(deserialize)
def on_post(self, req, resp, user_id, doc):
proper_thing = self.db.add_thing(doc)
resp.status = falcon.HTTP_201
resp.location = '/%s/things/%s' % (user_id, proper_thing['id'])
This gave me a hard time, req.context is only avaliable from version 0.2.0b1, the problem is that via pip I only get version 0.1.10
Right, sorry folks for the confusion. The docs didn't even exist until 0.2 and it isn't clear from readthedocs this is the case. I'll get this cleaned up ASAP.