Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 96125c0

Browse files
Merge pull request #66 from cbsinteractive/support-timeout
Support timeout configuration
2 parents e213f48 + afa206a commit 96125c0

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
ignore = E501,E306,W503,W504

elemental/client.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ class InvalidResponse(ElementalException):
3232

3333

3434
class ElementalLive():
35-
def __init__(self, server_ip, user=None, api_key=None):
35+
def __init__(self, server_ip, user=None, api_key=None, timeout=5):
3636
self.server_ip = server_ip
3737
self.user = user
3838
self.api_key = api_key
39+
self.timeout = timeout
3940
self.session = requests.Session()
4041

4142
def generate_headers(self, url=""):
@@ -62,11 +63,12 @@ def generate_headers(self, url=""):
6263
'Content-Type': 'application/xml'
6364
}
6465

65-
def send_request(self, http_method, url, headers, body=""):
66+
def send_request(self, http_method, url, headers, body="", timeout=None):
6667
# Send request according to different methods
6768
try:
69+
timeout = timeout or self.timeout
6870
response = self.session.request(
69-
method=http_method, url=url, data=body, headers=headers)
71+
method=http_method, url=url, data=body, headers=headers, timeout=timeout)
7072

7173
except requests.exceptions.RequestException as e:
7274
raise InvalidRequest(f"{http_method}: {url} failed\n{e}")
@@ -76,7 +78,7 @@ def send_request(self, http_method, url, headers, body=""):
7678
f"{response.status_code}\n{response.text}")
7779
return response
7880

79-
def create_event(self, options):
81+
def create_event(self, options, timeout=None):
8082

8183
# Initiate url
8284
url = f'{self.server_ip}/live_events'
@@ -93,7 +95,7 @@ def create_event(self, options):
9395

9496
# Send request and do exception handling
9597
response = self.send_request(
96-
http_method="POST", url=url, headers=headers, body=body)
98+
http_method="POST", url=url, headers=headers, body=body, timeout=timeout)
9799

98100
# Find newly created event id
99101
xml_root = ET.fromstring(response.content)
@@ -102,7 +104,7 @@ def create_event(self, options):
102104

103105
return {'id': event_id}
104106

105-
def delete_event(self, event_id):
107+
def delete_event(self, event_id, timeout=None):
106108

107109
# Initial url
108110
url = f'{self.server_ip}/live_events/{event_id}'
@@ -111,9 +113,9 @@ def delete_event(self, event_id):
111113
headers = self.generate_headers(url)
112114

113115
# Send request and do exception handling
114-
self.send_request(http_method="DELETE", url=url, headers=headers)
116+
self.send_request(http_method="DELETE", url=url, headers=headers, timeout=timeout)
115117

116-
def start_event(self, event_id):
118+
def start_event(self, event_id, timeout=None):
117119
# Initail url
118120
url = f'{self.server_ip}/live_events/{event_id}/start'
119121

@@ -125,9 +127,9 @@ def start_event(self, event_id):
125127

126128
# Send request and do exception handling
127129
self.send_request(http_method="POST", url=url,
128-
headers=headers, body=body)
130+
headers=headers, body=body, timeout=timeout)
129131

130-
def stop_event(self, event_id):
132+
def stop_event(self, event_id, timeout=None):
131133
# Initail url
132134
url = f'{self.server_ip}/live_events/{event_id}/stop'
133135

@@ -139,15 +141,15 @@ def stop_event(self, event_id):
139141

140142
# Send request and do exception handling
141143
self.send_request(http_method="POST", url=url,
142-
headers=headers, body=body)
144+
headers=headers, body=body, timeout=timeout)
143145

144-
def describe_event(self, event_id):
146+
def describe_event(self, event_id, timeout=None):
145147
url = f'{self.server_ip}/live_events/{event_id}'
146148

147149
headers = self.generate_headers(url)
148150

