-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor UserAgent setup for extensibility (#161)
* Refactor UserAgent setup for extensibility Update MockClient, store just User-Agent prefix string and add assertions for MountpointClient prefix. Add hypothesis test for starts with package/version.
- Loading branch information
Showing
8 changed files
with
131 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# // SPDX-License-Identifier: BSD | ||
from typing import List | ||
|
||
from ._version import __version__ | ||
|
||
# https://www.rfc-editor.org/rfc/rfc9110#name-user-agent | ||
|
||
|
||
class UserAgent: | ||
def __init__(self, comments: List[str] = None): | ||
if comments is not None and not isinstance(comments, list): | ||
raise ValueError("Argument comments must be a List[str]") | ||
self._user_agent_prefix = f"{__package__}/{__version__}" | ||
self._comments = comments or [] | ||
|
||
@property | ||
def prefix(self): | ||
comments_str = "; ".join(filter(None, self._comments)) | ||
if comments_str: | ||
return f"{self._user_agent_prefix} ({comments_str})" | ||
return self._user_agent_prefix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# // SPDX-License-Identifier: BSD | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
import pytest | ||
|
||
from s3torchconnector._version import __version__ | ||
from s3torchconnector._user_agent import UserAgent | ||
|
||
DEFAULT_PREFIX = f"s3torchconnector/{__version__}" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"comments, expected_prefix", | ||
[ | ||
(None, DEFAULT_PREFIX), | ||
([], DEFAULT_PREFIX), | ||
([""], DEFAULT_PREFIX), | ||
(["", ""], DEFAULT_PREFIX), | ||
( | ||
["component/version", "metadata"], | ||
f"{DEFAULT_PREFIX} (component/version; metadata)", | ||
), | ||
], | ||
) | ||
def test_user_agent_creation(comments: List[str] | None, expected_prefix: str): | ||
user_agent = UserAgent(comments) | ||
assert user_agent.prefix == expected_prefix | ||
|
||
|
||
def test_default_user_agent_creation(): | ||
user_agent = UserAgent() | ||
assert user_agent.prefix == DEFAULT_PREFIX | ||
|
||
|
||
@pytest.mark.parametrize("invalid_comment", [0, "string"]) | ||
def test_invalid_comments_argument(invalid_comment): | ||
with pytest.raises(ValueError, match="Argument comments must be a List\[str\]"): | ||
UserAgent(invalid_comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters