Authentication
The auth
parameter of the Consumer
constructor offers a way to
define an auth method to use for all requests.
uplink.auth.BasicAuth
uplink.auth.ProxyAuth
Authorizes requests with an intermediate HTTP proxy.
If both API auth and intermediate Proxy auth are required, wrap ProxyAuth in MultiAuth:
# only ProxyAuth
github = Github(BASE_URL, auth=ProxyAuth(PROXYUSER, PROXYPASS))
# both BasicAuth and ProxyAuth
auth_methods = MultiAuth(
BasicAuth(USER, PASS),
ProxyAuth(PROXYUSER, PROXYPASS)
)
github = GitHub(BASE_URL, auth=auth_methods)
Source code in uplink/auth.py
uplink.auth.BearerToken
uplink.auth.MultiAuth
Authorizes requests using multiple auth methods at the same time.
This is useful for API users to supply both API credentials and intermediary credentials (such as for a proxy).
auth_methods = MultiAuth(
BasicAuth(USER, PASS),
ProxyAuth(PROXY_USER, PROXY_PASS)
)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)
This may also be used if an API requires multiple Auth Tokens.
auth_methods = MultiAuth(
BearerToken(API_TOKEN),
ApiTokenParam(QUERY_PARAMETER_NAME, QUERY_PARAMETER_VALUE),
ApiTokenHeader(API_HEADER_NAME, API_TOKEN_2)
)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)
API library authors may find it more helpful to treat MultiAuth as
a list using append
or extend
to add aditional auth methods.
auth_methods = MultiAuth()
auth_methods.append(BearerToken(API_TOKEN))
auth_methods.extend([
ApiTokenParam(QUERY_PARAMETER_NAME, QUERY_PARAMETER_VALUE),
ApiTokenHeader(API_HEADER_NAME, API_TOKEN_2)
])
api_consumer = SomeApiConsumerClass(BASE_URL, auth=auth_methods)
# looping over contained auth methods is also supported
for method in auth_methods:
print(method.__class__.__name__)
Source code in uplink/auth.py
uplink.auth.ApiTokenParam
Authorizes requests using a token or key in a query parameter.
Users may use this directly, or API library authors may subclass this
to predefine the query parameter name to use. If supplying query parameter
name on a subclass, define the _param
property or attribute and
override __init__()
without using super()
.
Examples:
Direct use:
token_param = ApiTokenParam(QUERY_PARAM_NAME, TOKEN)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_param)
Subclass in API library:
class ExampleApiTokenParam(ApiTokenParam):
_param = "api-token"
def __init__(self, token):
self._param_value = token
# using the subclass
token_param = ExampleApiTokenParam(TOKEN)
api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_param)