From 0c9bddf3d68d346a410c7572f355927e4533d342 Mon Sep 17 00:00:00 2001 From: Joe Lee Date: Fri, 22 Dec 2023 16:12:11 +0800 Subject: [PATCH] dynamic create transport obj and remove unnecessary logger (#100) * dynamic create transport obj * remove unnecessary logger --- api4jenkins/__version__.py | 2 +- api4jenkins/http.py | 38 +++++++++++++---------------------- api4jenkins/item.py | 3 --- docs/source/user/example.rst | 12 +++++------ tests/integration/conftest.py | 6 ++++-- tests/unit/test_jenkins.py | 9 +++++++++ tox.ini | 4 ++-- 7 files changed, 36 insertions(+), 38 deletions(-) diff --git a/api4jenkins/__version__.py b/api4jenkins/__version__.py index 0663b37..0599af6 100644 --- a/api4jenkins/__version__.py +++ b/api4jenkins/__version__.py @@ -1,5 +1,5 @@ # encoding: utf-8 -__version__ = '2.0.2' +__version__ = '2.0.3' __title__ = 'api4jenkins' __description__ = 'Jenkins Python Client' __url__ = 'https://github.com/joelee2012/api4jenkins' diff --git a/api4jenkins/http.py b/api4jenkins/http.py index 91b3403..4465c26 100644 --- a/api4jenkins/http.py +++ b/api4jenkins/http.py @@ -1,4 +1,5 @@ # encoding: utf-8 +import inspect import logging from httpx import (AsyncClient, AsyncHTTPTransport, Client, HTTPTransport, @@ -29,20 +30,19 @@ def check_response(response: Response) -> None: response.raise_for_status() +def _new_transport(obj, kwargs): + init_args = { + arg: kwargs.pop(arg) + for arg in inspect.getfullargspec(obj).args + if arg in kwargs + } + return obj(**init_args) + + def new_http_client(**kwargs) -> Client: + trans = _new_transport(HTTPTransport, kwargs) return Client( - transport=HTTPTransport( - verify=kwargs.pop('verify', True), - cert=kwargs.pop('cert', None), - http1=kwargs.pop('http1', True), - http2=kwargs.pop('http2', False), - trust_env=kwargs.pop('trust_env', True), - proxy=kwargs.pop('proxy', None), - uds=kwargs.pop('uds', None), - local_address=kwargs.pop('local_address', None), - retries=kwargs.pop('retries', 0), - socket_options=kwargs.pop('socket_options', None) - ), + transport=trans, **kwargs, event_hooks={'request': [log_request], 'response': [check_response]} ) @@ -58,19 +58,9 @@ async def async_check_response(response: Response) -> None: def new_async_http_client(**kwargs) -> AsyncClient: + trans = _new_transport(AsyncHTTPTransport, kwargs) return AsyncClient( - transport=AsyncHTTPTransport( - verify=kwargs.pop('verify', True), - cert=kwargs.pop('cert', None), - http1=kwargs.pop('http1', True), - http2=kwargs.pop('http2', False), - trust_env=kwargs.pop('trust_env', True), - proxy=kwargs.pop('proxy', None), - uds=kwargs.pop('uds', None), - local_address=kwargs.pop('local_address', None), - retries=kwargs.pop('retries', 0), - socket_options=kwargs.pop('socket_options', None) - ), + transport=trans, **kwargs, event_hooks={'request': [async_log_request], 'response': [async_check_response]} diff --git a/api4jenkins/item.py b/api4jenkins/item.py index feddaea..a4a4090 100644 --- a/api4jenkins/item.py +++ b/api4jenkins/item.py @@ -1,7 +1,6 @@ # encoding: utf-8 import contextlib -import logging import re from importlib import import_module @@ -9,8 +8,6 @@ from .exceptions import ItemNotFoundError -logger = logging.getLogger(__name__) - def camel(s): if s[0] == '_': diff --git a/docs/source/user/example.rst b/docs/source/user/example.rst index 5fe7c32..970b76c 100644 --- a/docs/source/user/example.rst +++ b/docs/source/user/example.rst @@ -146,7 +146,7 @@ build with parameters is supported too:: it's also possiable to iterate jobs of Jenkins, iterate jobs in first level:: # call function straightforward - >>> for job in j.iter_jobs(): + >>> for job in j.iter(): ... print(job) # or pythonic @@ -158,7 +158,7 @@ it's also possiable to iterate jobs of Jenkins, iterate jobs in first level:: or iterate with depth :: - >>> for job in j.iter_jobs(3): + >>> for job in j.iter(3): ... print(job) >>> for job in j(3): @@ -318,8 +318,8 @@ check if any build of project is running get build with given number or display name - >>> build = job.get_build(1) - >>> build = job.get_build("some new build name") + >>> build = job.get(1) + >>> build = job.get("some new build name") or subscript with build number @@ -346,7 +346,7 @@ iterate builds(latest 100 builds) of this project, following are same >>> for build in job: ... print(build) ... - >>> for build in job.iter_builds(): + >>> for build in job.iter(): ... print(build) ... @@ -466,7 +466,7 @@ stop/term/kill build, more detail can be found: https://www.jenkins.io/doc/book/ get job of build: - >>> job = build.get_job() + >>> job = build.project or get previous/next build: diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 5d26b12..a58e5ae 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -6,8 +6,10 @@ from pathlib import Path import pytest -from api4jenkins import Jenkins, Folder, EMPTY_FOLDER_XML, AsyncJenkins, AsyncFolder -from api4jenkins.job import WorkflowJob, AsyncWorkflowJob + +from api4jenkins import (EMPTY_FOLDER_XML, AsyncFolder, AsyncJenkins, Folder, + Jenkins) +from api4jenkins.job import AsyncWorkflowJob, WorkflowJob TEST_DATA_DIR = Path(__file__).with_name('tests_data') diff --git a/tests/unit/test_jenkins.py b/tests/unit/test_jenkins.py index 0212f83..70fc528 100644 --- a/tests/unit/test_jenkins.py +++ b/tests/unit/test_jenkins.py @@ -1,6 +1,8 @@ import pytest +from httpx import HTTPTransport from api4jenkins.exceptions import BadRequestError, ItemNotFoundError +from api4jenkins.http import _new_transport from api4jenkins.item import new_item, snake from api4jenkins.job import AsyncFolder, AsyncWorkflowJob, Folder, WorkflowJob @@ -306,3 +308,10 @@ async def test_dumplicate(self, async_jenkins, async_folder, respx_mock): respx_mock.post(req_url) await async_jenkins.duplicate_job('folder', 'folder2') assert respx_mock.calls[1].request.url == req_url + + +def test_new_transport(): + kwargs = {'verify': True, 'http2': True, 'other': '1111'} + trans = _new_transport(HTTPTransport, kwargs) + assert isinstance(trans, HTTPTransport) + assert kwargs == {'other': '1111'} diff --git a/tox.ini b/tox.ini index e007ddc..0c46f5f 100644 --- a/tox.ini +++ b/tox.ini @@ -6,7 +6,7 @@ envlist = style, pylint, py3 [testenv] deps = respx pytest-cov - pytest-asyncio + pytest-asyncio==0.21.1 commands = pytest -v --cov=api4jenkins tests/unit \ -o junit_family=xunit2 \ @@ -28,7 +28,7 @@ commands = [testenv:integration] deps = pytest-cov - pytest-asyncio + pytest-asyncio==0.21.1 pyyaml passenv = JENKINS_* commands =