bottle decorator in route

149 views
Skip to first unread message

Felix

unread,
Mar 26, 2012, 1:59:50 AM3/26/12
to bottlepy
Hi all,
I have some basic question on how a decorator in the apply list can
get the current route information.

@route("/yo", apply=[basic_oauth])
def yo():
return "YO"

def basic_oauth(func):
"""
handle the basic oauth case
"""
def wrapper(*arg, **kargs):
print bottle.request.url
return func(**kargs)
return wrapper

the request.url returned is http://127.0.0.1/ without the yo in the
path. What is interesting is the server is listening on http://192.168.1.105:8080/
not sure what is going on with the request.url

Using __version__ = '0.9.6'

Thanks,

Felix

ma...@gsites.de

unread,
Mar 26, 2012, 1:53:34 PM3/26/12
to bott...@googlegroups.com
> Hi all,
> I have some basic question on how a decorator in the apply list can
> get the current route information.
>
> @route("/yo", apply=[basic_oauth])
> def yo():
> return "YO"
>
> def basic_oauth(func):
> """
> handle the basic oauth case
> """
> def wrapper(*arg, **kargs):
> print bottle.request.url
> return func(**kargs)
> return wrapper

Your example looks perfectly fine. And works as expected on my machine.

> the request.url returned is http://127.0.0.1/ without the yo in the
> path. What is interesting is the server is listening on
> http://192.168.1.105:8080/
> not sure what is going on with the request.url

Try to print bottle.request.environ and check if the environ variables are
right. Which server adapter are you using?


Felix

unread,
Mar 28, 2012, 10:28:54 PM3/28/12
to bottlepy
I updated bottle to 0.10.9
now if I tried to print bottle.request.url I get
Traceback (most recent call last):
  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 737, in
_handle
    return route.call(**args)
  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 1456, in
wrapper
    rv = callback(*a, **ka)
  File "/Users/g/p/taotie/code/backend/REST/oauth.py", line 80, in
wrapper
    print bottle.request.url
  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 1044, in
url
    return self.urlparts.geturl()
  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 155, in
__get__
    key, storage = self.key, getattr(obj, self.attr)
AttributeError: 'LocalRequest' object has no attribute 'environ'


if I tried to print bottle.request.environ I get

  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 737, in
_handle
    return route.call(**args)
  File "/Users/g/p/taotie/code/backend/REST/bottle.py", line 1456, in
wrapper
    rv = callback(*a, **ka)
  File "/Users/g/p/taotie/code/backend/REST/oauth.py", line 81, in
wrapper
    print bottle.request.environ
AttributeError: 'LocalRequest' object has no attribute 'environ'

what is going on?

On Mar 26, 10:53 am, m...@gsites.de wrote:
> > Hi all,
> > I have some basic question on how a decorator in the apply list can
> > get the current route information.
>
> > @route("/yo", apply=[basic_oauth])
> > def yo():
> >     return "YO"
>
> > def basic_oauth(func):
> >     """
> >     handle the basic oauth case
> >     """
> >     def wrapper(*arg, **kargs):
> >         print bottle.request.url
> >         return func(**kargs)
> >     return wrapper
>
> Your example looks perfectly fine. And works as expected on my machine.
>
> > the request.url returned ishttp://127.0.0.1/without the yo in the

Marcel Hellkamp

unread,
Mar 29, 2012, 7:21:28 AM3/29/12
to bott...@googlegroups.com
Am 29.03.2012 04:28, schrieb Felix:
> I updated bottle to 0.10.9
> now if I tried to print bottle.request.url I get
> AttributeError: 'LocalRequest' object has no attribute 'environ'

bottle.request.url only works during a request. You are accessing it
outside of a request/response cycle. Are you sure your example is accurate?

Felix

unread,
Mar 31, 2012, 12:47:05 AM3/31/12
to bottlepy
i figure out the problem, the basic_oauth function is defined in a
different module oauth.py while the actual bottle routes are defined
in another module called rest_api.py. If I move the code from oauth.py
to rest_api.py everything is fine.  Now the question is how to make
the code work when the actual function is defined else where.
Reply all
Reply to author
Forward
0 new messages