Skip to content

Commit 7bb9334

Browse files
authored
Merge pull request #5 from krasoffski/authfix
Adjusted working with requests
2 parents 759cb4e + fb75dce commit 7bb9334

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

reportportal_client/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@
22
from .model import (OperationCompletionRS, EntryCreatedRS, StartTestItemRQ,
33
StartLaunchRQ, SaveLogRQ, FinishTestItemRQ,
44
FinishExecutionRQ, StartRQ)
5+
6+
__all__ = (
7+
EntryCreatedRS,
8+
FinishExecutionRQ,
9+
FinishTestItemRQ,
10+
OperationCompletionRS,
11+
ReportPortalService,
12+
SaveLogRQ,
13+
StartLaunchRQ,
14+
StartRQ,
15+
StartTestItemRQ,
16+
)

reportportal_client/model/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
from .request import (FinishExecutionRQ, FinishTestItemRQ, SaveLogRQ,
22
StartLaunchRQ, StartTestItemRQ, StartRQ)
33
from .response import EntryCreatedRS, OperationCompletionRS
4+
5+
__all__ = (
6+
EntryCreatedRS,
7+
FinishExecutionRQ,
8+
FinishTestItemRQ,
9+
OperationCompletionRS,
10+
SaveLogRQ,
11+
StartLaunchRQ,
12+
StartRQ,
13+
StartTestItemRQ,
14+
)

reportportal_client/model/request.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ def __init__(self):
66
super(RQ, self).__init__()
77

88
def as_dict(self):
9-
res = {}
10-
for k, v in self.__dict__.items():
11-
if v:
12-
res[k] = v
13-
return res
9+
return dict((k, v) for k, v in self.__dict__.items() if v)
1410

1511
@property
1612
def data(self):
@@ -21,7 +17,7 @@ class StartRQ(RQ):
2117
def __init__(self, name=None, description=None, tags=None,
2218
start_time=None):
2319
super(StartRQ, self).__init__()
24-
#Field 'name' should have size from '1' to '256'.
20+
# Field 'name' should have size from '1' to '256'.
2521
if len(name) > 255:
2622
self.name = "{0} ...".format(name[:250])
2723
else:

reportportal_client/service.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import os
2-
31
import requests
2+
from requests.auth import AuthBase
43

54
from .model import (EntryCreatedRS, OperationCompletionRS)
65

76

7+
class BearerAuth(AuthBase):
8+
"""Attaches HTTP Bearer Authentication to the given Request object."""
9+
def __init__(self, token):
10+
self.token = token
11+
12+
def __call__(self, r):
13+
r.headers["Authorization"] = "bearer {0}".format(self.token)
14+
return r
15+
16+
817
class ReportPortalService(object):
918
"""Service class with report portal event callbacks."""
1019

@@ -15,7 +24,7 @@ def __init__(self, endpoint, project, token, api_base=None):
1524
endpoint: endpoint of report portal service.
1625
project: project name to use for launch names.
1726
token: authorization token.
18-
api_base: defaults to api/v1, can be customized to use another version.
27+
api_base: defaults to api/v1, can be changed to other version.
1928
"""
2029
super(ReportPortalService, self).__init__()
2130
self.endpoint = endpoint
@@ -26,19 +35,19 @@ def __init__(self, endpoint, project, token, api_base=None):
2635
self.base_url = self.uri_join(self.endpoint,
2736
self.api_base,
2837
self.project)
29-
self.headers = {"Content-Type": "application/json",
30-
"Authorization": "{0} {1}".format("bearer",
31-
self.token)}
3238
self.session = requests.Session()
39+
self.session.auth = BearerAuth(self.token)
3340

3441
@staticmethod
3542
def uri_join(*uri_parts):
3643
"""Join uri parts.
3744
38-
Avoiding usage of urlparse.urljoin and os.path.join as it does not clearly join parts.
45+
Avoiding usage of urlparse.urljoin and os.path.join
46+
as it does not clearly join parts.
3947
4048
Args:
41-
*uri_parts: tuple of values for join, can contain back and forward slashes (will be stripped up).
49+
*uri_parts: tuple of values for join, can contain back and forward
50+
slashes (will be stripped up).
4251
4352
Returns:
4453
An uri string.
@@ -48,33 +57,28 @@ def uri_join(*uri_parts):
4857

4958
def start_launch(self, start_launch_rq):
5059
url = self.uri_join(self.base_url, "launch")
51-
r = self.session.post(url=url, headers=self.headers,
52-
data=start_launch_rq.data)
60+
r = self.session.post(url=url, json=start_launch_rq.as_dict())
5361
return EntryCreatedRS(raw=r.text)
5462

5563
def finish_launch(self, launch_id, finish_execution_rq):
5664
url = self.uri_join(self.base_url, "launch", launch_id, "finish")
57-
r = self.session.put(url=url, headers=self.headers,
58-
data=finish_execution_rq.data)
65+
r = self.session.put(url=url, json=finish_execution_rq.as_dict())
5966
return OperationCompletionRS(raw=r.text)
6067

6168
def start_test_item(self, parent_item_id, start_test_item_rq):
6269
if parent_item_id is not None:
6370
url = self.uri_join(self.base_url, "item", parent_item_id)
6471
else:
6572
url = self.uri_join(self.base_url, "item")
66-
r = self.session.post(url=url, headers=self.headers,
67-
data=start_test_item_rq.data)
73+
r = self.session.post(url=url, json=start_test_item_rq.as_dict())
6874
return EntryCreatedRS(raw=r.text)
6975

7076
def finish_test_item(self, item_id, finish_test_item_rq):
7177
url = self.uri_join(self.base_url, "item", item_id)
72-
r = self.session.put(url=url, headers=self.headers,
73-
data=finish_test_item_rq.data)
78+
r = self.session.put(url=url, json=finish_test_item_rq.as_dict())
7479
return OperationCompletionRS(raw=r.text)
7580

7681
def log(self, save_log_rq):
7782
url = self.uri_join(self.base_url, "log")
78-
r = self.session.post(url=url, headers=self.headers,
79-
data=save_log_rq.data)
83+
r = self.session.post(url=url, json=save_log_rq.as_dict())
8084
return EntryCreatedRS(raw=r.text)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
name='reportportal-client',
55
packages=find_packages(),
66
version='2.5.5',
7-
description='Python client for ReportPortal',
7+
description='Python client for Report Portal',
88
author='Artsiom Tkachou',
99
author_email='[email protected]',
1010
url='https://github.com/reportportal/client-Python',
1111
download_url='https://github.com/reportportal/client-Python/tarball/2.5.5',
1212
keywords=['testing', 'reporting', 'reportportal'],
1313
classifiers=[],
14-
install_requires=["requests"],
14+
install_requires=["requests>=2.4.2"],
1515
)

0 commit comments

Comments
 (0)