-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathinit.py
More file actions
109 lines (88 loc) · 3.32 KB
/
init.py
File metadata and controls
109 lines (88 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Cloudsmith API - Initialisation."""
import base64
from typing import Type, TypeVar
import click
import cloudsmith_api
from ..rest import RestClient
def initialise_api(
debug=False,
host=None,
credential=None,
proxy=None,
ssl_verify=True,
user_agent=None,
headers=None,
rate_limit=True,
rate_limit_callback=None,
error_retry_max=None,
error_retry_backoff=None,
error_retry_codes=None,
error_retry_cb=None,
):
"""Initialise the cloudsmith_api.Configuration."""
# FIXME: pylint: disable=too-many-arguments
config = cloudsmith_api.Configuration()
config.debug = debug
config.host = host if host else config.host
config.proxy = proxy if proxy else config.proxy
config.user_agent = user_agent
config.headers = headers if headers else {}
config.rate_limit = rate_limit
config.rate_limit_callback = rate_limit_callback
config.error_retry_max = error_retry_max
config.error_retry_backoff = error_retry_backoff
config.error_retry_codes = error_retry_codes
config.error_retry_cb = error_retry_cb
config.verify_ssl = ssl_verify
config.client_side_validation = False
if credential:
if credential.auth_type == "bearer":
config.headers["Authorization"] = f"Bearer {credential.api_key}"
if config.debug:
click.echo("SSO access token config value set")
else:
config.api_key["X-Api-Key"] = credential.api_key
if config.debug:
click.echo("User API key config value set")
auth_header = headers and config.headers.get("Authorization")
if auth_header and " " in auth_header:
auth_type, encoded = auth_header.split(" ", 1)
if auth_type == "Basic":
decoded = base64.b64decode(encoded)
values = decoded.decode("utf-8")
config.username, config.password = values.split(":")
if config.debug:
click.echo("Username and password config values set")
# Important! Some of the attributes set above (e.g. error_retry_max) are not
# present in the cloudsmith_api.Configuration class declaration.
# By calling the set_default() method, we ensure that future instances of that
# class will include those attributes, and their (default) values.
cloudsmith_api.Configuration.set_default(config)
return config
T = TypeVar("T")
def get_api_client(cls: Type[T]) -> T:
"""Get an API client (with configuration)."""
config = cloudsmith_api.Configuration()
client = cls()
client.config = config
client.api_client.rest_client = RestClient(
error_retry_cb=getattr(config, "error_retry_cb", None),
respect_retry_after_header=getattr(config, "rate_limit", True),
)
user_agent = getattr(config, "user_agent", None)
if user_agent:
client.api_client.user_agent = user_agent
headers = getattr(config, "headers", None)
if headers:
for k, v in headers.items():
client.api_client.set_default_header(k, v)
return client
def unset_api_key():
"""Unset the API key."""
config = cloudsmith_api.Configuration()
try:
del config.api_key["X-Api-Key"]
except KeyError:
pass
if hasattr(cloudsmith_api.Configuration, "set_default"):
cloudsmith_api.Configuration.set_default(config)