Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turned SDK APIs into context manager #283

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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 @@ -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()
Expand Down Expand Up @@ -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.

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 @@ -56,6 +56,12 @@
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

Check warning on line 61 in src/core/zowe/core_for_zowe_sdk/sdk_api.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/sdk_api.py#L61

Added line #L61 was not covered by tests

def __exit__(self, exc_type, exception, traceback):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will there be any use of exc_type, exception, traceback?
I am curious 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these parameters could be used to do exception handling inside the __exit__ function 😋
https://realpython.com/python-with-statement/#handling-exceptions-in-a-context-manager

In our case we always want to clean up the request handler and session object, regardless of whether an error has been raised, so I don't think we need to use them at all.

del self.request_handler

Check warning on line 64 in src/core/zowe/core_for_zowe_sdk/sdk_api.py

View check run for this annotation

Codecov / codecov/patch

src/core/zowe/core_for_zowe_sdk/sdk_api.py#L64

Added line #L64 was not covered by tests

def _create_custom_request_arguments(self):
"""Create a copy of the default request arguments dictionary.
Expand Down
Loading