From cc7d0a88e5ac44645291fff8e67614abd0011477 Mon Sep 17 00:00:00 2001 From: aadityasinha-dotcom Date: Fri, 7 Jun 2024 13:33:09 +0530 Subject: [PATCH 1/4] Turned SDK Apis to context manager Signed-off-by: aadityasinha-dotcom --- src/core/zowe/core_for_zowe_sdk/request_handler.py | 7 ++++++- src/core/zowe/core_for_zowe_sdk/sdk_api.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) 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 20791ceb..706e5b43 100644 --- a/src/core/zowe/core_for_zowe_sdk/request_handler.py +++ b/src/core/zowe/core_for_zowe_sdk/request_handler.py @@ -42,6 +42,7 @@ def __init__(self, session_arguments, logger_name = __name__): logger_name The logger name of the modules calling request handler """ + self.session = requests.Session() self.session_arguments = session_arguments self.valid_methods = ["GET", "POST", "PUT", "DELETE"] self.__handle_ssl_warnings() @@ -96,11 +97,15 @@ def __validate_method(self): def __send_request(self, stream=False): """Build a custom session object, prepare it with a custom request and send it.""" - session = requests.Session() + session = self.session request_object = requests.Request(method=self.method, **self.request_arguments) prepared = session.prepare_request(request_object) self.response = session.send(prepared, stream=stream, **self.session_arguments) + def __del__(self): + """Clean up the REST session object once it is no longer needed anymore""" + self.session.close() + def __validate_response(self): """Validate if request response is acceptable based on expected code list. 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 dae4fc29..76b4111f 100644 --- a/src/core/zowe/core_for_zowe_sdk/sdk_api.py +++ b/src/core/zowe/core_for_zowe_sdk/sdk_api.py @@ -56,6 +56,12 @@ def __init__(self, profile, default_url, logger_name = __name__): self.default_headers["Authorization"] = f"Bearer {self.session.tokenValue}" elif self.session.type == session_constants.AUTH_TYPE_TOKEN: self.default_headers["Cookie"] = f"{self.session.tokenType}={self.session.tokenValue}" + + def __enter__(self): + return self + + def __exit__(self): + del self.request_handler def _create_custom_request_arguments(self): """Create a copy of the default request arguments dictionary. From 605ab4737597a0b6a9e3542de61bb528cb5e75ed Mon Sep 17 00:00:00 2001 From: aadityasinha-dotcom Date: Fri, 7 Jun 2024 13:37:12 +0530 Subject: [PATCH 2/4] added changes to changelog.md Signed-off-by: aadityasinha-dotcom --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bc3d0ae..494e9836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,7 @@ All notable changes to the Zowe Client Python SDK will be documented in this fil - Added method to load profile properties from environment variables [#136](https://github.com/zowe/zowe-client-python-sdk/issues/136) - Added validation of zowe.config.json file matching the schema [#192](https://github.com/zowe/zowe-client-python-sdk/issues/192) - Added Secrets SDK for storing client secrets in OS keyring [#208](https://github.com/zowe/zowe-client-python-sdk/issues/208) +- Turned SDK APIs into context manager [#145](https://github.com/zowe/zowe-client-python-sdk/issues/145) ### Bug Fixes From f6d9e23a558a43baf1bde9ffced86337095bf1c5 Mon Sep 17 00:00:00 2001 From: Aaditya Sinha <75474786+aadityasinha-dotcom@users.noreply.github.com> Date: Mon, 10 Jun 2024 22:38:46 +0530 Subject: [PATCH 3/4] Update src/core/zowe/core_for_zowe_sdk/sdk_api.py Co-authored-by: Timothy Johnson Signed-off-by: Aaditya Sinha <75474786+aadityasinha-dotcom@users.noreply.github.com> --- src/core/zowe/core_for_zowe_sdk/sdk_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 76b4111f..a67721b9 100644 --- a/src/core/zowe/core_for_zowe_sdk/sdk_api.py +++ b/src/core/zowe/core_for_zowe_sdk/sdk_api.py @@ -60,7 +60,7 @@ def __init__(self, profile, default_url, logger_name = __name__): def __enter__(self): return self - def __exit__(self): + def __exit__(self, exc_type, exception, traceback): del self.request_handler def _create_custom_request_arguments(self): From 1d995f5d359205431ffc1781d0706fd30a078321 Mon Sep 17 00:00:00 2001 From: aadityasinha-dotcom Date: Tue, 11 Jun 2024 18:14:01 +0530 Subject: [PATCH 4/4] changes to the README.md files for each SDK APIs Signed-off-by: aadityasinha-dotcom --- src/zos_console/README.md | 4 ++-- src/zos_files/README.md | 4 ++-- src/zos_jobs/README.md | 14 +++++++------- src/zos_tso/README.md | 18 +++++++++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/zos_console/README.md b/src/zos_console/README.md index d37afd05..67e9397d 100644 --- a/src/zos_console/README.md +++ b/src/zos_console/README.md @@ -13,7 +13,7 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_console_for_zowe_sdk import Console profile = ProfileManager().load(profile_name="zosmf") -console_info = Console(profile) -print(console_info.issue_command(command="D IPLINFO", console="EMCS")) +with Console(profile) as console_info: + print(console_info.issue_command(command="D IPLINFO", console="EMCS")) ``` diff --git a/src/zos_files/README.md b/src/zos_files/README.md index 346ac3b6..0c421e73 100644 --- a/src/zos_files/README.md +++ b/src/zos_files/README.md @@ -30,7 +30,7 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_files_for_zowe_sdk import Files profile = ProfileManager().load(profile_name="zosmf") -files_info = Files(profile) -print(files_info.delete_data_set(dataset_name="ZOWEUSER.PUBLIC.MY.DATASET.JCL", member_name="MEMBER")) +with Files(profile) as files_info: + print(files_info.delete_data_set(dataset_name="ZOWEUSER.PUBLIC.MY.DATASET.JCL", member_name="MEMBER")) ``` diff --git a/src/zos_jobs/README.md b/src/zos_jobs/README.md index af646de5..3854f739 100644 --- a/src/zos_jobs/README.md +++ b/src/zos_jobs/README.md @@ -25,9 +25,9 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_jobs_for_zowe_sdk import Jobs profile = ProfileManager().load(profile_name="zosmf") -jobs_info = Jobs(profile) -print(jobs_info.delete_job("JOBNAME", "JOBID")) +with Jobs(profile) as jobs_info: + print(jobs_info.delete_job("JOBNAME", "JOBID")) ``` Get jobs by owner @@ -37,10 +37,10 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_jobs_for_zowe_sdk import Jobs profile = ProfileManager().load(profile_name="zosmf") -jobs_info = Jobs(profile) -job_owner = "USERNAME" -print(jobs_info.list_jobs(job_owner)) +with Jobs(profile) as jobs_info: + job_owner = "USERNAME" + print(jobs_info.list_jobs(job_owner)) ``` Submit a job from mainframe @@ -50,7 +50,7 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_jobs_for_zowe_sdk import Jobs profile = ProfileManager().load(profile_name="zosmf") -jobs_info = Jobs(profile) -print(jobs_info.submit_from_mainframe(jcl_path="ZOWEUSER.PUBLIC.MY.DATASET.JCL(MEMBER)")) +with Jobs(profile) as jobs_info: + print(jobs_info.submit_from_mainframe(jcl_path="ZOWEUSER.PUBLIC.MY.DATASET.JCL(MEMBER)")) ``` diff --git a/src/zos_tso/README.md b/src/zos_tso/README.md index cd57c14f..c3778546 100644 --- a/src/zos_tso/README.md +++ b/src/zos_tso/README.md @@ -13,12 +13,12 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_tso_for_zowe_sdk import Tso profile = ProfileManager().load(profile_name="zosmf") -tso_info = Tso(profile) -started_tso_session = tso_info.start_tso_session() +with Tso(profile) as tso_info: + started_tso_session = tso_info.start_tso_session() -issue_command = tso_info.send_tso_message(started_tso_session, message="status") -print(issue_command) + issue_command = tso_info.send_tso_message(started_tso_session, message="status") + print(issue_command) ``` Demonstrate starting, pinging, and stopping a TSO address space @@ -28,12 +28,12 @@ from zowe.core_for_zowe_sdk import ProfileManager from zowe.zos_tso_for_zowe_sdk import Tso profile = ProfileManager().load(profile_name="zosmf") -tso_info = Tso(profile) -started_tso_session = tso_info.start_tso_session() -print(started_tso_session) +with Tso(profile) as tso_info: + started_tso_session = tso_info.start_tso_session() + print(started_tso_session) -print(tso_info.ping_tso_session(started_tso_session)) + print(tso_info.ping_tso_session(started_tso_session)) -print(tso_info.end_tso_session(started_tso_session)) + print(tso_info.end_tso_session(started_tso_session)) ```