From e74bf07236814d222180618f0ebb20b519dd293a Mon Sep 17 00:00:00 2001 From: Mitch Kelly Date: Mon, 20 Feb 2023 15:21:22 +1100 Subject: [PATCH] fix: fixes #174, remove hardcoded URL for creating ZPA sessions refactor: move url_base instance attribute from private to public --- pyzscaler/zpa/__init__.py | 12 ++++++------ pyzscaler/zpa/session.py | 8 +++++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pyzscaler/zpa/__init__.py b/pyzscaler/zpa/__init__.py index e2daa9f..8466e83 100644 --- a/pyzscaler/zpa/__init__.py +++ b/pyzscaler/zpa/__init__.py @@ -73,19 +73,19 @@ def _build_session(self, **kwargs) -> None: # Configure URL base for this API session if self._override_url: - self._url_base = self._override_url + self.url_base = self._override_url elif not self._cloud or self._cloud == "production": - self._url_base = "https://config.private.zscaler.com" + self.url_base = "https://config.private.zscaler.com" elif self._cloud == "beta": - self._url_base = "https://config.zpabeta.net" + self.url_base = "https://config.zpabeta.net" else: raise ValueError("Missing Attribute: You must specify either cloud or override_url") # Configure URLs for this API session - self._url = f"{self._url_base}/mgmtconfig/v1/admin/customers/{self._customer_id}" - self.user_config_url = f"{self._url_base}/userconfig/v1/customers/{self._customer_id}" + self._url = f"{self.url_base}/mgmtconfig/v1/admin/customers/{self._customer_id}" + self.user_config_url = f"{self.url_base}/userconfig/v1/customers/{self._customer_id}" # The v2 URL supports additional API endpoints - self.v2_url = f"{self._url_base}/mgmtconfig/v2/admin/customers/{self._customer_id}" + self.v2_url = f"{self.url_base}/mgmtconfig/v2/admin/customers/{self._customer_id}" self._auth_token = self.session.create_token(client_id=self._client_id, client_secret=self._client_secret) return self._session.headers.update({"Authorization": f"Bearer {self._auth_token}"}) diff --git a/pyzscaler/zpa/session.py b/pyzscaler/zpa/session.py index 753bca7..cea6921 100644 --- a/pyzscaler/zpa/session.py +++ b/pyzscaler/zpa/session.py @@ -1,7 +1,13 @@ +from restfly import APISession from restfly.endpoint import APIEndpoint class AuthenticatedSessionAPI(APIEndpoint): + def __init__(self, api: APISession): + super().__init__(api) + + self.url_base = api.url_base + def create_token(self, client_id: str, client_secret: str): """ Creates a ZPA authentication token. @@ -24,4 +30,4 @@ def create_token(self, client_id: str, client_secret: str): headers = { "Content-Type": "application/x-www-form-urlencoded", } - return self._post("https://config.private.zscaler.com/signin", headers=headers, data=payload).access_token + return self._post(f"{self.url_base}/signin", headers=headers, data=payload).access_token