Skip to content

Commit

Permalink
Add possibility to set requests timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
anancarv authored Jul 27, 2024
2 parents b0196d8 + bd0e65d commit f0bf265
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pyartifactory/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 2 additions & 0 deletions pyartifactory/objects/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_object.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f0bf265

Please sign in to comment.