Skip to content

Commit 27fcf12

Browse files
authored
feat: set User-Agent (#95)
### what Sets the User-Agent header. ### why Noticed auth events with `python-requests/2.32.5`, thought it didn't look great. ### testing Authenticated, observed `"userAgent": "stacklet.client.platform/2025.11.0"` logged. ### docs Don't think so.
1 parent 0f70a9a commit 27fcf12

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

stacklet/client/platform/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .config import DEFAULT_CONFIG_FILE
1212
from .context import StackletContext
1313
from .executor import StackletGraphqlExecutor
14-
from .utils import _PAGINATION_OPTIONS
14+
from .utils import PAGINATION_OPTIONS
1515

1616

1717
def platform_client(pager=False, expr=False):
@@ -78,14 +78,14 @@ def _method(executor, function_name, snippet, pager, expr):
7878
doc.append("")
7979
if snippet.pagination:
8080
doc.append("pagination: ")
81-
for p in _PAGINATION_OPTIONS:
81+
for p in PAGINATION_OPTIONS:
8282
doc.append(f" - {p}")
8383
doc.append("")
8484
doc = "\n".join(doc)
8585

8686
defaults = {}
8787
if snippet.pagination:
88-
for k, v in _PAGINATION_OPTIONS.items():
88+
for k, v in PAGINATION_OPTIONS.items():
8989
defaults[k] = v["default"]
9090
for k, v in snippet.optional.items():
9191
defaults[k] = None

stacklet/client/platform/commands/cube.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from ..context import StackletContext
1616
from ..exceptions import MissingToken
17+
from ..utils import get_user_agent
1718

1819

1920
@click.group()
@@ -71,6 +72,7 @@ def _request(context: StackletContext, method: str, path: str, payload: Any = No
7172
headers={
7273
"Authorization": f"Bearer {token}",
7374
"Content-Type": "application/json",
75+
"User-Agent": get_user_agent(),
7476
},
7577
json=payload,
7678
)

stacklet/client/platform/executor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .context import StackletContext
1010
from .exceptions import MissingToken
1111
from .registry import PluginRegistry
12-
from .utils import _PAGINATION_OPTIONS, wrap_command
12+
from .utils import PAGINATION_OPTIONS, get_user_agent, wrap_command
1313

1414

1515
class StackletGraphqlExecutor:
@@ -31,6 +31,7 @@ def __init__(self, context: StackletContext):
3131
self.session.headers.update(
3232
{
3333
"Authorization": f"Bearer {token}",
34+
"User-Agent": get_user_agent(),
3435
}
3536
)
3637

@@ -54,7 +55,7 @@ def wrapper(func):
5455
func = wrap_command(func, snippet.required, required=True)
5556
func = wrap_command(func, snippet.optional)
5657
if snippet.pagination:
57-
func = wrap_command(func, _PAGINATION_OPTIONS)
58+
func = wrap_command(func, PAGINATION_OPTIONS)
5859
return func
5960

6061
return wrapper

stacklet/client/platform/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright Stacklet, Inc.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import importlib.metadata
45
import logging
56
from pathlib import Path
67

@@ -16,7 +17,7 @@ def expand_user_path(ctx, param, value):
1617
return value.expanduser()
1718

1819

19-
_PAGINATION_OPTIONS = {
20+
PAGINATION_OPTIONS = {
2021
"first": {
2122
"help": "For use with pagination. Return the first n results.",
2223
"default": 20,
@@ -78,3 +79,9 @@ def setup_logging(level):
7879
root_handler = logging.getLogger()
7980
if level:
8081
root_handler.setLevel(level=get_log_level(level))
82+
83+
84+
def get_user_agent():
85+
package = "stacklet.client.platform"
86+
version = importlib.metadata.version(package)
87+
return f"{package}/{version}"

0 commit comments

Comments
 (0)