Skip to content

Commit

Permalink
Merge pull request #283 from aadityasinha-dotcom/context_manager
Browse files Browse the repository at this point in the history
Turned SDK APIs into context manager
  • Loading branch information
t1m0thyj authored Jun 17, 2024
2 parents 3a15fc1 + 8d20b51 commit 8ac7348
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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

Expand Down
7 changes: 6 additions & 1 deletion src/core/zowe/core_for_zowe_sdk/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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()
Expand Down Expand Up @@ -97,11 +98,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.
Expand Down
6 changes: 6 additions & 0 deletions src/core/zowe/core_for_zowe_sdk/sdk_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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, exc_type, exception, traceback):
del self.request_handler

def _create_custom_request_arguments(self):
"""Create a copy of the default request arguments dictionary.
Expand Down
4 changes: 2 additions & 2 deletions src/zos_console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
```
4 changes: 2 additions & 2 deletions src/zos_files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
```
14 changes: 7 additions & 7 deletions src/zos_jobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
```

<strong>Get jobs by owner</strong>
Expand All @@ -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))
```

<strong>Submit a job from mainframe</strong>
Expand All @@ -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)"))
```
18 changes: 9 additions & 9 deletions src/zos_tso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```

<strong>Demonstrate starting, pinging, and stopping a TSO address space</strong>
Expand All @@ -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))
```

0 comments on commit 8ac7348

Please sign in to comment.