Skip to content

Commit

Permalink
Update REST API response type
Browse files Browse the repository at this point in the history
Signed-off-by: pem70 <[email protected]>
  • Loading branch information
pem70 committed Aug 7, 2024
1 parent 9c42f3b commit 7063507
Show file tree
Hide file tree
Showing 35 changed files with 926 additions and 144 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/sdk-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Lint with pydocstyle
run: pydocstyle --match-dir='^(?!build$).*' --match='^(?!(__init__\.py|setup\.py$)).*\.py$' src
run: pydocstyle --match-dir='^(?!(build|response)$).*' --match='^(?!(__init__\.py|setup\.py$)).*\.py$' src
- name: Lint with pydoclint
run: pydoclint --exclude='.*/build/.*' src
run: pydoclint --exclude='.*/(build|response)/.*' src
- name: Lint with pylint
run: |
# check for Python errors
pylint src --errors-only --disable=E0401,E0611 --ignore=build
# check for lint
pylint ./src --disable=all --enable=C0103,C0301 --ignore=build --max-line-length=127
pylint ./src --disable=all --enable=C0103,C0301 --ignore=build,response --max-line-length=127
- name: Check license headers
run: python scripts/license_header.py src
- name: Test with pytest
Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"./src/zos_console",
"./src/zos_files",
"./src/zos_jobs",
"./src/zosmf"
"./src/zosmf",
"./src/zos_tso"
],
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
Expand Down
11 changes: 11 additions & 0 deletions src/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
"""Zowe Python Client SDK.
This program and the accompanying materials are made available under the terms of the
Eclipse Public License v2.0 which accompanies this distribution, and is available at
https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
"""
__version__ = "1.0.0-dev19"
14 changes: 7 additions & 7 deletions src/core/zowe/core_for_zowe_sdk/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def __handle_ssl_warnings(self):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def perform_request(
self, method: str, request_arguments: dict, expected_code: dict = [200], stream: bool = False
) -> dict:
self, method: str, request_arguments: dict, expected_code: list = [200], stream: bool = False
) -> Union[str, bytes, dict, None]:
"""Execute an HTTP/HTTPS requests from given arguments and return validated response (JSON).
Parameters
Expand All @@ -54,14 +54,14 @@ def perform_request(
The request method that should be used
request_arguments: dict
The dictionary containing the required arguments for the execution of the request
expected_code: dict
expected_code: list
The list containing the acceptable response codes (default is [200])
stream: bool
The boolean value whether the request is stream
Returns
-------
dict
Union[str, bytes, dict, None]
normalized request response in json (dictionary)
"""
self.__method = method
Expand Down Expand Up @@ -136,13 +136,13 @@ def __validate_response(self):
)
raise RequestFailed(self.__response.status_code, output_str)

def __normalize_response(self) -> Union[str, bytes, dict]:
def __normalize_response(self) -> Union[str, bytes, dict, None]:
"""
Normalize the response object to a JSON format.
Returns
-------
Union[str, bytes, dict]
Union[str, bytes, dict, None]
Response object at the format based on Content-Type header:
- `bytes` when the response is binary data
- `str` when the response is plain text
Expand All @@ -152,6 +152,6 @@ def __normalize_response(self) -> Union[str, bytes, dict]:
if content_type == "application/octet-stream":
return self.__response.content
elif content_type and content_type.startswith("application/json"):
return "" if self.__response.text == "" else self.__response.json()
return None if self.__response.text == "" else self.__response.json()
else:
return self.__response.text
14 changes: 8 additions & 6 deletions src/zos_console/zowe/zos_console_for_zowe_sdk/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from zowe.core_for_zowe_sdk import SdkApi

from .response import GetReponseResponse, IssueCommandResponse


class Console(SdkApi):
"""
Expand All @@ -28,7 +30,7 @@ class Console(SdkApi):
def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restconsoles/consoles/defcn", logger_name=__name__)

