Skip to content

Adjusted working with requests #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions reportportal_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,15 @@
from .model import (OperationCompletionRS, EntryCreatedRS, StartTestItemRQ,
StartLaunchRQ, SaveLogRQ, FinishTestItemRQ,
FinishExecutionRQ, StartRQ)

__all__ = (
EntryCreatedRS,
FinishExecutionRQ,
FinishTestItemRQ,
OperationCompletionRS,
ReportPortalService,
SaveLogRQ,
StartLaunchRQ,
StartRQ,
StartTestItemRQ,
)
11 changes: 11 additions & 0 deletions reportportal_client/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
from .request import (FinishExecutionRQ, FinishTestItemRQ, SaveLogRQ,
StartLaunchRQ, StartTestItemRQ, StartRQ)
from .response import EntryCreatedRS, OperationCompletionRS

__all__ = (
EntryCreatedRS,
FinishExecutionRQ,
FinishTestItemRQ,
OperationCompletionRS,
SaveLogRQ,
StartLaunchRQ,
StartRQ,
StartTestItemRQ,
)
8 changes: 2 additions & 6 deletions reportportal_client/model/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ def __init__(self):
super(RQ, self).__init__()

def as_dict(self):
res = {}
for k, v in self.__dict__.items():
if v:
res[k] = v
return res
return dict((k, v) for k, v in self.__dict__.items() if v)

@property
def data(self):
Expand All @@ -21,7 +17,7 @@ class StartRQ(RQ):
def __init__(self, name=None, description=None, tags=None,
start_time=None):
super(StartRQ, self).__init__()
#Field 'name' should have size from '1' to '256'.
# Field 'name' should have size from '1' to '256'.
if len(name) > 255:
self.name = "{0} ...".format(name[:250])
else:
Expand Down
40 changes: 22 additions & 18 deletions reportportal_client/service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import os

import requests
from requests.auth import AuthBase

from .model import (EntryCreatedRS, OperationCompletionRS)


class BearerAuth(AuthBase):
"""Attaches HTTP Bearer Authentication to the given Request object."""
def __init__(self, token):
self.token = token

def __call__(self, r):
r.headers["Authorization"] = "bearer {0}".format(self.token)
return r


class ReportPortalService(object):
"""Service class with report portal event callbacks."""

Expand All @@ -15,7 +24,7 @@ def __init__(self, endpoint, project, token, api_base=None):
endpoint: endpoint of report portal service.
project: project name to use for launch names.
token: authorization token.
api_base: defaults to api/v1, can be customized to use another version.
api_base: defaults to api/v1, can be changed to other version.
"""
super(ReportPortalService, self).__init__()
self.endpoint = endpoint
Expand All @@ -26,19 +35,19 @@ def __init__(self, endpoint, project, token, api_base=None):
self.base_url = self.uri_join(self.endpoint,
self.api_base,
self.project)
self.headers = {"Content-Type": "application/json",
"Authorization": "{0} {1}".format("bearer",
self.token)}
self.session = requests.Session()
self.session.auth = BearerAuth(self.token)

@staticmethod
def uri_join(*uri_parts):
"""Join uri parts.

Avoiding usage of urlparse.urljoin and os.path.join as it does not clearly join parts.
Avoiding usage of urlparse.urljoin and os.path.join
as it does not clearly join parts.

Args:
*uri_parts: tuple of values for join, can contain back and forward slashes (will be stripped up).
*uri_parts: tuple of values for join, can contain back and forward
slashes (will be stripped up).

Returns:
An uri string.
Expand All @@ -48,33 +57,28 @@ def uri_join(*uri_parts):

def start_launch(self, start_launch_rq):
url = self.uri_join(self.base_url, "launch")
r = self.session.post(url=url, headers=self.headers,
data=start_launch_rq.data)
r = self.session.post(url=url, json=start_launch_rq.as_dict())
return EntryCreatedRS(raw=r.text)

def finish_launch(self, launch_id, finish_execution_rq):
url = self.uri_join(self.base_url, "launch", launch_id, "finish")
r = self.session.put(url=url, headers=self.headers,
data=finish_execution_rq.data)
r = self.session.put(url=url, json=finish_execution_rq.as_dict())
return OperationCompletionRS(raw=r.text)

def start_test_item(self, parent_item_id, start_test_item_rq):
if parent_item_id is not None:
url = self.uri_join(self.base_url, "item", parent_item_id)
else:
url = self.uri_join(self.base_url, "item")
r = self.session.post(url=url, headers=self.headers,
data=start_test_item_rq.data)
r = self.session.post(url=url, json=start_test_item_rq.as_dict())
return EntryCreatedRS(raw=r.text)

def finish_test_item(self, item_id, finish_test_item_rq):
url = self.uri_join(self.base_url, "item", item_id)
r = self.session.put(url=url, headers=self.headers,
data=finish_test_item_rq.data)
r = self.session.put(url=url, json=finish_test_item_rq.as_dict())
return OperationCompletionRS(raw=r.text)

def log(self, save_log_rq):
url = self.uri_join(self.base_url, "log")
r = self.session.post(url=url, headers=self.headers,
data=save_log_rq.data)
r = self.session.post(url=url, json=save_log_rq.as_dict())
return EntryCreatedRS(raw=r.text)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
name='reportportal-client',
packages=find_packages(),
version='2.5.5',
description='Python client for ReportPortal',
description='Python client for Report Portal',
author='Artsiom Tkachou',
author_email='artsiom_tkachou@epam.com',
url='https://github.com/reportportal/client-Python',
download_url='https://github.com/reportportal/client-Python/tarball/2.5.5',
keywords=['testing', 'reporting', 'reportportal'],
classifiers=[],
install_requires=["requests"],
install_requires=["requests>=2.4.2"],
)