Skip to content

Commit 3c4bdd9

Browse files
author
Dzmitry Humianiuk
authored
Merge pull request #13 from frizzby/async_requests
Async client requests and batch logging
2 parents 695198d + 3861e09 commit 3c4bdd9

File tree

8 files changed

+551
-262
lines changed

8 files changed

+551
-262
lines changed

README.md

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,18 @@ pip install reportportal-client
2626
Main classes are:
2727

2828
- reportportal_client.ReportPortalService
29-
- reportportal_client.StartLaunchRQ
30-
- reportportal_client.StartTestItemRQ
31-
- reportportal_client.FinishTestItemRQ
32-
- reportportal_client.FinishExecutionRQ
33-
- reportportal_client.SaveLogRQ
29+
- reportportal_client.ReportPortalServiceAsync
3430

3531
Basic usage example:
3632

3733
```python
3834
import os
3935
import subprocess
40-
from time import time
36+
import traceback
4137
from mimetypes import guess_type
38+
from time import time
4239

43-
from reportportal_client import (ReportPortalService,
44-
FinishExecutionRQ,
45-
StartLaunchRQ, StartTestItemRQ,
46-
FinishTestItemRQ, SaveLogRQ)
40+
from reportportal_client import ReportPortalServiceAsync
4741

4842

4943
def timestamp():
@@ -57,73 +51,75 @@ token = "1adf271d-505f-44a8-ad71-0afbdf8c83bd"
5751
launch_name = "Test launch"
5852
launch_doc = "Testing logging with attachment."
5953

60-
service = ReportPortalService(endpoint=endpoint, project=project, token=token)
6154

62-
# Create start launch request.
63-
sl_rq = StartLaunchRQ(name=launch_name,
64-
start_time=timestamp(),
65-
description=launch_doc)
55+
def my_error_handler(exc_info):
56+
"""
57+
This callback function will be called by async service client when error occurs.
58+
Return True if error is not critical and you want to continue work.
59+
:param exc_info: result of sys.exc_info() -> (type, value, traceback)
60+
:return:
61+
"""
62+
print("Error occured: {}".format(exc_info[1]))
63+
traceback.print_exception(*exc_info)
6664

67-
# Start launch.
68-
launch = service.start_launch(sl_rq)
6965

70-
# Create start test item request.
71-
sti_rq = StartTestItemRQ(name="Test Case",
72-
description="First Test Case",
73-
tags=["Image", "Smoke"],
74-
start_time=timestamp(),
75-
launch_id=launch.id,
76-
type="TEST")
66+
service = ReportPortalServiceAsync(endpoint=endpoint, project=project,
67+
token=token, error_handler=my_error_handler)
68+
69+
# Start launch.
70+
launch = service.start_launch(name=launch_name,
71+
start_time=timestamp(),
72+
description=launch_doc)
7773

7874
# Start test item.
79-
test = service.start_test_item(parent_item_id=None, start_test_item_rq=sti_rq)
75+
test = service.start_test_item(name="Test Case",
76+
description="First Test Case",
77+
tags=["Image", "Smoke"],
78+
start_time=timestamp(),
79+
item_type="TEST")
8080

8181
# Create text log message with INFO level.
82-
service.log(SaveLogRQ(item_id=test.id,
83-
time=timestamp(),
84-
message="Hello World!",
85-
level="INFO"))
82+
service.log(time=timestamp(),
83+
message="Hello World!",
84+
level="INFO")
8685

8786
# Create log message with attached text output and WARN level.
88-
service.attach(SaveLogRQ(item_id=test.id,
89-
time=timestamp(),
90-
message="Too high memory usage!",
91-
level="WARN"),
92-
name="free_memory.txt",
93-
data=subprocess.check_output("free -h".split()))
94-
95-
# Create log message with piped binary file, INFO level and custom mimetype.
87+
service.log(time=timestamp(),
88+
message="Too high memory usage!",
89+
level="WARN",
90+
attachment={
91+
"name": "free_memory.txt",
92+
"data": subprocess.check_output("ps".split()),
93+
"mime": "text/plain"
94+
})
95+
96+
# Create log message with binary file, INFO level and custom mimetype.
9697
image = "/tmp/image.png"
97-
sl_rq = SaveLogRQ(test.id, timestamp(), "Screen shot of issue.", "INFO")
9898
with open(image, "rb") as fh:
99-
service.attach(save_log_rq=sl_rq,
100-
name=os.path.basename(image),
101-
data=fh,
102-
mime=guess_type(image)[0] or "application/octet-stream")
103-
104-
# Create log message with binary data and INFO level.
105-
filebin = "/tmp/file.bin"
106-
with open(filebin, "rb") as fd:
107-
bindata = fd.read()
108-
# Note here that we pass binary data instead of file handle.
109-
service.attach(SaveLogRQ(item_id=test.id,
110-
time=timestamp(),
111-
message="Binary data file.",
112-
level="INFO"),
113-
name="file.bin",
114-
data=bindata,
115-
mime="application/octet-stream")
116-
117-
# Create finish test item request.
118-
fti_rq = FinishTestItemRQ(end_time=timestamp(), status="PASSED")
99+
attachment = {
100+
"name": os.path.basename(image),
101+
"data": fh.read(),
102+
"mime": guess_type(image)[0] or "application/octet-stream"
103+
}
104+
service.log(timestamp(), "Screen shot of issue.", "INFO", attachment)
105+
106+
# Create log message supplying only contents
107+
service.log(
108+
timestamp(),
109+
"running processes",
110+
"INFO",
111+
attachment=subprocess.check_output("ps aux".split()))
119112

120113
# Finish test item.
121-
service.finish_test_item(item_id=test.id, finish_test_item_rq=fti_rq)
114+
service.finish_test_item(end_time=timestamp(), status="PASSED")
122115

123-
# Create finish launch request.
124-
fl_rq = FinishExecutionRQ(end_time=timestamp(), status="PASSED")
125116
# Finish launch.
126-
service.finish_launch(launch.id, fl_rq)
117+
service.finish_launch(end_time=timestamp())
118+
119+
# Due to async nature of the service we need to call terminate() method which
120+
# ensures all pending requests to server are proccessed.
121+
# Failure to call terminate() may result in lost data.
122+
service.terminate()
127123
```
128124

