Skip to content

Commit

Permalink
enable --endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Gatto <matthias.gatto@outscale.com>
  • Loading branch information
outscale-mgo committed Jan 30, 2023
1 parent 748155f commit 7fe24e5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions osc_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ class ApiCall:
date: Optional[str] = field(default=None, init=False)
datestamp: Optional[str] = field(default=None, init=False)

endpoint: Optional[str] = None
method: str = DEFAULT_METHOD
endpoint: str = ""
host: str = ""
access_key: Optional[str] = None
secret_key: Optional[str] = None
Expand Down Expand Up @@ -242,7 +242,7 @@ def __post_init__(self):
if parsed_url.scheme:
self.host = parsed_url.netloc
else:
self.host = self.endpoint
self.host = str(self.endpoint)
self.endpoint = f"{self.protocol}://{self.endpoint}"

def check_authentication_options(self):
Expand All @@ -269,7 +269,7 @@ def set_datestamp(self):
def get_url(
self, call: str, encoded_request_params: EncodedCallParameters = None
) -> str:
value = self.endpoint
value = self.endpoint if self.endpoint else ""
if self.method == "GET":
value += f"?{encoded_request_params}"
return value
Expand Down Expand Up @@ -674,7 +674,7 @@ def get_canonical_uri(self, call: str) -> str:
def get_url(
self, call: str, encoded_request_params: EncodedCallParameters = None
) -> str:
return "/".join([self.endpoint, self.get_canonical_uri(call)])
return "/".join([str(self.endpoint), self.get_canonical_uri(call)])

def get_password_params(self) -> PasswordParams:
# Don't put any auth parameters in body
Expand Down Expand Up @@ -708,6 +708,11 @@ def build_headers(self, target: str, _) -> Headers:
return signed_headers, canonical_headers, headers


def patch_conf(conf: Configuration, endpoint: Optional[str] = None) -> Configuration:
if endpoint:
conf['endpoint'] = endpoint
return conf

def get_conf(profile: str) -> Configuration:
# Check which conf_path is used.
conf_path = next((path for path in CONF_PATHS if path.exists()), None)
Expand Down Expand Up @@ -747,6 +752,7 @@ def api_connect(
call: str,
profile: str = DEFAULT_PROFILE,
login: Optional[str] = None,
endpoint: Optional[str] = None,
password: Optional[str] = None,
authentication_method: Optional[str] = None,
bash_completion: bool = False,
Expand All @@ -764,8 +770,13 @@ def api_connect(
}

handler = calls[service](
profile, login, PASSWORD_ARG, authentication_method, **get_conf(profile)
profile,
login,
PASSWORD_ARG,
authentication_method,
**patch_conf(get_conf(profile), endpoint),
)

handler.make_request(call, **kwargs)
if handler.response:
print(json.dumps(handler.response, indent=4))
Expand Down

0 comments on commit 7fe24e5

Please sign in to comment.