Skip to content

Commit 9d16f41

Browse files
committed
No failures fix
1 parent c4064d5 commit 9d16f41

File tree

7 files changed

+254
-114
lines changed

7 files changed

+254
-114
lines changed

reportportal_client/client.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ def finish_launch(self,
127127
description=kwargs.get('description')
128128
).payload
129129
response = HttpRequest(self.session.put, url=url, json=request_payload,
130-
verify_ssl=self.verify_ssl).make()
130+
verify_ssl=self.verify_ssl,
131+
name='Finish Launch').make()
132+
if not response:
133+
return
131134
logger.debug('finish_launch - ID: %s', self.launch_id)
132135
logger.debug('response message: %s', response.message)
133136
return response.message
@@ -172,7 +175,10 @@ def finish_test_item(self,
172175
).payload
173176
response = HttpRequest(self.session.put, url=url, json=request_payload,
174177
verify_ssl=self.verify_ssl).make()
175-
self._item_stack.pop()
178+
if not response:
179+
return
180+
# noinspection PyUnresolvedReferences
181+
self._item_stack.pop() if len(self._item_stack) > 0 else None
176182
logger.debug('finish_test_item - ID: %s', item_id)
177183
logger.debug('response message: %s', response.message)
178184
return response.message
@@ -186,7 +192,7 @@ def get_item_id_by_uuid(self, uuid):
186192
url = uri_join(self.base_url_v1, 'item', 'uuid', uuid)
187193
response = HttpRequest(self.session.get, url=url,
188194
verify_ssl=self.verify_ssl).make()
189-
return response.id
195+
return response.id if response else None
190196

191197
def get_launch_info(self):
192198
"""Get the current launch information.
@@ -199,6 +205,8 @@ def get_launch_info(self):
199205
logger.debug('get_launch_info - ID: %s', self.launch_id)
200206
response = HttpRequest(self.session.get, url=url,
201207
verify_ssl=self.verify_ssl).make()
208+
if not response:
209+
return
202210
if response.is_success:
203211
launch_info = response.json
204212
logger.debug(
@@ -214,14 +222,17 @@ def get_launch_ui_id(self):
214222
215223
:return: UI ID of the given launch. None if UI ID has not been found.
216224
"""
217-
return self.get_launch_info().get('id')
225+
launch_info = self.get_launch_info()
226+
return launch_info.get('id') if launch_info else None
218227

219228
def get_launch_ui_url(self):
220229
"""Get UI URL of the current launch.
221230
222231
:return: launch URL or all launches URL.
223232
"""
224-
ui_id = self.get_launch_ui_id() or ''
233+
ui_id = self.get_launch_ui_id()
234+
if not ui_id:
235+
return
225236
path = 'ui/#{0}/launches/all/{1}'.format(self.project, ui_id)
226237
url = uri_join(self.endpoint, path)
227238
logger.debug('get_launch_ui_url - ID: %s', self.launch_id)
@@ -235,7 +246,7 @@ def get_project_settings(self):
235246
url = uri_join(self.base_url_v1, 'settings')
236247
response = HttpRequest(self.session.get, url=url, json={},
237248
verify_ssl=self.verify_ssl).make()
238-
return response.json
249+
return response.json if response else None
239250

