Skip to content

Commit 6637d84

Browse files
authored
Merge pull request #3 from equinor/bug/client-fixes
Client improvements
2 parents 3879999 + 923c1ed commit 6637d84

4 files changed

Lines changed: 20 additions & 43 deletions

File tree

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Note: This is currently a WIP so will be subject to breaking changes.
1111
Change Log
1212
==========
1313

14+
0.0.10
15+
------
16+
17+
- Delete doesn't check status codes (consistency with other functions)
18+
- Put will pass list objects as json
19+
1420
0.0.9
1521
-----
1622

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.9"
7+
__VERSION__ = "0.0.10"

src/osdu/client.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ def put(self, url: str, data: Union[str, dict]) -> requests.Response:
187187

188188
# determine whether to send to requests as data or json
189189
_json = None
190-
if isinstance(data, dict):
190+
if isinstance(data, (dict, list)):
191191
_json = data
192192
data = None
193193

194194
response = requests.put(url, data=data, json=_json, headers=headers)
195-
# logger.debug(response.text)
196195
return response
197196

198197
def put_returning_json(
@@ -221,25 +220,17 @@ def put_returning_json(
221220
raise HTTPError(response=response)
222221
return response.json()
223222

224-
def delete(self, url: str, ok_status_codes: list = None) -> requests.Response:
223+
def delete(self, url: str) -> requests.Response:
225224
"""GET to a url
226225
227226
Args:
228227
url (str): url to PUT to
229-
ok_status_codes (list, optional): Status codes indicating successful call. Defaults to [200].
230228
231229
Returns:
232230
requests.Response: response object
233231
"""
234-
if ok_status_codes is None:
235-
ok_status_codes = [200]
236-
237232
headers = self.get_headers()
238233
response = requests.delete(url, headers=headers)
239-
240-
if response.status_code not in ok_status_codes:
241-
raise HTTPError(response=response)
242-
243234
return response
244235

245236
# endregion HTTP Actions

tests/test_client.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -319,41 +319,21 @@ def test_put_returning_json_http_error_throws_exception(
319319
# endregion test put_returning_json
320320

321321
# region test delete
322-
@params((None, 200), ([200], 200), ([200, 202], 202), ([202], 202))
323-
@patch.object(OsduClient, "get_headers", return_value=(None))
324-
def test_delete(self, expected_status_codes, actual_status_code, _):
322+
@params(
323+
"http://www.test.com/",
324+
"http://www.test.com/test2/",
325+
)
326+
@patch.object(OsduClient, "get_headers", return_value=dummy_headers)
327+
def test_delete(self, url, _):
325328
"""Test valid delete returns expected values"""
326-
expected_response_data = {
327-
"name": "value",
328-
}
329-
ok_response_mock = mock.Mock()
330-
type(ok_response_mock).status_code = mock.PropertyMock(return_value=actual_status_code)
331-
ok_response_mock.json.return_value = expected_response_data
332-
with mock.patch("requests.delete", return_value=ok_response_mock) as mock_delete:
329+
response_mock = mock.Mock()
330+
with mock.patch("requests.delete", return_value=response_mock) as mock_delete:
333331
client = create_dummy_client()
334-
335-
if expected_status_codes:
336-
response = client.delete("http://www.test.com/", expected_status_codes)
337-
else:
338-
response = client.delete("http://www.test.com/")
332+
response = client.delete(url)
339333

340334
mock_delete.assert_called_once()
341-
mock_delete.assert_called_with("http://www.test.com/", headers=None)
342-
self.assertEqual(ok_response_mock, response)
343-
344-
@params((None, 404), (None, 201), ([200], 404), ([200, 202], 500))
345-
@patch.object(OsduClient, "get_headers", return_value=(None))
346-
def test_delete_http_error_throws_exception(self, expected_status_codes, actual_status_code, _):
347-
"""Test delete http error returns expected values"""
348-
error_response_mock = mock.MagicMock()
349-
type(error_response_mock).status_code = mock.PropertyMock(return_value=actual_status_code)
350-
with mock.patch("requests.delete", return_value=error_response_mock):
351-
with self.assertRaises(HTTPError):
352-
client = create_dummy_client()
353-
if expected_status_codes:
354-
_ = client.delete("http://www.test.com/", expected_status_codes)
355-
else:
356-
_ = client.delete("http://www.test.com/")
335+
mock_delete.assert_called_with(url, headers=self.dummy_headers)
336+
self.assertEqual(response_mock, response)
357337

358338
# endregion test delete
359339

0 commit comments

Comments
 (0)