Skip to content

Commit ad1aef0

Browse files
Iljushkin, AlexanderIljushkin, Alexander
authored andcommitted
added safe uri join method to avoid os.path.join platform depended method
1 parent 7dd2f9c commit ad1aef0

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

reportportal_client/service.py

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,74 @@
66

77

88
class ReportPortalService(object):
9+
"""Service class with report portal event callbacks."""
910
def __init__(self, endpoint, project, token, api_base=None):
11+
"""Init the service class.
12+
13+
Args:
14+
endpoint: endpoint of report portal service.
15+
project: project name to use for launch names.
16+
token: authorization token.
17+
api_base: defaults to api/v1, can be customized to use another version.
18+
"""
1019
super(ReportPortalService, self).__init__()
1120
self.endpoint = endpoint
1221
if api_base is None:
1322
self.api_base = "api/v1"
1423
self.project = project
1524
self.token = token
16-
self.base_url = os.path.join(self.endpoint,
17-
self.api_base,
18-
self.project)
25+
self.base_url = self.uri_join_safe(self.endpoint,
26+
self.api_base,
27+
self.project)
1928
self.headers = {"Content-Type": "application/json",
2029
"Authorization": "{0} {1}".format("bearer",
2130
self.token)}
2231
self.session = requests.Session()
2332

33+
@staticmethod
34+
def uri_join_safe(*uri_parts):
35+
"""Safe join of uri parts for our case.
36+
37+
Avoiding usage of urlparse.urljoin and os.path.join as it does not clearly join parts.
38+
39+
Args:
40+
*uri_parts: tuple of values for join, can contain back and forward slashes (will be stripped up).
41+
42+
Returns:
43+
Safely joined uri parts.
44+
"""
45+
stripped = [str(i).strip('/').strip('\\') for i in uri_parts]
46+
return '/'.join(stripped)
47+
2448
def start_launch(self, start_launch_rq):
25-
url = os.path.join(self.base_url, "launch")
49+
url = self.uri_join_safe(self.base_url, "launch")
2650
r = self.session.post(url=url, headers=self.headers,
27-
data=start_launch_rq.data)
51+
data=start_launch_rq.data)
2852
return EntryCreatedRS(raw=r.text)
2953

3054
def finish_launch(self, launch_id, finish_execution_rq):
31-
url = os.path.join(self.base_url, "launch", launch_id, "finish")
55+
url = self.uri_join_safe(self.base_url, "launch", launch_id, "finish")
3256
r = self.session.put(url=url, headers=self.headers,
33-
data=finish_execution_rq.data)
57+
data=finish_execution_rq.data)
3458
return OperationCompletionRS(raw=r.text)
3559

3660
def start_test_item(self, parent_item_id, start_test_item_rq):
3761
if parent_item_id is not None:
38-
url = os.path.join(self.base_url, "item", parent_item_id)
62+
url = self.uri_join_safe(self.base_url, "item", parent_item_id)
3963
else:
40-
url = os.path.join(self.base_url, "item")
64+
url = self.uri_join_safe(self.base_url, "item")
4165
r = self.session.post(url=url, headers=self.headers,
42-
data=start_test_item_rq.data)
66+
data=start_test_item_rq.data)
4367
return EntryCreatedRS(raw=r.text)
4468

4569
def finish_test_item(self, item_id, finish_test_item_rq):
46-
url = os.path.join(self.base_url, "item", item_id)
70+
url = self.uri_join_safe(self.base_url, "item", item_id)
4771
r = self.session.put(url=url, headers=self.headers,
48-
data=finish_test_item_rq.data)
72+
data=finish_test_item_rq.data)
4973
return OperationCompletionRS(raw=r.text)
5074

5175
def log(self, save_log_rq):
52-
url = os.path.join(self.base_url, "log")
76+
url = self.uri_join_safe(self.base_url, "log")
5377
r = self.session.post(url=url, headers=self.headers,
54-
data=save_log_rq.data)
78+
data=save_log_rq.data)
5579
return EntryCreatedRS(raw=r.text)

0 commit comments

Comments
 (0)