def issue_command(self, command: str, console: Optional[str] = None) -> dict:
def issue_command(self, command: str, console: Optional[str] = None) -> IssueCommandResponse:
"""Issues a command on z/OS Console.
Parameters
Expand All @@ -40,17 +42,17 @@ def issue_command(self, command: str, console: Optional[str] = None) -> dict:
Returns
-------
dict
IssueCommandResponse
A JSON containing the response from the console command
"""
custom_args = self._create_custom_request_arguments()
custom_args["url"] = self._request_endpoint.replace("defcn", console or "defcn")
request_body = {"cmd": command}
custom_args["json"] = request_body
response_json = self.request_handler.perform_request("PUT", custom_args)
return response_json
return IssueCommandResponse(response_json)

def get_response(self, response_key: str, console: Optional[str] = None) -> dict:
def get_response(self, response_key: str, console: Optional[str] = None) -> GetReponseResponse:
"""
Collect outstanding synchronous z/OS Console response messages.
Expand All @@ -63,11 +65,11 @@ def get_response(self, response_key: str, console: Optional[str] = None) -> dict
Returns
-------
dict
GetReponseResponse
A JSON containing the response to the command
"""
custom_args = self._create_custom_request_arguments()
request_url = "{}/solmsgs/{}".format(console or "defcn", response_key)
custom_args["url"] = self._request_endpoint.replace("defcn", request_url)
response_json = self.request_handler.perform_request("GET", custom_args)
return response_json
return GetReponseResponse(response_json)
12 changes: 12 additions & 0 deletions src/zos_console/zowe/zos_console_for_zowe_sdk/response/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Zowe Python Client SDK.
This program and the accompanying materials are made available under the terms of the
Eclipse Public License v2.0 which accompanies this distribution, and is available at
https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
"""
from .console import GetReponseResponse, IssueCommandResponse
49 changes: 49 additions & 0 deletions src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Zowe Python Client SDK.
This program and the accompanying materials are made available under the terms of the
Eclipse Public License v2.0 which accompanies this distribution, and is available at
https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
"""
from dataclasses import dataclass
from typing import Any, Optional


@dataclass
class IssueCommandResponse:
cmd_response_key: Optional[str] = None
cmd_response_url: Optional[str] = None
cmd_response_uri: Optional[str] = None
cmd_response: Optional[str] = None

def __init__(self, response: dict) -> None:
for k, value in response.items():
key = k.replace("-", "_")
super().__setattr__(key, value)

Check warning on line 26 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L25-L26

Added lines #L25 - L26 were not covered by tests

def __getitem__(self, key: str) -> str:
return self.__dict__[key.replace("-", "_")]

Check warning on line 29 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L29

Added line #L29 was not covered by tests

def __setitem__(self, key: str, value: str) -> None:
self.__dict__[key.replace("-", "_")] = value

Check warning on line 32 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L32

Added line #L32 was not covered by tests


@dataclass
class GetReponseResponse:
cmd_response: Optional[str] = None
sol_key_detected: Optional[bool] = None

def __init__(self, response: dict) -> None:
for k, value in response.items():
key = k.replace("-", "_")
super().__setattr__(key, value)

Check warning on line 43 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L42-L43

Added lines #L42 - L43 were not covered by tests

def __getitem__(self, key: str) -> Any:
return self.__dict__[key.replace("-", "_")]

Check warning on line 46 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L46

Added line #L46 was not covered by tests

def __setitem__(self, key: str, value: Any) -> None:
self.__dict__[key.replace("-", "_")] = value

Check warning on line 49 in src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py

View check run for this annotation

Codecov / codecov/patch

src/zos_console/zowe/zos_console_for_zowe_sdk/response/console.py#L49

Added line #L49 was not covered by tests
13 changes: 7 additions & 6 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from zowe.core_for_zowe_sdk import SdkApi
from zowe.core_for_zowe_sdk.exceptions import FileNotFound
from zowe.zos_files_for_zowe_sdk.constants import FileType, zos_file_constants
from zowe.zos_files_for_zowe_sdk.response import DatasetListResponse, MemberListResponse

_ZOWE_FILES_DEFAULT_ENCODING = zos_file_constants["ZoweFilesDefaultEncoding"]

