Skip to content

Commit 3d80bb5

Browse files
committed
Added timeout
1 parent 2ec5889 commit 3d80bb5

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

test_wls_rest_python.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_wls_init():
3434
assert wls.isLatest is True
3535
assert wls.lifecycle == 'active'
3636
assert wls.version == '17.12.3.1'
37+
assert wls.timeout == wls_rest_python.DEFAULT_TIMEOUT
3738
assert wls.edit._url == 'https://edit-link'
3839
assert wls.session.verify is True
3940
assert wls.session.auth == ('weblogic', 'Welcome1')
@@ -82,30 +83,54 @@ def test_wls_init_noverify():
8283
)
8384
assert wls.session.verify == False
8485

86+
def test_wls_init_nondefault_timeout():
87+
collection = {
88+
'version': '17.12.3.1',
89+
'isLatest': True,
90+
'lifecycle': 'active',
91+
'links': [
92+
{'rel': 'edit', 'href': 'https://edit-link'},
93+
{'rel': 'domainRuntime', 'href': 'https://domainruntime-link'},
94+
],
95+
}
96+
with requests_mock.mock() as r:
97+
r.get(
98+
'https://wls.example.com:7001/management/weblogic/latest', json=collection
99+
)
100+
wls = wls_rest_python.WLS(
101+
'https://wls.example.com:7001', 'weblogic', 'Welcome1', timeout=832
102+
)
103+
assert wls.timeout == 832
85104

86105
def test_wls_get():
87106
fake_wls = MagicMock(spec=wls_rest_python.WLS)
107+
fake_wls.timeout = 372
88108
fake_wls.session = MagicMock()
89109
fake_wls.session.get = MagicMock()
90110
wls_rest_python.WLS.get(fake_wls, 'https://url', weird_requests_option='hei')
91-
fake_wls.session.get.assert_called_once_with('https://url', weird_requests_option='hei')
111+
fake_wls.session.get.assert_called_once_with('https://url', timeout=372,
112+
weird_requests_option='hei')
92113

93114

94115
def test_wls_post():
95116
fake_wls = MagicMock(spec=wls_rest_python.WLS)
117+
fake_wls.timeout = 372
96118
fake_wls.session = MagicMock()
97119
fake_wls.session.post = MagicMock()
98120
wls_rest_python.WLS.post(fake_wls, 'https://url', weird_requests_option='hei')
99121
fake_wls.session.post.assert_called_once_with('https://url', headers=None,
122+
timeout=372,
100123
weird_requests_option='hei')
101124

102125

103126
def test_wls_delete():
104127
fake_wls = MagicMock(spec=wls_rest_python.WLS)
128+
fake_wls.timeout = 372
105129
fake_wls.session = MagicMock()
106130
fake_wls.session.delete = MagicMock()
107131
wls_rest_python.WLS.delete(fake_wls, 'https://url', weird_requests_option='hei')
108132
fake_wls.session.delete.assert_called_once_with('https://url', headers=None,
133+
timeout=372,
109134
weird_requests_option='hei')
110135

111136

wls_rest_python.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import logging
77
import requests
88

9-
__version__ = '0.1.1'
9+
__version__ = '0.1.2'
1010

1111
logger = logging.getLogger(__name__)
1212

13+
# This is quite high, as the WLS server will, by default,
14+
# do operations that take "approximately 5 minutes" synchronous.
15+
DEFAULT_TIMEOUT = 305
1316

1417
class WLSException(Exception):
1518
"""Superclass for exceptions thrown by this module"""
@@ -100,7 +103,8 @@ class WLS(object):
100103
:param bool verify_ssl: Whether to verify certificates on SSL connections.
101104
"""
102105

103-
def __init__(self, host, username, password, version='latest', verify=True):
106+
def __init__(self, host, username, password, version='latest', verify=True,
107+
timeout=DEFAULT_TIMEOUT):
104108
self.session = requests.Session()
105109
self.session.verify = verify
106110
self.session.auth = (username, password)
@@ -110,6 +114,7 @@ def __init__(self, host, username, password, version='latest', verify=True):
110114
self.session.headers.update(
111115
{'Accept': 'application/json', 'User-Agent': user_agent, 'X-Requested-By': user_agent}
112116
)
117+
self.timeout = timeout
113118
self.base_url = '{}/management/weblogic/{}'.format(host, version)
114119
collection = self.get(self.base_url)
115120
self.version = collection['version']
@@ -125,7 +130,7 @@ def get(self, url, **kwargs):
125130
126131
Returns the decoded JSON.
127132
"""
128-
response = self.session.get(url, **kwargs)
133+
response = self.session.get(url, timeout=self.timeout, **kwargs)
129134
return self._handle_response(response)
130135

131136
def post(self, url, prefer_async=False, **kwargs):
@@ -136,7 +141,7 @@ def post(self, url, prefer_async=False, **kwargs):
136141
WLSObject. Otherwise it will return the decoded JSON
137142
"""
138143
headers = {'Prefer': 'respond-async'} if prefer_async else None
139-
response = self.session.post(url, headers=headers, **kwargs)
144+
response = self.session.post(url, headers=headers, timeout=self.timeout, **kwargs)
140145
return self._handle_response(response)
141146

142147
def delete(self, url, prefer_async=False, **kwargs):
@@ -147,7 +152,7 @@ def delete(self, url, prefer_async=False, **kwargs):
147152
WLSObject. Otherwise it will return the decoded JSON
148153
"""
149154
headers = {'Prefer': 'respond-async'} if prefer_async else None
150-
response = self.session.delete(url, headers=headers, **kwargs)
155+
response = self.session.delete(url, headers=headers, timeout=self.timeout, **kwargs)
151156
return self._handle_response(response)
152157

153158
def _handle_response(self, response):

0 commit comments

Comments
 (0)