Skip to content

Commit 4360287

Browse files
author
Vladimir Kotal
committed
raise exception from do_api_call()
fixes #3356
1 parent 4cf065d commit 4360287

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

opengrok-tools/src/main/python/opengrok_tools/utils/restful.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ def do_api_call(verb, uri, params=None, headers=None, data=None):
3838
if handler is None or not callable(handler):
3939
raise Exception('Unknown HTTP verb: {}'.format(verb))
4040

41-
return handler(
41+
logger = logging.getLogger(__name__)
42+
43+
logger.debug("{} API call: {} with data '{}' and headers: {}".
44+
format(verb, uri, data, headers))
45+
r = handler(
4246
uri,
4347
data=data,
4448
params=params,
4549
headers=headers,
4650
proxies=get_proxies(uri)
4751
)
4852

53+
if r is not None:
54+
logger.error("API call failed: {}".format(r))
55+
r.raise_for_status()
56+
57+
return r
58+
4959

5060
def call_rest_api(command, pattern, name):
5161
"""
@@ -100,10 +110,4 @@ def call_rest_api(command, pattern, name):
100110
data = data.replace(pattern, name)
101111
logger.debug("entity data: {}".format(data))
102112

103-
logger.debug("{} API call: {} with data '{}' and headers: {}".
104-
format(verb, uri, data, headers))
105-
r = do_api_call(verb, uri, headers=headers, data=data)
106-
if r is not None:
107-
logger.debug("API call result: {}".format(r))
108-
r.raise_for_status()
109-
return r
113+
return do_api_call(verb, uri, headers=headers, data=data)

opengrok-tools/src/test/python/test_command_sequence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ def __init__(self):
168168
self.status_code = 500
169169
self.raise_for_status = self.p
170170

171-
def mock_response(verb, uri, headers, data):
171+
def mock_response(uri, headers, data, params, proxies):
172172
return MockResponse()
173173

174174
commands = CommandSequence(
175175
CommandSequenceBase("test-cleanup-list",
176176
[{'command': ['http://foo', 'PUT', 'data']}]))
177177
assert commands is not None
178178
with monkeypatch.context() as m:
179-
m.setattr("opengrok_tools.utils.restful.do_api_call",
179+
m.setattr("requests.put",
180180
mock_response)
181181
commands.run()
182182
assert commands.check([]) == 1

opengrok-tools/src/test/python/test_restful.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#
2121

2222
#
23-
# Copyright (c) 2019-2020, Oracle and/or its affiliates. All rights reserved.
23+
# Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2424
#
2525

2626
import pytest
@@ -107,12 +107,11 @@ def __init__(self):
107107
self.status_code = 400
108108
self.raise_for_status = self.p
109109

110-
def mock_response(uri, verb, headers, data):
110+
def mock_response(uri, headers, data, params, proxies):
111111
return MockResponse()
112112

113113
with monkeypatch.context() as m:
114-
m.setattr("opengrok_tools.utils.restful.do_api_call",
115-
mock_response)
114+
m.setattr("requests.put", mock_response)
116115
with pytest.raises(HTTPError):
117116
call_rest_api({'command': ['http://foo', 'PUT', 'data']},
118117
None, None)

0 commit comments

Comments
 (0)