diff --git a/README.md b/README.md index d97eddf..fbb854a 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ This library enables you to manage Artifactory resources such as users, groups, - [Usage](#usage) * [Authentication](#authentication) * [SSL Cert Verification Options](#ssl-cert-verification-options) + * [Timeout option](#timeout-option) * [Admin objects](#admin-objects) + [User](#user) + [Group](#group) @@ -90,6 +91,17 @@ art = Artifactory(url="ARTIFACTORY_URL", auth=('USERNAME','PASSWORD_OR_API_KEY') > `verify` can be also set as a boolean to enable/disable SSL host verification. +### Timeout option + +Use timeout option to limit connect and read timeout in case the artifactory server is not responding in a timely manner. + +```python +from pyartifactory import Artifactory +art = Artifactory(url="ARTIFACTORY_URL", auth=('USERNAME','PASSWORD_OR_API_KEY'), api_version=1, timeout=60) +``` + +> `timeout` is None by default. + ### Admin objects #### User diff --git a/pyartifactory/models/auth.py b/pyartifactory/models/auth.py index ffb0100..b9e52e5 100644 --- a/pyartifactory/models/auth.py +++ b/pyartifactory/models/auth.py @@ -16,6 +16,7 @@ class AuthModel(BaseModel): verify: Union[bool, str] = True cert: Optional[str] = None api_version: int = 1 + timeout: Optional[int] = None class ApiKeyModel(BaseModel): diff --git a/pyartifactory/objects/object.py b/pyartifactory/objects/object.py index 6c8bf6d..54b8daa 100644 --- a/pyartifactory/objects/object.py +++ b/pyartifactory/objects/object.py @@ -21,6 +21,7 @@ def __init__(self, artifactory: AuthModel) -> None: self._api_version = self._artifactory.api_version self._verify = self._artifactory.verify self._cert = self._artifactory.cert + self._timeout = self._artifactory.timeout self.session = requests.Session() def _get(self, route: str, **kwargs) -> Response: @@ -84,6 +85,7 @@ def _generic_http_method_request( **kwargs, verify=self._verify, cert=self._cert, + timeout=self._timeout, ) if raise_for_status: response.raise_for_status() diff --git a/tests/test_object.py b/tests/test_object.py new file mode 100644 index 0000000..163d19b --- /dev/null +++ b/tests/test_object.py @@ -0,0 +1,18 @@ +from __future__ import annotations + +from pyartifactory import ArtifactoryArtifact +from pyartifactory.models import AuthModel + +URL = "http://localhost:8080/artifactory" +AUTH = ("user", "password_or_apiKey") +ARTIFACT_REPO = "my_repository" + + +# Test that timeout is used by requests +def test_get_artifact_folder_info_timeout(mocker): + artifactory = ArtifactoryArtifact(AuthModel(url=URL, auth=AUTH, timeout=1)) + artifactory.session = mocker.MagicMock() + mocker.patch("pyartifactory.models.artifact.ArtifactFolderInfoResponse.model_validate") + artifactory.session.return_value = mocker.MagicMock() + artifactory.info("ARTIFACT_REPO") + assert artifactory.session.get.call_args_list[0][1]["timeout"] == 1