Skip to content

Commit

Permalink
feat: Add version file and include in HTTP request user-agent (#21)
Browse files Browse the repository at this point in the history
- Created a version file to store the current SDK version
- Enhanced HTTP requests to include SDK version, language, and system
version in the user-agent header
- Improved traceability and debugging with detailed user-agent
information in requests
  • Loading branch information
chyroc authored Sep 27, 2024
1 parent 8dcc15f commit d8ef6ee
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.idea/
.venv/
.DS_Store
__pycache__/
dist/
debug.*
.env
.env_private
scripts/
scripts/
3 changes: 3 additions & 0 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
)
from .workspaces import WorkspaceRoleType, WorkspaceType, Workspace

from .version import VERSION

__all__ = [
"VERSION",
# auth
"OAuthToken",
"DeviceAuthCode",
Expand Down
2 changes: 2 additions & 0 deletions cozepy/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from httpx import Response
from typing_extensions import get_args, get_origin # compatibility with python 3.7

from cozepy.version import user_agent
from cozepy.exception import CozeAPIError

if TYPE_CHECKING:
Expand Down Expand Up @@ -45,6 +46,7 @@ def request(
"""
if headers is None:
headers = {}
headers["User-Agent"] = user_agent()
if self._auth:
self._auth.authentication(headers)
r = httpx.request(
Expand Down
31 changes: 31 additions & 0 deletions cozepy/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import platform
import sys
from functools import lru_cache

VERSION = "0.1.1"


@lru_cache(maxsize=1)
def user_agent():
print("调用了")
python_version = ".".join(map(str, sys.version_info[:2]))

os_name = platform.system().lower()
if os_name == "darwin":
os_name = "macos"

if os_name == "macos":
os_version = platform.mac_ver()[0]
elif os_name == "windows":
os_version = platform.win32_ver()[0]
elif os_name == "linux":
try:
import distro

os_version = distro.version(pretty=False, best=True)
except ImportError:
os_version = platform.release()
else:
os_version = platform.release()

return f"cozepy/{VERSION} python/{python_version} {os_name}/{os_version}".lower()
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pydantic = "^2.5.0"
authlib = "^1.2.0"
httpx = "^0.24.0"
typing-extensions = "^4.3.0"
distro = "^1.9.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0.0"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from cozepy import VERSION
from cozepy.version import user_agent


def test_user_agent():
res = user_agent()
assert f"cozepy/{VERSION}" in res
assert "python" in res

0 comments on commit d8ef6ee

Please sign in to comment.