149151
response = self.send_request(http_method="GET", url=url,
150-
headers=headers)
152+
headers=headers, timeout=timeout)
151153
# print(response.text)
152154
event_info = {}
153155

@@ -160,11 +162,11 @@ def describe_event(self, event_id):
160162

161163
return event_info
162164

163-
def find_devices_in_use(self):
165+
def find_devices_in_use(self, timeout=None):
164166
events_url = f'{self.server_ip}/live_events?filter=active'
165167
events_headers = self.generate_headers(events_url)
166168
events = self.send_request(
167-
http_method="GET", url=events_url, headers=events_headers)
169+
http_method="GET", url=events_url, headers=events_headers, timeout=timeout)
168170
events_list = ET.fromstring(events.text)
169171

170172
# Find in use devices from active events
@@ -174,11 +176,11 @@ def find_devices_in_use(self):
174176

175177
return in_use_devices
176178

177-
def get_input_devices(self):
179+
def get_input_devices(self, timeout=None):
178180
devices_url = f'{self.server_ip}/devices'
179181
devices_headers = self.generate_headers(devices_url)
180182
devices = self.send_request(
181-
http_method="GET", url=devices_url, headers=devices_headers)
183+
http_method="GET", url=devices_url, headers=devices_headers, timeout=timeout)
182184
devices_info = xmltodict.parse(devices.text)[
183185
'device_list']['device']
184186

@@ -193,19 +195,19 @@ def get_input_devices(self):
193195
devices_info, key=lambda d: int(d["id"]))
194196
return [dict(d) for d in devices_info]
195197

196-
def get_input_device_by_id(self, input_device_id):
198+
def get_input_device_by_id(self, input_device_id, timeout=None):
197199
devices_url = f'{self.server_ip}/devices/{input_device_id}'
198200
devices_headers = self.generate_headers(devices_url)
199201
devices = self.send_request(
200-
http_method="GET", url=devices_url, headers=devices_headers)
202+
http_method="GET", url=devices_url, headers=devices_headers, timeout=timeout)
201203
device_info = xmltodict.parse(devices.text)['device']
202204
devices_in_use = self.find_devices_in_use()
203205
device_info['availability'] = (device_info['device_name']
204206
not in devices_in_use)
205207
device_info.pop('@href')
206208
return dict(device_info)
207209

208-
def generate_preview(self, input_id):
210+
def generate_preview(self, input_id, timeout=None):
209211
url = f'{self.server_ip}/inputs/generate_preview'
210212
headers = self.generate_headers(url)
211213

@@ -220,7 +222,7 @@ def generate_preview(self, input_id):
220222
f"[input_format]=Auto&live_event[inputs_attributes][0]" \
221223
f"[device_input_attributes][device_id]={input_id}"
222224
response = self.send_request(
223-
http_method="POST", url=url, headers=headers, body=data)
225+
http_method="POST", url=url, headers=headers, body=data, timeout=timeout)
224226

225227
response_parse = ast.literal_eval(response.text)
226228

@@ -232,7 +234,7 @@ def generate_preview(self, input_id):
232234
f'p_{response_parse["preview_image_id"]}_job_0.jpg'
233235
return {'preview_url': preview_url}
234236

235-
def event_can_delete(self, channel_id):
237+
def event_can_delete(self, channel_id, timeout=None):
236238
channel_info = self.describe_event(channel_id)
237239
if channel_info['status'] in ('pending', 'running',
238240
'preprocessing', 'postprocessing'):

elemental/client_test.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ELEMENTAL_ADDRESS = "FAKE_ADDRESS.com"
1414
HEADERS = {'Accept': 'application/xml', 'Content-Type': 'application/xml'}
1515
REQUEST_BODY = "<live_event>FAKE</live_event>"
16+
TIMEOUT = 10
1617

1718

