Auth Methods¶
The auth
parameter of the Consumer
constructor offers a way
to define an auth method to use for all requests.
auth_method = SomeAuthMethod(...)
github = GitHub(BASE_URL, auth=auth_method)
BasicAuth¶
-
class
uplink.auth.
BasicAuth
(username, password)[source]¶ Authorizes requests using HTTP Basic Authentication.
There are two ways to use BasicAuth with a Consumer:
# implicit BasicAuth github = Github(BASE_URL, auth=(USER, PASS)) # explicit BasicAuth github = GitHub(BASE_URL, auth=BasicAuth(USER, PASS))
ProxyAuth¶
-
class
uplink.auth.
ProxyAuth
(username, password)[source]¶ 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)
BearerToken¶
MultiAuth¶
-
class
uplink.auth.
MultiAuth
(*auth_methods)[source]¶ 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
orextend
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__)
ApiTokenParam¶
-
class
uplink.auth.
ApiTokenParam
(param, token)[source]¶ 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 usingsuper()
.# 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)
ApiTokenHeader¶
-
class
uplink.auth.
ApiTokenHeader
(header, token, prefix=None)[source]¶ Authorizes requests using a token or key in a header. Users should subclass this class to define which header is the token header. The subclass may also, optionally, define a token prefix (such as in BearerToken)
Users may use this directly, or API library authors may subclass this to predefine the header name or a prefix to use. If supplying header name or prefix in a subclass, define the
_header
and/or_prefix
properties or attributes and override__init__()
without usingsuper()
.# direct use token_header = ApiTokenHeader(HEADER_NAME, TOKEN) api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_header) # subclass in API library with a prefix class ExampleApiTokenHeader(ApiTokenHeader): _header = "X-Api-Token" def __init__(self, token): self._token = token # subclass in API library with a prefix class ExampleApiTokenHeader(ApiTokenHeader): _header = "X-App-Id" _prefix = "APP" def __init__(self, token): self._token = token # using the subclass token_header = ExampleApiTokenHeader(TOKEN) api_consumer = SomeApiConsumerClass(BASE_URL, auth=token_header)