129125

reportportal_client/__init__.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
from .service import ReportPortalService
2-
from .model import (OperationCompletionRS, EntryCreatedRS, StartTestItemRQ,
3-
StartLaunchRQ, SaveLogRQ, FinishTestItemRQ,
4-
FinishExecutionRQ, StartRQ)
2+
from .service_async import ReportPortalServiceAsync
3+
54

65
__all__ = (
7-
EntryCreatedRS,
8-
FinishExecutionRQ,
9-
FinishTestItemRQ,
10-
OperationCompletionRS,
116
ReportPortalService,
12-
SaveLogRQ,
13-
StartLaunchRQ,
14-
StartRQ,
15-
StartTestItemRQ,
7+
ReportPortalServiceAsync,
168
)

reportportal_client/errors.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ class Error(Exception):
22
"""General exception for package."""
33

44

5-
class EntryCreatedError(Error):
5+
class ResponseError(Error):
6+
"""Error in response returned by RP"""
7+
8+
9+
class EntryCreatedError(ResponseError):
610
"""Represents error in case no entry is created.
711
812
No 'id' in the json response.
913
"""
1014

1115

12-
class OperationCompletionError(Error):
16+
class OperationCompletionError(ResponseError):
1317
"""Represents error in case of operation failure.
1418
1519
No 'msg' in the json response.

reportportal_client/model/__init__.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

reportportal_client/model/request.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

reportportal_client/model/response.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)