Skip to content
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

AuthHandler example in docs shows wrong code #329

Open
stphnthiel opened this issue Jul 20, 2022 · 0 comments
Open

AuthHandler example in docs shows wrong code #329

stphnthiel opened this issue Jul 20, 2022 · 0 comments
Labels
Documentation Low Hanging Fruit Pull Request Welcome Project would be willing to review a pr to fix this

Comments

@stphnthiel
Copy link

This docs page on AuthHandlers misses one line of code to be working

import requests
from requests_toolbelt.auth.handler import AuthHandler

def gitlab_auth(request):
    request.headers['PRIVATE-TOKEN'] = 'asecrettoken'
    return request --> add this line!

handler = AuthHandler({
    'https://api.github.com': ('sigmavirus24', 'apassword'),
    'https://gitlab.com': gitlab_auth,
})

session = requests.Session()
session.auth = handler
r = session.get('https://api.github.com/user')
# assert r.ok
r2 = session.get('https://gitlab.com/api/v3/projects')
# assert r2.ok

The current example gives the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [17], in <cell line: 1>()
----> 1 r = session.get("some-url")

File python3.8/site-packages/requests/sessions.py:555, in Session.get(self, url, **kwargs)
    547 r"""Sends a GET request. Returns :class:`Response` object.
    548 
    549 :param url: URL for the new :class:`Request` object.
    550 :param \*\*kwargs: Optional arguments that ``request`` takes.
    551 :rtype: requests.Response
    552 """
    554 kwargs.setdefault('allow_redirects', True)
--> 555 return self.request('GET', url, **kwargs)

File python3.8/site-packages/requests_toolbelt/sessions.py:64, in BaseUrlSession.request(self, method, url, *args, **kwargs)
     62 """Send the request after generating the complete URL."""
     63 url = self.create_url(url)
---> 64 return super(BaseUrlSession, self).request(
     65     method, url, *args, **kwargs
     66 )

File python3.8/site-packages/requests/sessions.py:528, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    515 # Create the Request.
    516 req = Request(
    517     method=method.upper(),
    518     url=url,
   (...)
    526     hooks=hooks,
    527 )
--> 528 prep = self.prepare_request(req)
    530 proxies = proxies or {}
    532 settings = self.merge_environment_settings(
    533     prep.url, proxies, stream, verify, cert
    534 )

File python3.8/site-packages/requests/sessions.py:456, in Session.prepare_request(self, request)
    453     auth = get_netrc_auth(request.url)
    455 p = PreparedRequest()
--> 456 p.prepare(
    457     method=request.method.upper(),
    458     url=request.url,
    459     files=request.files,
    460     data=request.data,
    461     json=request.json,
    462     headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),
    463     params=merge_setting(request.params, self.params),
    464     auth=merge_setting(auth, self.auth),
    465     cookies=merged_cookies,
    466     hooks=merge_hooks(request.hooks, self.hooks),
    467 )
    468 return p

File python3.8/site-packages/requests/models.py:320, in PreparedRequest.prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json)
    318 self.prepare_cookies(cookies)
    319 self.prepare_body(data, files, json)
--> 320 self.prepare_auth(auth, url)
    322 # Note that prepare_auth must be last to enable authentication schemes
    323 # such as OAuth to work on a fully prepared request.
    324 
    325 # This MUST go after prepare_auth. Authenticators could add a hook
    326 self.prepare_hooks(hooks)

File python3.8/site-packages/requests/models.py:559, in PreparedRequest.prepare_auth(self, auth, url)
    556 r = auth(self)
    558 # Update self to reflect the auth changes.
--> 559 self.__dict__.update(r.__dict__)
    561 # Recompute Content-Length
    562 self.prepare_content_length(self.body)

AttributeError: 'NoneType' object has no attribute '__dict__'

Including that here so people find it when Googling.

@sigmavirus24 sigmavirus24 added Low Hanging Fruit Documentation Pull Request Welcome Project would be willing to review a pr to fix this labels Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Low Hanging Fruit Pull Request Welcome Project would be willing to review a pr to fix this
Projects
None yet
Development

No branches or pull requests

2 participants