Skip to content

Commit

Permalink
modified logger logic and stream request
Browse files Browse the repository at this point in the history
Signed-off-by: pem70 <[email protected]>
  • Loading branch information
pem70 committed May 23, 2024
1 parent adcfe12 commit 4d1fa81
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 40 deletions.
5 changes: 5 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/profile_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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}'")

Check warning on line 203 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L203

Added line #L203 was not covered by tests
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}'")

Check warning on line 209 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L209

Added line #L209 was not covered by tests
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}'")

Check warning on line 216 in src/core/zowe/core_for_zowe_sdk/profile_manager.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/profile_manager.py#L216

Added line #L216 was not covered by tests
warnings.warn(
f"Could not load '{cfg.filename}' at '{cfg.filepath}'" f"because {type(exc).__name__}'{exc}'.",
ConfigNotFoundWarning,
Expand Down
35 changes: 7 additions & 28 deletions src/core/zowe/core_for_zowe_sdk/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/core/zowe/core_for_zowe_sdk/sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/zos_console/zowe/zos_console_for_zowe_sdk/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Check warning on line 455 in src/zos_files/zowe/zos_files_for_zowe_sdk/files.py

View check run for this annotation

Codecov / codecov/patch

src/zos_files/zowe/zos_files_for_zowe_sdk/files.py#L455

Added line #L455 was not covered by tests
return response

def get_dsn_binary_content(self, dataset_name, with_prefixes=False):
Expand Down Expand Up @@ -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)

Check warning on line 503 in src/zos_files/zowe/zos_files_for_zowe_sdk/files.py

View check run for this annotation

Codecov / codecov/patch

src/zos_files/zowe/zos_files_for_zowe_sdk/files.py#L503

Added line #L503 was not covered by tests
return response

def write_to_dsn(self, dataset_name, data, encoding=_ZOWE_FILES_DEFAULT_ENCODING):
Expand Down Expand Up @@ -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)

Check warning on line 587 in src/zos_files/zowe/zos_files_for_zowe_sdk/files.py

View check run for this annotation

Codecov / codecov/patch

src/zos_files/zowe/zos_files_for_zowe_sdk/files.py#L587

Added line #L587 was not covered by tests
return response

def download_uss(self, file_path, output_file, binary=False):
Expand Down
2 changes: 1 addition & 1 deletion src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/zos_tso/zowe/zos_tso_for_zowe_sdk/tso.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
2 changes: 1 addition & 1 deletion src/zosmf/zowe/zosmf_for_zowe_sdk/zosmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_zowe_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down

0 comments on commit 4d1fa81

Please sign in to comment.