Skip to content

Commit

Permalink
Merge pull request #2 from zowe/SampleSDK
Browse files Browse the repository at this point in the history
Sample SDK
  • Loading branch information
zFernand0 authored Jul 8, 2024
2 parents 76755f3 + 541dac9 commit bc3ed2f
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ This repository contains sample code demonstrating Zowe client tools.

## Samples

- [Zowe Python SDK Sample](./zowe-python-sdk-sample)
- [Zowe Python SDK Sample](./python-sdk-sample)

31 changes: 31 additions & 0 deletions python-sdk-sample/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
# Zowe Python SDK Sample

## Requirements

The sample SDK has the following requirements:
```
black
isort
zowe.core-for-zowe-sdk~=1.0.0.dev
pytest==7.1.2
```
You can install the requirements in a virtual environment with following commands:

```shell
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```

You can clone the repository using `git`:

```
git clone https://github.com/zowe/zowe-client-samples.git
```

## Quickstart

After you install the package, import the class SampleSDK from modules.

Below is a simple script of how to run the sample SDK, assuming the script file is located under the `python-sdk-sample` folder:

```python
from org.sample_for_zowe_sdk import SampleSdk

t = SampleSdk()
result = t.create_post({"title": "foo", "body": "bar", "userId": 10})
print(result)
```
7 changes: 7 additions & 0 deletions python-sdk-sample/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
[project]
name = "org_sample-for-zowe-sdk"
version = "0.0.1"

[tool.black]
line-length = 120

[tool.isort]
profile = "black"

[tool.setuptools.package-dir]
"org.sample_for_zowe_sdk" = "src"
4 changes: 3 additions & 1 deletion python-sdk-sample/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
black
isort
zowe.core-for-zowe-sdk~=1.0.0.dev
zowe.core-for-zowe-sdk>=1.0.0-dev16
pytest==7.1.2
-e .
1 change: 0 additions & 1 deletion python-sdk-sample/setup.py

This file was deleted.

1 change: 1 addition & 0 deletions python-sdk-sample/src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .main import SampleSdk
120 changes: 119 additions & 1 deletion python-sdk-sample/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,122 @@
Copyright Contributors to the Zowe Project.
"""

print("Hello world!")
from zowe.core_for_zowe_sdk import SdkApi


class SampleSdk(SdkApi):
"""
Class used to represent the sample API.
...
Attributes
----------
connection
Connection object
"""

def __init__(self):
"""
Construct a sample SDK object.
Parameters
----------
connection
The connection object
"""
super().__init__({"host": "jsonplaceholder.typicode.com", "user": "fake", "password": "fake"}, "/", logger_name=__name__)

def create_post(self, post):
"""
Create a post with provided contents
Parameters
----------
post: JSON
contents of the post to be created
Returns
-------
json
A JSON of the created post
"""
custom_args = self._create_custom_request_arguments()
custom_args["json"] = post
custom_args["url"] = f"{self._request_endpoint}/posts"
response_json = self.request_handler.perform_request("POST", custom_args, expected_code=[201])
return response_json

def update_post(self, id, post):
"""
Overwrite a given post with provided contents
Parameters
----------
id: int
id of the post to be updated
post: JSON
contents of the post to be uploaded
Returns
-------
json
A JSON of the updated post
"""
custom_args = self._create_custom_request_arguments()
custom_args["json"] = post
custom_args["url"] = f"{self._request_endpoint}/posts/{id}"
response_json = self.request_handler.perform_request("PUT", custom_args, expected_code=[200])
return response_json

def get_post(self, id):
"""
Retrieve the contents of a given post.
Parameters
----------
id: int
id of the post to be retrieved
Returns
-------
json
A JSON with the contents of the post
"""
custom_args = self._create_custom_request_arguments()
custom_args["url"] = f"{self._request_endpoint}/posts/{id}"
response_json = self.request_handler.perform_request("GET", custom_args, expected_code=[200])
return response_json

def list_post(self):
"""
Retrieve the list of all posts.
Returns
-------
list
A list object of all posts (JSON objects).
"""
custom_args = self._create_custom_request_arguments()
custom_args["url"] = f"{self._request_endpoint}/posts"
response_json = self.request_handler.perform_request("GET", custom_args, expected_code=[200])
self.logger.info("Listed all posts")
return response_json

def delete_post(self, id):
"""
Deletes a post.
Parameters
----------
id: int
id of the post to be deleted
Returns
-------
json - A empty JSON
"""
custom_args = self._create_custom_request_arguments()
custom_args["url"] = f"{self._request_endpoint}/posts/{id}"
response_json = self.request_handler.perform_request("DELETE", custom_args, expected_code=[200])
return response_json
45 changes: 45 additions & 0 deletions python-sdk-sample/tests/integration/test_integration_sample_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from unittest import TestCase

import sys
import os

current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, "../.."))
from src import SampleSdk

class TestSampleSDK(TestCase):

def setUp(self):
self.expected = {"title": "foo", "body": "bar", "userId": 10}

def is_subset(self, dict1, dict2):
for key, value in dict1.items():
if key not in dict2 or dict2[key] != value:
return False
return True

def test_create_post(self):
sdk = SampleSdk()
result = sdk.create_post(self.expected)
self.assertTrue(self.is_subset(self.expected, result))

def test_get_post(self):
sdk = SampleSdk()
result = sdk.get_post(1)
self.assertEqual(result.get("id"), 1)

def test_update_post(self):
sdk = SampleSdk()
result = sdk.update_post(1, self.expected)
self.assertTrue(self.is_subset(self.expected, result))

def test_delete_post(self):
sdk = SampleSdk()
result = sdk.delete_post(1)
self.assertEqual(result, {})

def test_list_post(self):
sdk = SampleSdk()
result = sdk.list_post()
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], dict)
45 changes: 45 additions & 0 deletions python-sdk-sample/tests/unit/test_sample_sdk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from unittest import TestCase, mock

import sys
import os

current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(current_dir, "../.."))
from src import SampleSdk

class TestSampleSDK(TestCase):

@mock.patch("requests.Session.send")
def test_create_post(self, mock_send_request):
mock_send_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=201)
post = SampleSdk()
result = post.create_post({"title": "foo", "body": "bar", "userId": 10})
mock_send_request.assert_called()

@mock.patch("requests.Session.send")
def test_update_post(self, mock_send_request):
mock_send_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
post = SampleSdk()
result = post.update_post(1, {"title": "foo", "body": "bar", "userId": 10})
mock_send_request.assert_called()

@mock.patch("requests.Session.send")
def test_get_post(self, mock_send_request):
mock_send_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
post = SampleSdk()
result = post.get_post(1)
mock_send_request.assert_called()

@mock.patch("requests.Session.send")
def test_list_post(self, mock_send_request):
mock_send_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
post = SampleSdk()
result = post.list_post()
mock_send_request.assert_called()

@mock.patch("requests.Session.send")
def test_delete_post(self, mock_send_request):
mock_send_request.return_value = mock.Mock(headers={"Content-Type": "application/json"}, status_code=200)
post = SampleSdk()
result = post.delete_post(1)
mock_send_request.assert_called()

0 comments on commit bc3ed2f

Please sign in to comment.