Skip to content

Instantly share code, notes, and snippets.

@Xion
Last active October 2, 2022 02:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xion/9c1bbbc287d4443dbaf5 to your computer and use it in GitHub Desktop.
Save Xion/9c1bbbc287d4443dbaf5 to your computer and use it in GitHub Desktop.
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