Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move call to get_conf out of ApiCall init (fixed) #117

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 27 additions & 33 deletions osc_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,56 +160,43 @@ class ApiCall:
date: Optional[str] = field(default=None, init=False)
datestamp: Optional[str] = field(default=None, init=False)

method: str = field(default="", init=False)
endpoint: str = field(default="", init=False)
host: str = field(default="", init=False)
access_key: Optional[str] = field(default=None, init=False)
secret_key: Optional[str] = field(default=None, init=False)
version: str = field(default=DEFAULT_VERSION, init=False)
protocol: str = field(default="", init=False)
region: str = field(default=DEFAULT_REGION, init=False)
ssl_verify: bool = field(default=SSL_VERIFY, init=False)
client_certificate: Optional[str] = field(default=None, init=False)
method: str = ""
endpoint: str = ""
host: str = ""
access_key: Optional[str] = None
secret_key: Optional[str] = None
version: str = DEFAULT_VERSION
https: bool = False
protocol: str = ""
region_name: str = DEFAULT_REGION
ssl_verify: bool = SSL_VERIFY
client_certificate: Optional[str] = None

def __post_init__(self):
if not self.API_NAME:
raise RuntimeError("API_NAME is required and should not be empty")

self.setup_profile_options(self.profile)
self.check_authentication_options()

def setup_profile_options(self, profile: str):
conf = get_conf(profile)

self.method = conf.get("method", DEFAULT_METHOD)
if self.method not in METHODS_SUPPORTED:
raise Exception(
f"Wrong method {self.method}. Supported: {METHODS_SUPPORTED}."
)

self.access_key = conf.get("access_key")
self.secret_key = conf.get("secret_key")
self.version = conf.get("version", DEFAULT_VERSION)
self.protocol = "https" if conf.get("https", False) else "http"
self.region = conf.get("region_name", DEFAULT_REGION)
self.ssl_verify = conf.get("ssl_verify", SSL_VERIFY)
self.client_certificate = conf.get("client_certificate")
self.protocol = "https" if self.https else "http"

endpoint = conf.get("endpoint")
host = conf.get("host", "")
if host and not endpoint:
endpoint = ".".join([self.API_NAME, self.region, host])
if self.host and not self.endpoint:
self.endpoint = ".".join([self.API_NAME, self.region_name, self.host])

if not endpoint:
if not self.endpoint:
raise RuntimeError("No endpoint found")

parsed_url = urllib.parse.urlparse(endpoint)
parsed_url = urllib.parse.urlparse(self.endpoint)
if parsed_url.scheme:
self.endpoint = endpoint
self.host = parsed_url.netloc
else:
self.endpoint = f"{self.protocol}://{endpoint}"
self.host = endpoint
self.host = self.endpoint
self.endpoint = f"{self.protocol}://{self.endpoint}"

def check_authentication_options(self):
if self.authentication_method not in {"accesskey", "password"}:
Expand Down Expand Up @@ -251,7 +238,12 @@ def get_authorization_header(
if not self.secret_key:
raise RuntimeError("Secret key is needed to authorize call")

credentials = [self.datestamp, self.region, self.API_NAME, self.REQUEST_TYPE]
credentials = [
self.datestamp,
self.region_name,
self.API_NAME,
self.REQUEST_TYPE,
]
credential_scope = "/".join(credentials)
string_to_sign = "\n".join(
[
Expand Down Expand Up @@ -682,7 +674,9 @@ def api_connect(
"lbu": LbuCall,
"okms": OKMSCall,
}
handler = calls[service](profile, login, password, authentication_method)
handler = calls[service](
profile, login, password, authentication_method, **get_conf(profile)
)
handler.make_request(call, **kwargs)
if handler.response:
print(json.dumps(handler.response, indent=4))
Expand Down