Skip to content

Commit 8e6c7d0

Browse files
committed
feat: Add --version CLI option and setting User-Agent HTTP header
1 parent 87d4573 commit 8e6c7d0

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

onekey_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from .client import APP_NAME as APP_NAME
2+
from .client import APP_VERSION as APP_VERSION
13
from .client import Client as Client
24
from .models import FirmwareMetadata as FirmwareMetadata
35
from .models import Tenant as Tenant

onekey_client/cli/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import click
44
import httpx
55

6-
from onekey_client import Client
6+
from onekey_client import APP_NAME, APP_VERSION, Client
77

88
from .ci import ci_result
99
from .firmware_upload import upload_firmware
1010
from .misc import get_tenant_token, list_tenants
1111

1212

1313
@click.group()
14+
@click.version_option(version=APP_VERSION, prog_name=APP_NAME)
1415
@click.option(
1516
"--api-url",
1617
default="https://app.eu.onekey.com/api",

onekey_client/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import secrets
44
import time
55
from importlib import resources
6+
from importlib.metadata import version
67
from pathlib import Path
78

89
import httpx
@@ -17,6 +18,8 @@
1718

1819
CLIENT_ID = "ONEKEY Python SDK"
1920
TOKEN_NAMESPACE = "https://www.onekey.com/" # noqa: S105 (hardcoded credential)
21+
APP_NAME="onekey_client"
22+
APP_VERSION=version(APP_NAME)
2023

2124

2225
def _login_required(func):
@@ -63,17 +66,18 @@ def _setup_httpx_client(
6366
ca_bundle: Path | None = None,
6467
disable_tls_verify: bool | None = False,
6568
):
69+
headers = {"User-Agent": f"{APP_NAME}/{APP_VERSION}"}
6670
if disable_tls_verify:
67-
return httpx.Client(base_url=api_url, verify=False) # noqa: S501 (TLS certificate validation disabled)
71+
return httpx.Client(base_url=api_url, headers=headers, verify=False) # noqa: S501 (TLS certificate validation disabled)
6872

6973
if ca_bundle is not None:
7074
ca = ca_bundle.expanduser()
7175
if not ca.exists():
7276
raise errors.InvalidCABundle
7377

74-
return httpx.Client(base_url=api_url, verify=str(ca))
78+
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
7579
with resources.path(keys, "ca.pem") as ca:
76-
return httpx.Client(base_url=api_url, verify=str(ca))
80+
return httpx.Client(base_url=api_url, headers=headers, verify=str(ca))
7781

7882
def _load_key(self, key_name: str, path: Path | None = None):
7983
if path is not None:

0 commit comments

Comments
 (0)