Skip to content

Commit

Permalink
dynamic create transport obj and remove unnecessary logger (#100)
Browse files Browse the repository at this point in the history
* dynamic create transport obj
* remove unnecessary logger
  • Loading branch information
joelee2012 authored Dec 22, 2023
1 parent d5b526e commit 0c9bddf
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion api4jenkins/__version__.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
38 changes: 14 additions & 24 deletions api4jenkins/http.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8
import inspect
import logging

from httpx import (AsyncClient, AsyncHTTPTransport, Client, HTTPTransport,
Expand Down Expand Up @@ -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]}
)
Expand All @@ -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]}
Expand Down
3 changes: 0 additions & 3 deletions api4jenkins/item.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# encoding: utf-8

import contextlib
import logging
import re
from importlib import import_module

import api4jenkins

from .exceptions import ItemNotFoundError

logger = logging.getLogger(__name__)


def camel(s):
if s[0] == '_':
Expand Down
12 changes: 6 additions & 6 deletions docs/source/user/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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)
...

Expand Down Expand Up @@ -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:

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_jenkins.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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'}
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -28,7 +28,7 @@ commands =

[testenv:integration]
deps = pytest-cov
pytest-asyncio
pytest-asyncio==0.21.1
pyyaml
passenv = JENKINS_*
commands =
Expand Down

0 comments on commit 0c9bddf

Please sign in to comment.