From ad146e632cb051191dfad2ac153e24de91d48884 Mon Sep 17 00:00:00 2001 From: Vadzim Hushchanskou Date: Fri, 13 Oct 2023 14:13:09 +0300 Subject: [PATCH] Add factory function --- reportportal_client/__init__.py | 93 ++++++++++++++++++++++++++++++- reportportal_client/aio/client.py | 2 +- reportportal_client/client.py | 2 +- tests/test_client_factory.py | 32 +++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 tests/test_client_factory.py diff --git a/reportportal_client/__init__.py b/reportportal_client/__init__.py index e5427ff0..bd2b4f5a 100644 --- a/reportportal_client/__init__.py +++ b/reportportal_client/__init__.py @@ -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', diff --git a/reportportal_client/aio/client.py b/reportportal_client/aio/client.py index eaf7ac42..2a5dca21 100644 --- a/reportportal_client/aio/client.py +++ b/reportportal_client/aio/client.py @@ -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 @@ -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, diff --git a/reportportal_client/client.py b/reportportal_client/client.py index 9a106679..1b5b73a1 100644 --- a/reportportal_client/client.py +++ b/reportportal_client/client.py @@ -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, diff --git a/tests/test_client_factory.py b/tests/test_client_factory.py new file mode 100644 index 00000000..5b0461bb --- /dev/null +++ b/tests/test_client_factory.py @@ -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)