1819
def file_fixture(file_name):
@@ -66,13 +67,14 @@ def test_send_request_should_call_request_as_expected():
6667
client.session.request = mock.MagicMock(
6768
return_value=mock_response(status=200))
6869
client.send_request(
69-
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY)
70+
'POST', f'{ELEMENTAL_ADDRESS}/live_events', HEADERS, REQUEST_BODY, timeout=TIMEOUT)
7071

7172
request_to_elemental = client.session.request.call_args_list[0][1]
7273
assert request_to_elemental['url'] == f'{ELEMENTAL_ADDRESS}/live_events'
7374
assert request_to_elemental['method'] == 'POST'
7475
assert request_to_elemental['headers']['Accept'] == 'application/xml'
7576
assert request_to_elemental['headers']['Content-Type'] == 'application/xml'
77+
assert request_to_elemental['timeout'] == TIMEOUT
7678

7779

7880
def test_send_request_should_return_response_on_correct_status_code():
@@ -167,7 +169,7 @@ def test_delete_event_should_call_send_request_as_expect():
167169
client.delete_event(event_id)
168170
client.send_request.assert_called_once_with(
169171
http_method='DELETE',
170-
url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}', headers=HEADERS)
172+
url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}', headers=HEADERS, timeout=None)
171173

172174

173175
def test_start_event_should_call_send_request_as_expect():
@@ -185,7 +187,7 @@ def test_start_event_should_call_send_request_as_expect():
185187
client.send_request.assert_called_once_with(
186188
http_method='POST',
187189
url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/start',
188-
headers=HEADERS, body="<start></start>")
190+
headers=HEADERS, body="<start></start>", timeout=None)
189191

190192

191193
def test_stop_event_should_call_send_request_as_expect():
@@ -202,7 +204,7 @@ def test_stop_event_should_call_send_request_as_expect():
202204
client.send_request.assert_called_once_with(
203205
http_method='POST',
204206
url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}/stop',
205-
headers=HEADERS, body="<stop></stop>")
207+
headers=HEADERS, body="<stop></stop>", timeout=None)
206208

207209

208210
def send_request_side_effect(**kwargs):
@@ -230,7 +232,7 @@ def test_find_devices_in_use_will_call_send_request_as_expect():
230232
client.send_request.assert_called_with(http_method="GET",
231233
url=f'{ELEMENTAL_ADDRESS}'
232234
f'/live_events?'
233-
f'filter=active', headers=HEADERS)
235+
f'filter=active', headers=HEADERS, timeout=None)
234236

235237

236238
def test_find_devices_in_use_will_return_in_used_devices():
@@ -265,7 +267,7 @@ def test_get_input_devices_will_call_send_request_as_expect():
265267

266268
client.send_request.\
267269
assert_called_with(http_method="GET",
268-
url=f'{ELEMENTAL_ADDRESS}/devices', headers=HEADERS)
270+
url=f'{ELEMENTAL_ADDRESS}/devices', headers=HEADERS, timeout=None)
269271

270272

271273
def test_get_input_devices_will_get_right_devices_info():
@@ -314,7 +316,7 @@ def test_get_input_device_by_id_will_call_send_request_as_expect():
314316
client.send_request.\
315317
assert_called_with(http_method="GET",
316318
url=f'{ELEMENTAL_ADDRESS}/devices/2',
317-
headers=HEADERS)
319+
headers=HEADERS, timeout=None)
318320

319321

320322
def test_get_input_device_by_id_will_get_right_devices_info():
@@ -396,7 +398,7 @@ def test_describe_event_will_call_send_request_as_expect():
396398
client.send_request.assert_called_once_with(
397399
http_method='GET',
398400
url=f'{ELEMENTAL_ADDRESS}/live_events/{event_id}',
399-
headers=HEADERS)
401+
headers=HEADERS, timeout=None)
400402

401403

402404
def test_describe_event_will_return_event_info_as_expect():

0 commit comments

Comments
 (0)