Skip to content
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 @@ -9,6 +9,13 @@
how a consumer would use the library or CLI tool (e.g. adding unit tests, updating documentation, etc) are not captured
here.

## Unreleased

### Updated

- The CLI and SDK now have user-agent headers consistent with Code42 current standards.


## 2.2.0 - 2024-11-18

### Updated
Expand Down
3 changes: 3 additions & 0 deletions src/_incydr_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
)
@logging_options
def incydr(version, python, script_dir):
# Configure SDK settings
os.environ["INCYDR_USER_AGENT_PREFIX"] = "incydrCLI (Code42; code42.com) "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this intentionally not have a version? Is that because the incydrCLI will always be the same version as the SDK and the SDK logging will always have a version/

Copy link
Contributor Author

@ceciliastevens ceciliastevens Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks like the CLI uses the sdk version as well. However, we could certainly add the version to the prefix as well if you'd like.


if version:
console.print(__version__, highlight=False)
if python:
Expand Down
6 changes: 3 additions & 3 deletions src/_incydr_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from _incydr_sdk.users.client import UsersClient
from _incydr_sdk.watchlists.client import WatchlistsClient

_base_user_agent = user_agent("incydr", __version__)
_base_user_agent = user_agent("incydrSDK", __version__)


class Client:
Expand Down Expand Up @@ -75,8 +75,8 @@ def __init__(

self._session = BaseUrlSession(base_url=self._settings.url)
self._session.headers["User-Agent"] = (
self._settings.user_agent_prefix or "" + _base_user_agent
)
self._settings.user_agent_prefix or ""
) + _base_user_agent
self._session.auth = APIClientAuth(
session=self._session,
api_client_id=self._settings.api_client_id,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import Optional

import pytest

from _incydr_cli.main import incydr


Expand All @@ -9,3 +13,27 @@ def test_cli_auth_missing_error_prints_missing_vars(runner, monkeypatch):
assert "INCYDR_API_CLIENT_SECRET" in result.output
assert "INCYDR_URL" not in result.output
assert "INCYDR_API_CLIENT_ID" not in result.output


@pytest.mark.disable_autouse
def test_cli_user_agent(runner, httpserver_auth):
def starts_with_matcher(
header_name: str, actual: Optional[str], expected: str
) -> bool:
if actual is None:
return False

return actual.startswith(expected)

httpserver_auth.expect_ordered_request(
"/v1/users",
method="GET",
headers={"User-Agent": "incydrCLI"},
header_value_matcher=starts_with_matcher,
).respond_with_json({"users": [], "totalCount": 0})
result = runner.invoke(
incydr, ["users", "list", "--log-stderr", "--log-level", "DEBUG"]
)
httpserver_auth.check()
assert result.exit_code == 0
assert "No results found" in result.output
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@ class Test(Model):
err.value
)
assert "value is not a valid integer" in str(err.value)


def test_user_agent(httpserver_auth: HTTPServer):
c = Client()
assert c._session.headers["User-Agent"].startswith("incydrSDK")
Loading