Create a gist now

Instantly share code, notes, and snippets.

@Xion /auth.py
Last active Dec 13, 2017

What would you like to do?
Query string authenticators for Requests
"""
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