From 4d1fa813daf2c56ed746ac365283b6818a74a35e Mon Sep 17 00:00:00 2001 From: pem70 Date: Thu, 23 May 2024 13:35:47 -0400 Subject: [PATCH] modified logger logic and stream request Signed-off-by: pem70 --- .../zowe/core_for_zowe_sdk/profile_manager.py | 5 +++ .../zowe/core_for_zowe_sdk/request_handler.py | 35 ++++--------------- src/core/zowe/core_for_zowe_sdk/sdk_api.py | 6 ++-- .../zowe/zos_console_for_zowe_sdk/console.py | 2 +- .../zowe/zos_files_for_zowe_sdk/files.py | 8 ++--- .../zowe/zos_jobs_for_zowe_sdk/jobs.py | 2 +- src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py | 2 +- src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py | 2 +- tests/unit/test_zowe_core.py | 2 +- 9 files changed, 24 insertions(+), 40 deletions(-) diff --git a/src/core/zowe/core_for_zowe_sdk/profile_manager.py b/src/core/zowe/core_for_zowe_sdk/profile_manager.py index 20a34a64..76daa13f 100644 --- a/src/core/zowe/core_for_zowe_sdk/profile_manager.py +++ b/src/core/zowe/core_for_zowe_sdk/profile_manager.py @@ -177,6 +177,8 @@ def get_profile( NamedTuple (data, name, secure_props_not_found) """ + logger = logging.getLogger(__name__) + cfg_profile = Profile() try: cfg_profile = cfg.get_profile( @@ -198,17 +200,20 @@ def get_profile( raise jsonschema.exceptions.FormatError(f"Validating a format config_json failed for schema_json, {exc}") except ProfileNotFound: if profile_name: + logger.warning(f"Profile '{profile_name}' not found in file '{cfg.filename}'") warnings.warn( f"Profile '{profile_name}' not found in file '{cfg.filename}', returning empty profile instead.", ProfileNotFoundWarning, ) else: + logger.warning(f"Profile of type '{profile_type}' not found in file '{cfg.filename}'") warnings.warn( f"Profile of type '{profile_type}' not found in file '{cfg.filename}', returning empty profile" f" instead.", ProfileNotFoundWarning, ) except Exception as exc: + logger.warning(f"Could not load '{cfg.filename}' at '{cfg.filepath}'" f"because {type(exc).__name__}'{exc}'") warnings.warn( f"Could not load '{cfg.filename}' at '{cfg.filepath}'" f"because {type(exc).__name__}'{exc}'.", ConfigNotFoundWarning, diff --git a/src/core/zowe/core_for_zowe_sdk/request_handler.py b/src/core/zowe/core_for_zowe_sdk/request_handler.py index 7f3ac1c6..395bbe6e 100644 --- a/src/core/zowe/core_for_zowe_sdk/request_handler.py +++ b/src/core/zowe/core_for_zowe_sdk/request_handler.py @@ -29,7 +29,7 @@ class RequestHandler: List of supported request methods """ - def __init__(self, session_arguments): + def __init__(self, session_arguments, logger_name = __name__): """ Construct a RequestHandler object. @@ -41,14 +41,14 @@ def __init__(self, session_arguments): self.session_arguments = session_arguments self.valid_methods = ["GET", "POST", "PUT", "DELETE"] self.__handle_ssl_warnings() - self.__logger = logging.getLogger(__name__) + self.__logger = logging.getLogger(logger_name) def __handle_ssl_warnings(self): """Turn off warnings if the SSL verification argument if off.""" if not self.session_arguments["verify"]: urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) - def perform_request(self, method, request_arguments, expected_code=[200]): + def perform_request(self, method, request_arguments, expected_code=[200], stream = False): """Execute an HTTP/HTTPS requests from given arguments and return validated response (JSON). Parameters @@ -68,35 +68,14 @@ def perform_request(self, method, request_arguments, expected_code=[200]): self.method = method self.request_arguments = request_arguments self.expected_code = expected_code + self.__logger.debug(f"Request method: {self.method}, Request arguments: {self.request_arguments}, Expected code: {expected_code}") self.__validate_method() - self.__send_request() + self.__send_request(stream = stream) self.__validate_response() + if stream: + return self.response return self.__normalize_response() - def perform_streamed_request(self, method, request_arguments, expected_code=[200]): - """Execute a streamed HTTP/HTTPS requests from given arguments and return a raw response. - - Parameters - ---------- - method: str - The request method that should be used - request_arguments: dict - The dictionary containing the required arguments for the execution of the request - expected_code: int - The list containing the acceptable response codes (default is [200]) - - Returns - ------- - A raw response data - """ - self.method = method - self.request_arguments = request_arguments - self.expected_code = expected_code - self.__validate_method() - self.__send_request(stream=True) - self.__validate_response() - return self.response - def __validate_method(self): """Check if the input request method for the request is supported. diff --git a/src/core/zowe/core_for_zowe_sdk/sdk_api.py b/src/core/zowe/core_for_zowe_sdk/sdk_api.py index 65751ce1..edf24c11 100644 --- a/src/core/zowe/core_for_zowe_sdk/sdk_api.py +++ b/src/core/zowe/core_for_zowe_sdk/sdk_api.py @@ -24,12 +24,12 @@ class SdkApi: Abstract class used to represent the base SDK API. """ - def __init__(self, profile, default_url): + def __init__(self, profile, default_url, logger_name = __name__): self.profile = profile session = Session(profile) self.session: ISession = session.load() - self.logger = logging.getLogger(__name__) + self.logger = logging.getLogger(logger_name) self.default_service_url = default_url self.default_headers = { @@ -47,7 +47,7 @@ def __init__(self, profile, default_url): "verify": self.session.rejectUnauthorized, "timeout": 30, } - self.request_handler = RequestHandler(self.session_arguments) + self.request_handler = RequestHandler(self.session_arguments, logger_name = logger_name) if self.session.type == session_constants.AUTH_TYPE_BASIC: self.request_arguments["auth"] = (self.session.user, self.session.password) diff --git a/src/zos_console/zowe/zos_console_for_zowe_sdk/console.py b/src/zos_console/zowe/zos_console_for_zowe_sdk/console.py index 59817c86..42e5a3d8 100644 --- a/src/zos_console/zowe/zos_console_for_zowe_sdk/console.py +++ b/src/zos_console/zowe/zos_console_for_zowe_sdk/console.py @@ -22,7 +22,7 @@ def __init__(self, connection): connection The connection object """ - super().__init__(connection, "/zosmf/restconsoles/consoles/defcn") + super().__init__(connection, "/zosmf/restconsoles/consoles/defcn", logger_name=__name__) def issue_command(self, command, console=None): """Issues a command on z/OS Console. diff --git a/src/zos_files/zowe/zos_files_for_zowe_sdk/files.py b/src/zos_files/zowe/zos_files_for_zowe_sdk/files.py index f45ad57e..688c24e8 100644 --- a/src/zos_files/zowe/zos_files_for_zowe_sdk/files.py +++ b/src/zos_files/zowe/zos_files_for_zowe_sdk/files.py @@ -43,7 +43,7 @@ def __init__(self, connection): Also update header to accept gzip encoded responses """ - super().__init__(connection, "/zosmf/restfiles/") + super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__) self.default_headers["Accept-Encoding"] = "gzip" def list_files(self, path): @@ -452,7 +452,7 @@ def get_dsn_content_streamed(self, dataset_name): """ custom_args = self._create_custom_request_arguments() custom_args["url"] = "{}ds/{}".format(self.request_endpoint, self._encode_uri_component(dataset_name)) - response = self.request_handler.perform_streamed_request("GET", custom_args) + response = self.request_handler.perform_request("GET", custom_args, stream = True) return response def get_dsn_binary_content(self, dataset_name, with_prefixes=False): @@ -500,7 +500,7 @@ def get_dsn_binary_content_streamed(self, dataset_name, with_prefixes=False): custom_args["headers"]["X-IBM-Data-Type"] = "record" else: custom_args["headers"]["X-IBM-Data-Type"] = "binary" - response = self.request_handler.perform_streamed_request("GET", custom_args) + response = self.request_handler.perform_request("GET", custom_args, stream = True) return response def write_to_dsn(self, dataset_name, data, encoding=_ZOWE_FILES_DEFAULT_ENCODING): @@ -584,7 +584,7 @@ def get_file_content_streamed(self, file_path, binary=False): custom_args["url"] = "{}fs/{}".format(self.request_endpoint, self._encode_uri_component(file_path.lstrip("/"))) if binary: custom_args["headers"]["X-IBM-Data-Type"] = "binary" - response = self.request_handler.perform_streamed_request("GET", custom_args) + response = self.request_handler.perform_request("GET", custom_args, stream=True) return response def download_uss(self, file_path, output_file, binary=False): diff --git a/src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py b/src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py index ae4319e1..bcceb8d1 100644 --- a/src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py +++ b/src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py @@ -34,7 +34,7 @@ def __init__(self, connection): connection The connection object """ - super().__init__(connection, "/zosmf/restjobs/jobs/") + super().__init__(connection, "/zosmf/restjobs/jobs/", logger_name=__name__) def get_job_status(self, jobname, jobid): """Retrieve the status of a given job on JES. diff --git a/src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py b/src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py index 2a882f3f..53e273c8 100644 --- a/src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py +++ b/src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py @@ -38,7 +38,7 @@ def __init__(self, connection, tso_profile=None): connection The connection object """ - super().__init__(connection, "/zosmf/tsoApp/tso") + super().__init__(connection, "/zosmf/tsoApp/tso", logger_name=__name__) self.session_not_found = constants["TsoSessionNotFound"] self.tso_profile = tso_profile or {} diff --git a/src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py b/src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py index 22e4fa65..577e3fd3 100644 --- a/src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py +++ b/src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py @@ -31,7 +31,7 @@ def __init__(self, connection): connection The z/OSMF connection object (generated by the ZoweSDK object) """ - super().__init__(connection, "/zosmf/info") + super().__init__(connection, "/zosmf/info", logger_name=__name__) def get_info(self): """Return a JSON response from the GET request to z/OSMF info endpoint. diff --git a/tests/unit/test_zowe_core.py b/tests/unit/test_zowe_core.py index 7b1d1188..2c31da64 100644 --- a/tests/unit/test_zowe_core.py +++ b/tests/unit/test_zowe_core.py @@ -155,7 +155,7 @@ def test_perform_streamed_request(self, mock_send_request): """Performing a streamed request should call 'send_request' method""" mock_send_request.return_value = mock.Mock(status_code=200) request_handler = RequestHandler(self.session_arguments) - request_handler.perform_streamed_request("GET", {"url": "https://www.zowe.org"}) + request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, stream = True) mock_send_request.assert_called_once() self.assertTrue(mock_send_request.call_args[1]["stream"])