Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reinstantiate client unless needed #76

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ While the project is still on major version 0, breaking changes may be introduce

<!-- ## Unreleased -->

## [0.23.4](https://github.com/unioslo/harborapi/tree/harborapi-v0.23.4) - 2024-03-01

### Changed

- `HarborAsyncClient.authenticate(verify=...)` will no longer instantiate a new client object if the `verify` value is identical to current one.


## [0.23.3](https://github.com/unioslo/harborapi/tree/harborapi-v0.23.3) - 2024-03-01

### Fixed
Expand Down
35 changes: 17 additions & 18 deletions harborapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,28 @@ def __init__(
raise ValueError("A Harbor API URL is required.")
self.authenticate(username, secret, basicauth, credentials_file, url, **kwargs)

# Instantiate persistent HTTP client using the redirect policy
# NOTE: any reason we don't specify headers here too?
self.client = httpx.AsyncClient(
follow_redirects=follow_redirects,
timeout=timeout,
cookies=CookieDiscarder(),
verify=verify,
)

self.validate = validate
self.raw = raw
self.retry = retry
self.verify = verify
self.timeout = timeout
self.follow_redirects = follow_redirects

if logging or os.environ.get("HARBORAPI_LOGGING", "") == "1":
enable_logging()

self.response_log = ResponseLog(max_logs=max_logs)
self.client = self._get_client()

def _get_client(self) -> httpx.AsyncClient:
"""Returns a new HTTPX client instance."""
# NOTE: any reason we don't specify headers here too?
return httpx.AsyncClient(
follow_redirects=self.follow_redirects,
timeout=self.timeout,
cookies=CookieDiscarder(),
verify=self.verify,
)

def authenticate(
self,
Expand Down Expand Up @@ -321,15 +326,9 @@ def authenticate(

# If user want to change SSL verification, we have to reinstantiate the client
# https://www.python-httpx.org/advanced/ssl/#ssl-configuration-on-client-instances
if verify is not None:
# TODO: add abstraction for client instantiation
# that can be used in __init__ and here
self.client = httpx.AsyncClient(
follow_redirects=self.client.follow_redirects,
timeout=self.client.timeout,
cookies=CookieDiscarder(),
verify=verify,
)
if verify is not None and verify != self.verify:
self.verify = verify
self.client = self._get_client()

@contextlib.contextmanager
def no_retry(self) -> Generator[None, None, None]:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,27 @@ def test_authenticate(
)


@pytest.mark.parametrize("verify", [True, False])
def test_authenticate_verify(verify: bool) -> None:
client = HarborAsyncClient(
url="https://example.com/api/v2.0",
username="username",
secret="secret",
verify=verify, # instantiate with our verify value
)

# Calling authenticate should NOT change the client instance
client_id_pre = id(client.client)
client.authenticate(verify=verify)
assert client.verify == verify
assert id(client.client) == client_id_pre

# Calling with a different value should instantiate a new client
client.authenticate(verify=not verify)
assert client.verify == (not verify)
assert id(client.client) != client_id_pre


@pytest.mark.asyncio
@pytest.mark.parametrize(
"status_code",
Expand Down
Loading