Skip to content

Commit e51f50a

Browse files
authored
feat: report runtime properties (#182)
1 parent 72c05cb commit e51f50a

7 files changed

Lines changed: 79 additions & 2 deletions

File tree

codegen/pkg/builder/builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ func (b *Builder) Build() error {
100100
return err
101101
}
102102

103+
if err := b.writeAPIVersionFile(path.Join(b.cfg.Out, "_api_version.py")); err != nil {
104+
return err
105+
}
106+
103107
took := time.Since(b.start)
104108
slog.Info("sdk generated", slog.Duration("took", took))
105109

codegen/pkg/builder/out.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ func (b *Builder) writeClientFile(fname string, tags []string) error {
272272
return nil
273273
}
274274

275+
func (b *Builder) writeAPIVersionFile(fname string) error {
276+
f, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(0o755))
277+
if err != nil {
278+
return fmt.Errorf("create %q: %w", fname, err)
279+
}
280+
defer func() {
281+
_ = f.Close()
282+
}()
283+
284+
if err := b.templates.ExecuteTemplate(f, "api_version.py.tmpl", map[string]any{
285+
"Version": b.spec.Info.Version,
286+
}); err != nil {
287+
return fmt.Errorf("generate api version: %w", err)
288+
}
289+
290+
return nil
291+
}
292+
275293
func openGeneratedFile(filename string) (*os.File, error) {
276294
cwd, err := os.Getwd()
277295
if err != nil {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Code generated by `py-sdk-gen`. DO NOT EDIT.
2+
__api_version__ = "{{ .Version }}"

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Code generated by `py-sdk-gen`. DO NOT EDIT.
2+
__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)