Skip to content

Commit 8d41ffc

Browse files
committed
client put & put_returning_json functions
1 parent 6b6b014 commit 8d41ffc

4 files changed

Lines changed: 223 additions & 16 deletions

File tree

README.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ Note: This is currently a WIP so will be subject to breaking changes.
1010

1111
Change Log
1212
==========
13-
13+
14+
0.0.7
15+
-----
16+
17+
- client put & put_returning_json functions
18+
1419
0.0.6
1520
-----
1621

src/osdu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# license information.
55
# -----------------------------------------------------------------------------
66

7-
__VERSION__ = "0.0.6"
7+
__VERSION__ = "0.0.7"

src/osdu/client.py

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def get(self, url: str) -> requests.Response:
106106
def get_returning_json(self, url: str, ok_status_codes: list = None) -> dict:
107107
"""Get data from the specified url in json format.
108108
109+
To be able to do a conversion to json we typically need a valid http response so
110+
pass this in ok_status_codes.
111+
109112
Args:
110113
url (str): url to GET from to
111114
ok_status_codes (list, optional): Status codes for successful call. Defaults to [200].
@@ -134,8 +137,6 @@ def post(self, url: str, data: Union[str, dict]) -> requests.Response:
134137
[requests.Response]: response object
135138
"""
136139
headers = self.get_headers()
137-
# logger.debug(url)
138-
# logger.debug(data)
139140

140141
# determine whether to send to requests as data or json
141142
_json = None
@@ -144,14 +145,16 @@ def post(self, url: str, data: Union[str, dict]) -> requests.Response:
144145
data = None
145146

146147
response = requests.post(url, data=data, json=_json, headers=headers)
147-
# logger.debug(response.text)
148148
return response
149149

150150
def post_returning_json(
151151
self, url: str, data: Union[str, dict], ok_status_codes: list = None
152152
) -> dict:
153153
"""Post data to the specified url and get the result in json format.
154154
155+
To be able to do a conversion to json we typically need a valid http response so
156+
pass this in ok_status_codes.
157+
155158
Args:
156159
url (str): url to POST to
157160
data (Union[str, dict]): json data as string or dict to send as the body
@@ -170,21 +173,53 @@ def post_returning_json(
170173
raise HTTPError(response=response)
171174
return response.json()
172175

173-
def put(self, url: str, filepath: str) -> requests.Response:
174-
"""PUT from the file at the given path to a url
176+
def put(self, url: str, data: Union[str, dict]) -> requests.Response:
177+
"""PUT data to the specified url
175178
176179
Args:
177-
url (str): url to PUT to
178-
filepath (str): path to a file to PUT
180+
url (str): url to POST to
181+
data (Union[str, dict]): json data as string or dict to send as the body
179182
180183
Returns:
181-
requests.Response: response object
184+
[requests.Response]: response object
182185
"""
183186
headers = self.get_headers()
184-
headers.update({"Content-Type": "application/octet-stream", "x-ms-blob-type": "BlockBlob"})
185-
with open(filepath, "rb") as file_handle:
186-
response = requests.put(url, data=file_handle, headers=headers)
187-
return response
187+
188+
# determine whether to send to requests as data or json
189+
_json = None
190+
if isinstance(data, dict):
191+
_json = data
192+
data = None
193+
194+
response = requests.put(url, data=data, json=_json, headers=headers)
195+
# logger.debug(response.text)
196+
return response
197+
198+
def put_returning_json(
199+
self, url: str, data: Union[str, dict], ok_status_codes: list = None
200+
) -> dict:
201+
"""Post data to the specified url and get the result in json format.
202+
203+
To be able to do a conversion to json we typically need a valid http response so
204+
pass this in ok_status_codes.
205+
206+
Args:
207+
url (str): url to POST to
208+
data (Union[str, dict]): json data as string or dict to send as the body
209+
ok_status_codes (list, optional): Status codes indicating successful call. Defaults to [200].
210+
211+
Raises:
212+
HTTPError: Raised if the get returns a status other than those in ok_status_codes
213+
214+
Returns:
215+
dict: response json
216+
"""
217+
if ok_status_codes is None:
218+
ok_status_codes = [200]
219+
response = self.put(url, data)
220+
if response.status_code not in ok_status_codes:
221+
raise HTTPError(response=response)
222+
return response.json()
188223