Expand Down Expand Up @@ -306,7 +307,7 @@ def __init__(self, connection: dict):
super().__init__(connection, "/zosmf/restfiles/", logger_name=__name__)
self._default_headers["Accept-Encoding"] = "gzip"

def list(self, name_pattern: str, return_attributes: bool = False) -> List[Dict]:
def list(self, name_pattern: str, return_attributes: bool = False) -> DatasetListResponse:
"""
Retrieve a list of datasets based on a given pattern.
Expand All @@ -319,7 +320,7 @@ def list(self, name_pattern: str, return_attributes: bool = False) -> List[Dict]
Returns
-------
List[Dict]
DatasetListResponse
A JSON with a list of dataset names (and attributes if specified) matching the given pattern.
"""
custom_args = self._create_custom_request_arguments()
Expand All @@ -330,7 +331,7 @@ def list(self, name_pattern: str, return_attributes: bool = False) -> List[Dict]
custom_args["headers"]["X-IBM-Attributes"] = "base"

response_json = self.request_handler.perform_request("GET", custom_args)
return response_json
return DatasetListResponse(response_json, return_attributes)

def list_members(
self,
Expand All @@ -339,7 +340,7 @@ def list_members(
member_start: Optional[str] = None,
limit: int = 1000,
attributes: str = "member",
) -> dict:
) -> MemberListResponse:
"""
Retrieve the list of members on a given PDS/PDSE.
Expand All @@ -358,7 +359,7 @@ def list_members(
Returns
-------
dict
MemberListResponse
A JSON with a list of members from a given PDS/PDSE
"""
custom_args = self._create_custom_request_arguments()
Expand All @@ -372,7 +373,7 @@ def list_members(
custom_args["headers"]["X-IBM-Max-Items"] = "{}".format(limit)
custom_args["headers"]["X-IBM-Attributes"] = attributes
response_json = self.request_handler.perform_request("GET", custom_args)
return response_json["items"] # type: ignore
return MemberListResponse(response_json, (attributes == "base"))

def copy_data_set_or_member(
self,
Expand Down
10 changes: 7 additions & 3 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from zowe.core_for_zowe_sdk import SdkApi
from zowe.zos_files_for_zowe_sdk import constants, exceptions

from .response import FileSystemListResponse

_ZOWE_FILES_DEFAULT_ENCODING = constants.zos_file_constants["ZoweFilesDefaultEncoding"]


Expand Down Expand Up @@ -150,7 +152,9 @@ def unmount(self, file_system_name: str, options: dict = {}, encoding: str = _ZO
response_json = self.request_handler.perform_request("PUT", custom_args, expected_code=[204])
return response_json

def list(self, file_path_name: Optional[str] = None, file_system_name: Optional[str] = None) -> dict:
def list(
self, file_path_name: Optional[str] = None, file_system_name: Optional[str] = None
) -> FileSystemListResponse:
"""
List all mounted filesystems.
Expand All @@ -166,12 +170,12 @@ def list(self, file_path_name: Optional[str] = None, file_system_name: Optional[
Returns
-------
dict
FileSystemListResponse
A JSON containing the result of the operation
"""
custom_args = self._create_custom_request_arguments()

custom_args["params"] = {"path": file_path_name, "fsname": file_system_name}
custom_args["url"] = "{}mfs".format(self._request_endpoint)
response_json = self.request_handler.perform_request("GET", custom_args, expected_code=[200])
return response_json
return FileSystemListResponse(response_json)
14 changes: 14 additions & 0 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/response/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Zowe Python Client SDK.
This program and the accompanying materials are made available under the terms of the
Eclipse Public License v2.0 which accompanies this distribution, and is available at
https://www.eclipse.org/legal/epl-v20.html
SPDX-License-Identifier: EPL-2.0
Copyright Contributors to the Zowe Project.
"""
from .datasets import DatasetListResponse, MemberListResponse
from .file_system import FileSystemListResponse
from .uss import USSListResponse
Loading

0 comments on commit 7063507

Please sign in to comment.