Query string authenticators for Requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Requests' authenticator that attaches given parameters | |
to the URL query string. | |
License: Public Domain. | |
""" | |
from requests.auth import AuthBase | |
class QueryStringAuth(AuthBase): | |
"""Authenticator that attaches a set of query string parameters | |
(e.g. an API key) to the request. | |
""" | |
def __init__(self, **params): | |
self.params = {} | |
for name, value in params.items(): | |
if value is None: | |
continue # None means 'no value' in Requests, too | |
if name.endswith('[]'): | |
name = name[:-2] | |
self.params[name] = value | |
def __call__(self, request): | |
if self.params: | |
request.prepare_url(request.url, self.params) | |
return request |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment