Skip to content

Commit 400ca8f

Browse files
committed
feat: report runtime properties
Propagate more information about the SDK's runtime including the API version, package version, etc. to get more observability into the SDKs usage. Also removes the generation of the client/client.go file as it doesn't depend on the OpenAPI specs and can be maintained manually.
1 parent 72c05cb commit 400ca8f

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

codegen/templates/client.py.tmpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os
33
import httpx
44
import typing
55

6-
from ._service import Resource, AsyncResource
6+
from ._service import Resource, AsyncResource, runtime_headers
77
{{- range .Resources }}
88
from .{{ .Package }} import {{ .Name }}Resource, Async{{ .Name }}Resource
99
{{- end }}
@@ -25,6 +25,7 @@ class Sumup(Resource):
2525
headers={
2626
"User-Agent": f"sumup-py/{self.version()}",
2727
"Authorization": f"Bearer {self.api_key}",
28+
**runtime_headers(),
2829
},
2930
))
3031

@@ -51,6 +52,7 @@ class AsyncSumup(AsyncResource):
5152
headers={
5253
"User-Agent": f"sumup-py/{self.version()}",
5354
"Authorization": f"Bearer {self.api_key}",
55+
**runtime_headers(),
5456
},
5557
))
5658

sumup/_api_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__api_version__ = "1.0.0"

sumup/_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import httpx
44
import typing
55

6-
from ._service import Resource, AsyncResource
6+
from ._service import Resource, AsyncResource, runtime_headers
77
from .checkouts import CheckoutsResource, AsyncCheckoutsResource
88
from .customers import CustomersResource, AsyncCustomersResource
99
from .members import MembersResource, AsyncMembersResource
@@ -35,6 +35,7 @@ def __init__(
3535
headers={
3636
"User-Agent": f"sumup-py/{self.version()}",
3737
"Authorization": f"Bearer {self.api_key}",
38+
**runtime_headers(),
3839
},
3940
)
4041
)
@@ -127,6 +128,7 @@ def __init__(
127128
headers={
128129
"User-Agent": f"sumup-py/{self.version()}",
129130
"Authorization": f"Bearer {self.api_key}",
131+
**runtime_headers(),
130132
},
131133
)
132134
)

sumup/_service.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import httpx
2+
import platform
3+
import sys
4+
from functools import lru_cache
5+
6+
from ._api_version import __api_version__
27
from ._version import __version__
38

49
HeaderTypes = dict[str, str]
@@ -10,6 +15,48 @@ def version():
1015
return f"v{__version__}"
1116

1217

18+
def runtime_headers() -> dict[str, str]:
19+
return dict(_runtime_headers())
20+
21+
22+
@lru_cache(maxsize=1)
23+
def _runtime_headers() -> tuple[tuple[str, str], ...]:
24+
arch_raw = platform.machine()
25+
arch = arch_raw.lower() if arch_raw else ""
26+
arch_map = {
27+
"x86_64": "x86_64",
28+
"x64": "x86_64",
29+
"amd64": "x86_64",
30+
"x86": "x86",
31+
"i386": "x86",
32+
"i686": "x86",
33+
"ia32": "x86",
34+
"x32": "x86",
35+
"aarch64": "arm64",
36+
"arm64": "arm64",
37+
"arm": "arm",
38+
}
39+
arch = arch_map.get(arch, "unknown")
40+
41+
os_name = sys.platform
42+
os_map = {
43+
"win32": "windows",
44+
"linux": "linux",
45+
"darwin": "darwin",
46+
}
47+
os_name = os_map.get(os_name, os_name)
48+
49+
return (
50+
("X-Sumup-Api-Version", __api_version__),
51+
("X-Sumup-Lang", "python"),
52+
("X-Sumup-Package-Version", __version__),
53+
("X-Sumup-Os", os_name),
54+
("X-Sumup-Arch", arch),
55+
("X-Sumup-Runtime", "python"),
56+
("X-Sumup-Runtime-Version", platform.python_version()),
57+
)
58+
59+
1360
class Resource(BaseResource):
1461
_client: httpx.Client
1562

0 commit comments

Comments
 (0)