Skip to content

Commit ed222b8

Browse files
Merge pull request #138 from code42/INTEG-2751/user-agent
update user-agent
2 parents 2e9a9f4 + 08f95cf commit ed222b8

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
how a consumer would use the library or CLI tool (e.g. adding unit tests, updating documentation, etc) are not captured
1010
here.
1111

12+
## Unreleased
13+
14+
### Updated
15+
16+
- The CLI and SDK now have user-agent headers consistent with Code42 current standards.
17+
18+
1219
## 2.2.0 - 2024-11-18
1320

1421
### Updated

src/_incydr_cli/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
)
5353
@logging_options
5454
def incydr(version, python, script_dir):
55+
# Configure SDK settings
56+
os.environ["INCYDR_USER_AGENT_PREFIX"] = "incydrCLI (Code42; code42.com) "
57+
5558
if version:
5659
console.print(__version__, highlight=False)
5760
if python:

src/_incydr_sdk/core/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from _incydr_sdk.users.client import UsersClient
2929
from _incydr_sdk.watchlists.client import WatchlistsClient
3030

31-
_base_user_agent = user_agent("incydr", __version__)
31+
_base_user_agent = user_agent("incydrSDK", __version__)
3232

3333

3434
class Client:
@@ -75,8 +75,8 @@ def __init__(
7575

7676
self._session = BaseUrlSession(base_url=self._settings.url)
7777
self._session.headers["User-Agent"] = (
78-
self._settings.user_agent_prefix or "" + _base_user_agent
79-
)
78+
self._settings.user_agent_prefix or ""
79+
) + _base_user_agent
8080
self._session.auth = APIClientAuth(
8181
session=self._session,
8282
api_client_id=self._settings.api_client_id,

tests/test_cli_core.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
from typing import Optional
2+
3+
import pytest
4+
15
from _incydr_cli.main import incydr
26

37

@@ -9,3 +13,27 @@ def test_cli_auth_missing_error_prints_missing_vars(runner, monkeypatch):
913
assert "INCYDR_API_CLIENT_SECRET" in result.output
1014
assert "INCYDR_URL" not in result.output
1115
assert "INCYDR_API_CLIENT_ID" not in result.output
16+
17+
18+
@pytest.mark.disable_autouse
19+
def test_cli_user_agent(runner, httpserver_auth):
20+
def starts_with_matcher(
21+
header_name: str, actual: Optional[str], expected: str
22+
) -> bool:
23+
if actual is None:
24+
return False
25+
26+
return actual.startswith(expected)
27+
28+
httpserver_auth.expect_ordered_request(
29+
"/v1/users",
30+
method="GET",
31+
headers={"User-Agent": "incydrCLI"},
32+
header_value_matcher=starts_with_matcher,
33+
).respond_with_json({"users": [], "totalCount": 0})
34+
result = runner.invoke(
35+
incydr, ["users", "list", "--log-stderr", "--log-level", "DEBUG"]
36+
)
37+
httpserver_auth.check()
38+
assert result.exit_code == 0
39+
assert "No results found" in result.output

tests/test_core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,8 @@ class Test(Model):
117117
err.value
118118
)
119119
assert "value is not a valid integer" in str(err.value)
120+
121+
122+
def test_user_agent(httpserver_auth: HTTPServer):
123+
c = Client()
124+
assert c._session.headers["User-Agent"].startswith("incydrSDK")

0 commit comments

Comments
 (0)