Skip to content

Commit

Permalink
Add factory function
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Oct 13, 2023
1 parent 7ecb535 commit ad146e6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
93 changes: 92 additions & 1 deletion reportportal_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,109 @@
# limitations under the License

"""This package is the base package for ReportPortal client."""
import warnings
from typing import Optional, Any, Union

import aenum

# noinspection PyProtectedMember
from reportportal_client._internal.local import current, set_current
from reportportal_client.logs import RPLogger, RPLogHandler
from reportportal_client.aio.client import AsyncRPClient, BatchedRPClient, ThreadedRPClient
from reportportal_client.client import RP, RPClient, OutputType
from reportportal_client.logs import RPLogger, RPLogHandler
from reportportal_client.steps import step


class ClientType(aenum.Enum):
"""Enum of possible type of ReportPortal clients."""

SYNC = aenum.auto()
ASYNC = aenum.auto()
ASYNC_THREAD = aenum.auto()
ASYNC_BATCHED = aenum.auto()


# noinspection PyIncorrectDocstring
def create_client(
client_type: ClientType,
endpoint: str,
project: str,
*,
api_key: str = None,
**kwargs: Any
) -> Optional[RP]:
"""Create and ReportPortal Client based on the type and arguments provided.
:param client_type: Type of the Client to create.
:type client_type: ClientType
:param endpoint: Endpoint of the ReportPortal service.
:type endpoint: str
:param project: Project name to report to.
:type project: str
:param api_key: Authorization API key.
:type api_key: str
:param launch_uuid: A launch UUID to use instead of starting own one.
:type launch_uuid: str
:param is_skipped_an_issue: Option to mark skipped tests as not 'To Investigate' items on the server
side.
:type is_skipped_an_issue: bool
:param verify_ssl: Option to skip ssl verification.
:type verify_ssl: Union[str, bool]
:param retries: Number of retry attempts to make in case of connection / server
errors.
:type retries: int
:param max_pool_size: Option to set the maximum number of connections to save the pool.
:type max_pool_size: int
:param http_timeout : A float in seconds for connect and read timeout. Use a Tuple to
specific connect and read separately.
:type http_timeout: Tuple[float, float]
:param mode: Launch mode, all Launches started by the client will be in that mode.
:type mode: str
:param launch_uuid_print: Print Launch UUID into passed TextIO or by default to stdout.
:type launch_uuid_print: bool
:param print_output: Set output stream for Launch UUID printing.
:type print_output: OutputType
:param log_batch_size: Option to set the maximum number of logs that can be processed in one
batch.
:type log_batch_size: int
:param log_batch_payload_limit: Maximum size in bytes of logs that can be processed in one batch.
:type log_batch_payload_limit: int
:param keepalive_timeout: For Async Clients only. Maximum amount of idle time in seconds before
force connection closing.
:type keepalive_timeout: int
:param task_timeout: For Async Threaded and Batched Clients only. Time limit in seconds for a
Task processing.
:type task_timeout: float
:param shutdown_timeout: For Async Threaded and Batched Clients only. Time limit in seconds for
shutting down internal Tasks.
:type shutdown_timeout: float
:param trigger_num: For Async Batched Client only. Number of tasks which triggers Task batch
execution.
:param trigger_interval: For Async Batched Client only. Time limit which triggers Task batch
execution.
:return: ReportPortal Client instance.
"""
if client_type is ClientType.SYNC:
return RPClient(endpoint, project, api_key=api_key, **kwargs)
if client_type is ClientType.ASYNC:
return AsyncRPClient(endpoint, project, api_key=api_key, **kwargs)
if client_type is ClientType.ASYNC_THREAD:
return ThreadedRPClient(endpoint, project, api_key=api_key, **kwargs)
if client_type is ClientType.ASYNC_BATCHED:
return BatchedRPClient(endpoint, project, api_key=api_key, **kwargs)
warnings.warn(f'Unknown ReportPortal Client type requested: {client_type}', RuntimeWarning, stacklevel=2)


__all__ = [
'ClientType',
'create_client',
'current',
'set_current',
'RP',
'RPClient',
'AsyncRPClient',
'BatchedRPClient',
'ThreadedRPClient',
'OutputType',
'RPLogger',
'RPLogHandler',
Expand Down
2 changes: 1 addition & 1 deletion reportportal_client/aio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import aiohttp
import certifi

from reportportal_client import RP, OutputType
# noinspection PyProtectedMember
from reportportal_client._internal.aio.http import RetryingClientSession
# noinspection PyProtectedMember
Expand All @@ -46,6 +45,7 @@
# noinspection PyProtectedMember
from reportportal_client._internal.static.defines import NOT_FOUND, NOT_SET
from reportportal_client.aio.tasks import Task
from reportportal_client.client import RP, OutputType
from reportportal_client.core.rp_issues import Issue
from reportportal_client.core.rp_requests import (LaunchStartRequest, AsyncHttpRequest, AsyncItemStartRequest,
AsyncItemFinishRequest, LaunchFinishRequest, RPFile,
Expand Down
2 changes: 1 addition & 1 deletion reportportal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def __init__(
api_key: str = None,
log_batch_size: int = 20,
is_skipped_an_issue: bool = True,
verify_ssl: bool = True,
verify_ssl: Union[bool, str] = True,
retries: int = None,
max_pool_size: int = 50,
launch_uuid: str = None,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_client_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2023 EPAM Systems
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License

# noinspection PyPackageRequirements
import pytest

from reportportal_client import (create_client, ClientType, RPClient, AsyncRPClient, ThreadedRPClient,
BatchedRPClient)


@pytest.mark.parametrize(
'requested_type, expected_type',
[
(ClientType.SYNC, RPClient),
(ClientType.ASYNC, AsyncRPClient),
(ClientType.ASYNC_THREAD, ThreadedRPClient),
(ClientType.ASYNC_BATCHED, BatchedRPClient),
]
)
def test_client_factory_types(requested_type: ClientType, expected_type):
result = create_client(requested_type, 'http://endpoint', 'default_personal')
assert isinstance(result, expected_type)

0 comments on commit ad146e6

Please sign in to comment.