240251
def log(self, time, message, level=None, attachment=None, item_id=None):
241252
"""Send log message to the Report Portal.
@@ -287,6 +298,8 @@ def start_launch(self,
287298
url=url,
288299
json=request_payload,
289300
verify_ssl=self.verify_ssl).make()
301+
if not response:
302+
return
290303
self._log_manager.launch_id = self.launch_id = response.id
291304
logger.debug('start_launch - ID: %s', self.launch_id)
292305
return self.launch_id
@@ -346,9 +359,12 @@ def start_test_item(self,
346359
url=url,
347360
json=request_payload,
348361
verify_ssl=self.verify_ssl).make()
362+
if not response:
363+
return
349364
item_id = response.id
350365
if item_id is not NOT_FOUND:
351366
logger.debug('start_test_item - ID: %s', item_id)
367+
# noinspection PyUnresolvedReferences
352368
self._item_stack.append(item_id)
353369
else:
354370
logger.warning('start_test_item - invalid response: %s',
@@ -375,9 +391,12 @@ def update_test_item(self, item_uuid, attributes=None, description=None):
375391
url = uri_join(self.base_url_v1, 'item', item_id, 'update')
376392
response = HttpRequest(self.session.put, url=url, json=data,
377393
verify_ssl=self.verify_ssl).make()
394+
if not response:
395+
return
378396
logger.debug('update_test_item - Item: %s', item_id)
379397
return response.message
380398

381399
def current_item(self):
382400
"""Retrieve the last item reported by the client."""
401+
# noinspection PyUnresolvedReferences
383402
return self._item_stack[-1] if len(self._item_stack) > 0 else None

reportportal_client/core/rp_requests.py

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

2222
import json
23+
import logging
2324
import uuid
2425

2526
from reportportal_client.core.rp_file import RPFile
@@ -37,12 +38,15 @@
3738
)
3839
from .rp_responses import RPResponse
3940

41+
logger = logging.getLogger(__name__)
42+
4043

4144
class HttpRequest:
4245
"""This model stores attributes related to RP HTTP requests."""
4346

4447
def __init__(self, session_method, url, data=None, json=None,
45-
files=None, verify_ssl=True, http_timeout=(10, 10)):
48+
files=None, verify_ssl=True, http_timeout=(10, 10),
49+
name=None):
4650
"""Initialize instance attributes.
4751
4852
:param session_method: Method of the requests.Session instance
@@ -54,6 +58,7 @@ def __init__(self, session_method, url, data=None, json=None,
5458
:param http_timeout: a float in seconds for connect and read
5559
timeout. Use a Tuple to specific connect and
5660
read separately.
61+
:param name: request name
5762
"""
5863
self.data = data
5964
self.files = files
@@ -62,9 +67,11 @@ def __init__(self, session_method, url, data=None, json=None,
6267
self.url = url
6368
self.verify_ssl = verify_ssl
6469
self.http_timeout = http_timeout
70+
self.name = name
6571

6672
def make(self):
6773
"""Make HTTP request to the Report Portal API."""
74+
exceptions = []
6875
for attempt in range(SEND_RETRY_COUNT):
6976
try:
7077
return RPResponse(self.session_method(
@@ -73,9 +80,16 @@ def make(self):
7380
timeout=self.http_timeout)
7481
)
7582
# https://github.com/reportportal/client-Python/issues/39
76-
except KeyError:
77-
if attempt + 1 == SEND_RETRY_COUNT:
78-
raise
83+
except (KeyError, IOError, ValueError) as exc:
84+
exceptions.append(exc)
85+
86+
logger.warning(
87+
"Report Portal %s request failed after %d attempts",
88+
self.name,
89+
SEND_RETRY_COUNT,
90+
exc_info=exceptions[-1],
91+
stack_info=True
92+
)
7993

8094

8195
class RPRequestBase(object):

reportportal_client/core/rp_requests.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ class HttpRequest:
1515
json: Optional[Dict] = ...
1616
verify_ssl: Optional[bool] = ...
1717
http_timeout: Union[float, Tuple[float, float]] = ...
18+
name: Optional[Text] = ...
1819
def __init__(self,
1920
session_method: Callable,
2021
url: Text,
2122
data = Optional[Union[Dict, List[Union[tuple, ByteString, IO]]]],
2223
json = Optional[Dict],
2324
files = Optional[Dict],
24-
verify_ssl = Optional[bool]) -> None: ...
25-
def make(self) -> RPResponse: ...
25+
verify_ssl = Optional[bool],
26+
name = Optional[Text]) -> None: ...
27+
def make(self) -> Optional[RPResponse]: ...
2628

2729

2830
class RPRequestBase(metaclass=AbstractBaseClass):

reportportal_client/service.py

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ def start_launch(self,
232232
"rerunOf": rerunOf
233233
}
234234
url = uri_join(self.base_url_v2, "launch")
235-
r = self.session.request(
236-
method='POST',
237-
url=url,
238-
json=data,
239-
verify=self.verify_ssl,
240-
timeout=self.http_timeout
241-
)
235+
try:
236+
r = self.session.post(url, json=data, verify=self.verify_ssl,
237+
timeout=self.http_timeout)
238+
except (ValueError, KeyError, IOError) as exc:
239+
logger.warning("Report Portal Start Launch request failed",
240+
exc_info=exc, stack_info=True)
241+
return
242242
self.launch_id = _get_id(r)
243243
logger.debug("start_launch - ID: %s", self.launch_id)
244244
return self.launch_id
@@ -261,12 +261,15 @@ def finish_launch(self, end_time, status=None, attributes=None, **kwargs):
261261
"attributes": verify_value_length(attributes)
262262
}
263263
url = uri_join(self.base_url_v2, "launch", self.launch_id, "finish")
264-
r = self.session.request(
265-
method='PUT',
266-
url=url,
267-
json=data,
268-
verify=self.verify_ssl
269-
)
264+
try:
265+
r = self.session.put(url, json=data, verify=self.verify_ssl,
266+
timeout=self.http_timeout)
267+
except (ValueError, KeyError, IOError) as exc:
268+
logger.warning(
269+
"Report Portal Finish Launch request failed",
270+
exc_info=exc, stack_info=True
271+
)
272+
return
270273
logger.debug("finish_launch - ID: %s", self.launch_id)
271274
return _get_msg(r)
272275

@@ -285,11 +288,15 @@ def get_launch_info(self, max_retries=5):
285288

286289
for _ in range(max_retries):
287290
logger.debug("get_launch_info - ID: %s", self.launch_id)
288-
resp = self.session.request(
289-
method='GET',
290-
url=url,
291-
verify=self.verify_ssl,
292-
timeout=self.http_timeout)
291+
try:
292+
resp = self.session.get(url, verify=self.verify_ssl,
293+
timeout=self.http_timeout)
294+
except (ValueError, KeyError, IOError) as exc:
295+
logger.warning(
296+
"Report Portal Launch Info request failed",
297+
exc_info=exc, stack_info=True
298+
)
299+
continue
293300

294301
if resp.status_code == 200:
295302
launch_info = _get_json(resp)
@@ -374,13 +381,15 @@ def start_test_item(self,
374381
url = uri_join(self.base_url_v2, "item", parent_item_id)
375382
else:
376383
url = uri_join(self.base_url_v2, "item")
377-
r = self.session.request(
378-
method='POST',
379-
url=url,
380-
json=data,
381-
verify=self.verify_ssl,
382-
timeout=self.http_timeout
383-
)
384+
try:
385+
r = self.session.post(url, json=data, verify=self.verify_ssl,
386+
timeout=self.http_timeout)
387+
except (ValueError, KeyError, IOError) as exc:
388+
logger.warning(
389+
"Report Portal Start Item request failed",
390+
exc_info=exc, stack_info=True
391+
)
392+
return
384393

385394
item_id = _get_id(r)
386395
logger.debug("start_test_item - ID: %s", item_id)
@@ -400,13 +409,15 @@ def update_test_item(self, item_uuid, attributes=None, description=None):
400409
}
401410
item_id = self.get_item_id_by_uuid(item_uuid)
402411
url = uri_join(self.base_url_v1, "item", item_id, "update")
403-
r = self.session.request(
404-
method='PUT',
405-
url=url,
406-
json=data,
407-
verify=self.verify_ssl,
408-
timeout=self.http_timeout
409-
)
412+
try:
413+
r = self.session.put(url, json=data, verify=self.verify_ssl,
414+
timeout=self.http_timeout)
415+
except (ValueError, KeyError, IOError) as exc:
416+
logger.warning(
417+
"Report Portal Update Item request failed",
418+
exc_info=exc, stack_info=True
419+
)
420+
return
410421
logger.debug("update_test_item - Item: %s", item_id)
411422
return _get_msg(r)
412423

@@ -443,12 +454,14 @@ def finish_test_item(self,
443454
"attributes": verify_value_length(attributes)
444455
}
445456
url = uri_join(self.base_url_v2, "item", item_id)
446-
r = self.session.request(
447-
method='PUT',
448-
url=url,
449-
json=data,
450-
verify=self.verify_ssl
451-
)
457+
try:
458+
r = self.session.put(url, json=data, verify=self.verify_ssl)
459+
except (ValueError, KeyError, IOError) as exc:
460+
logger.warning(
461+
"Report Portal Finish Item request failed",
462+
exc_info=exc, stack_info=True
463+
)
464+
return
452465
logger.debug("finish_test_item - ID: %s", item_id)
453466
return _get_msg(r)
454467

@@ -459,14 +472,16 @@ def get_item_id_by_uuid(self, uuid):
459472
:return str: Test item id
460473
"""
461474
url = uri_join(self.base_url_v1, "item", "uuid", uuid)
462-
return _get_json(
463-
self.session.request(
464-
method='GET',
465-
url=url,
466-
verify=self.verify_ssl,
467-
timeout=self.http_timeout
475+
try:
476+
r = self.session.get(url, verify=self.verify_ssl,
477+
timeout=self.http_timeout)
478+
except (ValueError, KeyError, IOError) as exc:
479+
logger.warning(
480+
"Report Portal Item Details request failed",
481+
exc_info=exc, stack_info=True
468482
)
469-
)["id"]
483+
return
484+
return _get_json(r)["id"]
470485

471486
def get_project_settings(self):
472487
"""
@@ -475,13 +490,15 @@ def get_project_settings(self):
475490
:return: json body
476491
"""
477492
url = uri_join(self.base_url_v1, "settings")
478-
r = self.session.request(
479-
method='GET',
480-
url=url,
481-
json={},
482-
verify=self.verify_ssl,
483-
timeout=self.http_timeout
484-
)
493+
try:
494+
r = self.session.get(url, json={}, verify=self.verify_ssl,
495+
timeout=self.http_timeout)
496+
except (ValueError, KeyError, IOError) as exc:
497+
logger.warning(
498+
"Report Portal Project Settings request failed",
499+
exc_info=exc, stack_info=True
500+
)
501+
return
485502
logger.debug("settings")
486503
return _get_json(r)
487504

@@ -555,18 +572,19 @@ def _log_batch(self, log_data, force=False):
555572
)
556573
)]
557574
files.extend(attachments)
558-
for i in range(POST_LOGBATCH_RETRY_COUNT):
575+
exceptions = []
576+
for _ in range(POST_LOGBATCH_RETRY_COUNT):
559577
try:
560-
r = self.session.request(
561-
method='POST',
562-
url=url,
563-
files=files,
564-
verify=self.verify_ssl,
565-
timeout=self.http_timeout
566-
)
578+
r = self.session.post(url, files=files, verify=self.verify_ssl,
579+
timeout=self.http_timeout)
567580
logger.debug("log_batch response: %s", r.text)
568581
self._batch_logs = []
569582
return _get_data(r)
570-
except KeyError:
571-
if i + 1 == POST_LOGBATCH_RETRY_COUNT:
572-
raise
583+
except (ValueError, KeyError, IOError) as exc:
584+
exceptions.append(exc)
585+
586+
logger.warning(
587+
"Report Portal Batch Log request failed after %d attempts",
588+
POST_LOGBATCH_RETRY_COUNT,
589+
exc_info=exceptions[-1], stack_info=True
590+
)

0 commit comments

Comments
 (0)