189224
def delete(self, url: str, ok_status_codes: list = None) -> requests.Response:
190225
"""GET to a url

tests/test_client.py

Lines changed: 169 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from unittest.case import TestCase
1111

1212
import mock
13+
import requests
1314
from mock import patch
1415
from nose2.tools import params
1516
from requests.models import HTTPError
@@ -31,6 +32,11 @@ def create_dummy_client(server_url="http://www.test.com"):
3132
class TestOsduClient(TestCase):
3233
"""Test cases for base OSDU client"""
3334

35+
dummy_headers = {"headers": "value"}
36+
sample_json = {
37+
"name": "value",
38+
}
39+
3440
def test_init(self):
3541
"""Test the init method"""
3642
server_url = "http://www.test.com"
@@ -65,6 +71,26 @@ def test_get_headers(self, mock_get_token): # pylint: disable=W0613
6571

6672
self.assertDictEqual(expected_headers, headers)
6773

74+
# region test get
75+
@params(
76+
"http://www.test.com/",
77+
"http://www.test.com/test2/",
78+
)
79+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
80+
def test_get(self, url, _):
81+
"""Test valid get returns expected values"""
82+
response_mock = mock.Mock()
83+
with mock.patch.object(requests, "get", return_value=response_mock) as mock_get:
84+
client = create_dummy_client()
85+
86+
response = client.get(url)
87+
88+
mock_get.assert_called_once()
89+
mock_get.assert_called_with(url, headers=self.dummy_headers)
90+
self.assertEqual(response_mock, response)
91+
92+
# endregion test get
93+
6894
# region test get_returning_json
6995
@params((None, 200), ([200], 200), ([200, 202], 202), ([202], 202))
7096
def test_get_returning_json(self, expected_status_codes, actual_status_code):
@@ -104,9 +130,56 @@ def test_get_returning_json_http_error_throws_exception(
104130

105131
# endregion test get_returning_json
106132

133+
# region test post
134+
@params(
135+
"string1",
136+
"string2",
137+
)
138+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
139+
def test_post_string(self, string_data, _):
140+
"""Test valid post with string returns expected values"""
141+
response_mock = mock.Mock()
142+
with mock.patch.object(requests, "post", return_value=response_mock) as mock_post:
143+
client = create_dummy_client()
144+
145+
response = client.post("http://www.test.com/", string_data)
146+
147+
mock_post.assert_called_once()
148+
mock_post.assert_called_with(
149+
"http://www.test.com/", data=string_data, json=None, headers=self.dummy_headers
150+
)
151+
self.assertEqual(response_mock, response)
152+
153+
@params(
154+
{"name": "value"},
155+
{"name2": "value2"},
156+
)
157+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
158+
def test_post_json(self, json, _):
159+
"""Test valid post with json returns expected values"""
160+
response_mock = mock.Mock()
161+
with mock.patch.object(requests, "post", return_value=response_mock) as mock_post:
162+
client = create_dummy_client()
163+
164+
response = client.post("http://www.test.com/", json)
165+
166+
mock_post.assert_called_once()
167+
mock_post.assert_called_with(
168+
"http://www.test.com/", data=None, json=json, headers=self.dummy_headers
169+
)
170+
self.assertEqual(response_mock, response)
171+
172+
# endregion test post
173+
107174
# region test post_returning_json
108-
@params((None, 200), ([200], 200), ([200, 202], 202), ([202], 202))
109-
def test_post_returning_json(self, expected_status_codes, actual_status_code):
175+
176+
@params(
177+
(None, 200),
178+
([200], 200),
179+
([200, 202], 202),
180+
([202], 202),
181+
)
182+
def test_post_returning_json_status_codes(self, expected_status_codes, actual_status_code):
110183
"""Test valid post returns expected values"""
111184
input_data = {
112185
"name": "value",
@@ -151,6 +224,100 @@ def test_post_returning_json_http_error_throws_exception(
151224

152225
# endregion test post_returning_json
153226

227+
# region test put
228+
@params(
229+
"string1",
230+
"string2",
231+
)
232+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
233+
def test_put_string(self, string_data, _):
234+
"""Test valid put with string returns expected values"""
235+
response_mock = mock.Mock()
236+
with mock.patch.object(requests, "put", return_value=response_mock) as mock_put:
237+
client = create_dummy_client()
238+
239+
response = client.put("http://www.test.com/", string_data)
240+
241+
mock_put.assert_called_once()
242+
mock_put.assert_called_with(
243+
"http://www.test.com/", data=string_data, json=None, headers=self.dummy_headers
244+
)
245+
self.assertEqual(response_mock, response)
246+
247+
@params(
248+
{"name": "value"},
249+
{"name2": "value2"},
250+
)
251+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
252+
def test_put_json(self, json, _):
253+
"""Test valid put with json returns expected values"""
254+
response_mock = mock.Mock()
255+
with mock.patch.object(requests, "put", return_value=response_mock) as mock_put:
256+
client = create_dummy_client()
257+
258+
response = client.put("http://www.test.com/", json)
259+
260+
mock_put.assert_called_once()
261+
mock_put.assert_called_with(
262+
"http://www.test.com/", data=None, json=json, headers=self.dummy_headers
263+
)
264+
self.assertEqual(response_mock, response)
265+
266+
# endregion test put
267+
268+
# region test put_returning_json
269+
270+
@params(
271+
(None, 200),
272+
([200], 200),
273+
([200, 202], 202),
274+
([202], 202),
275+
)
276+
def test_put_returning_json_status_codes(self, expected_status_codes, actual_status_code):
277+
"""Test valid put returns expected values"""
278+
input_data = {
279+
"name": "value",
280+
}
281+
expected_response_data = input_data
282+
ok_response_mock = mock.Mock()
283+
type(ok_response_mock).status_code = mock.PropertyMock(return_value=actual_status_code)
284+
ok_response_mock.json.return_value = expected_response_data
285+
with mock.patch("osdu.client.OsduClient.put", return_value=ok_response_mock) as mock_get:
286+
client = create_dummy_client()
287+
288+
if expected_status_codes:
289+
response = client.put_returning_json(
290+
"http://www.test.com/", input_data, expected_status_codes
291+
)
292+
else:
293+
response = client.put_returning_json("http://www.test.com/", input_data)
294+
295+
mock_get.assert_called_once()
296+
mock_get.assert_called_with("http://www.test.com/", input_data)
297+
self.assertDictEqual(expected_response_data, response)
298+
299+
@params((None, 404), (None, 201), ([200], 404), ([200, 202], 500))
300+
def test_put_returning_json_http_error_throws_exception(
301+
self, expected_status_codes, actual_status_code
302+
):
303+
"""Test put error returns expected values"""
304+
input_data = {
305+
"name": "value",
306+
}
307+
error_response_mock = mock.MagicMock()
308+
type(error_response_mock).status_code = mock.PropertyMock(return_value=actual_status_code)
309+
with mock.patch("osdu.client.OsduClient.put", return_value=error_response_mock):
310+
with self.assertRaises(HTTPError):
311+
client = create_dummy_client()
312+
if expected_status_codes:
313+
_ = client.put_returning_json(
314+
"http://www.test.com/", input_data, expected_status_codes
315+
)
316+
else:
317+
_ = client.put_returning_json("http://www.test.com/", input_data)
318+
319+
# endregion test put_returning_json
320+
154321
# region test delete
155322
@params((None, 200), ([200], 200), ([200, 202], 202), ([202], 202))
156323
@patch.object(OsduClient, "get_headers", return_value=(None))

0 commit comments

Comments
 (0)