Skip to content

Commit

Permalink
new: Add API spec version to User-Agent; add User-Agent to metadata p…
Browse files Browse the repository at this point in the history
…lugin requests (#583)
  • Loading branch information
lgarber-akamai authored Mar 4, 2024
1 parent cef8240 commit 8b2e3c1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
6 changes: 1 addition & 5 deletions linodecli/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json
import sys
import time
from sys import version_info
from typing import Any, Iterable, List, Optional

import requests
Expand Down Expand Up @@ -69,10 +68,7 @@ def do_request(
headers = {
"Authorization": f"Bearer {ctx.config.get_token()}",
"Content-Type": "application/json",
"User-Agent": (
f"linode-cli:{ctx.version} "
f"python/{version_info[0]}.{version_info[1]}.{version_info[2]}"
),
"User-Agent": ctx.user_agent,
}

parsed_args = operation.parse_args(args)
Expand Down
11 changes: 11 additions & 0 deletions linodecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,14 @@ def find_operation(self, command, action):

# Fail if no matching alias was found
raise ValueError(f"No action {action} for command {command}")

@property
def user_agent(self) -> str:
"""
Returns the User-Agent to use when making API requests.
"""
return (
f"linode-cli/{self.version} "
f"linode-api-docs/{self.spec_version} "
f"python/{version_info[0]}.{version_info[1]}.{version_info[2]}"
)
4 changes: 2 additions & 2 deletions linodecli/plugins/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_metadata_parser():
return parser


def call(args, _):
def call(args, context):
"""
The entrypoint for this plugin
"""
Expand All @@ -209,7 +209,7 @@ def call(args, _):
# make a client, but only if we weren't printing help and endpoint is valid
if "--help" not in args:
try:
client = MetadataClient()
client = MetadataClient(user_agent=context.client.user_agent)
except ConnectTimeout as exc:
raise ConnectionError(
"Can't access Metadata service. Please verify that you are inside a Linode."
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _get_parsed_spec(filename):

@pytest.fixture
def mock_cli(
version="DEVELOPMENT",
version="0.0.0",
url="http://localhost",
defaults=True,
):
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def validate_http_request(url, headers=None, data=None, **kwargs):
]
}
)
assert headers["User-Agent"] == mock_cli.user_agent
assert "Authorization" in headers
assert data is None

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def test_find_operation(
mock_cli.find_operation("foo", "cool")
mock_cli.find_operation("cool", "cool")

def test_user_agent(self, mock_cli: CLI):
assert re.compile(
r"linode-cli/[0-9]+\.[0-9]+\.[0-9]+ linode-api-docs/[0-9]+\.[0-9]+\.[0-9]+ python/[0-9]+\.[0-9]+\.[0-9]+"
).match(mock_cli.user_agent)


def test_get_all_pages(
mock_cli: CLI, list_operation: OpenAPIOperation, monkeypatch: MonkeyPatch
Expand Down

0 comments on commit 8b2e3c1

Please sign in to comment.