frequenz-client-base 0.9.0__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {frequenz_client_base-0.9.0/src/frequenz_client_base.egg-info → frequenz_client_base-0.10.0}/PKG-INFO +26 -25
  2. frequenz_client_base-0.10.0/RELEASE_NOTES.md +20 -0
  3. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/pyproject.toml +27 -26
  4. frequenz_client_base-0.10.0/src/frequenz/client/base/authentication.py +116 -0
  5. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/channel.py +46 -10
  6. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/client.py +4 -1
  7. frequenz_client_base-0.10.0/src/frequenz/client/base/signing.py +158 -0
  8. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/streaming.py +7 -0
  9. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0/src/frequenz_client_base.egg-info}/PKG-INFO +26 -25
  10. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz_client_base.egg-info/SOURCES.txt +2 -0
  11. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz_client_base.egg-info/requires.txt +23 -23
  12. frequenz_client_base-0.9.0/RELEASE_NOTES.md +0 -6
  13. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/LICENSE +0 -0
  14. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/MANIFEST.in +0 -0
  15. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/README.md +0 -0
  16. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/setup.cfg +0 -0
  17. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/__init__.py +0 -0
  18. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/conftest.py +0 -0
  19. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/conversion.py +0 -0
  20. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/exception.py +0 -0
  21. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/py.typed +0 -0
  22. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz/client/base/retry.py +0 -0
  23. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz_client_base.egg-info/dependency_links.txt +0 -0
  24. {frequenz_client_base-0.9.0 → frequenz_client_base-0.10.0}/src/frequenz_client_base.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: frequenz-client-base
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Utilities for writing Frequenz API clients
5
5
  Author-email: Frequenz Energy-as-a-Service GmbH <floss@frequenz.com>
6
6
  License: MIT
@@ -21,53 +21,54 @@ Requires-Python: <4,>=3.11
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
23
  Requires-Dist: frequenz-channels<2,>=1.6.1
24
- Requires-Dist: grpcio<2,>=1.54.2
25
- Requires-Dist: protobuf<6,>=4.21.6
26
- Requires-Dist: typing-extensions<5,>=4.5.0
24
+ Requires-Dist: grpcio<2,>=1.59
25
+ Requires-Dist: protobuf<7,>=5.29.2
26
+ Requires-Dist: typing-extensions<5,>=4.6.0
27
27
  Provides-Extra: dev-flake8
28
- Requires-Dist: flake8==7.1.1; extra == "dev-flake8"
28
+ Requires-Dist: flake8==7.2.0; extra == "dev-flake8"
29
29
  Requires-Dist: flake8-docstrings==1.7.0; extra == "dev-flake8"
30
30
  Requires-Dist: flake8-pyproject==1.2.3; extra == "dev-flake8"
31
- Requires-Dist: pydoclint==0.6.0; extra == "dev-flake8"
31
+ Requires-Dist: pydoclint==0.6.6; extra == "dev-flake8"
32
32
  Requires-Dist: pydocstyle==6.3.0; extra == "dev-flake8"
33
33
  Requires-Dist: frequenz-client-base; extra == "dev-flake8"
34
34
  Provides-Extra: dev-formatting
35
35
  Requires-Dist: black==25.1.0; extra == "dev-formatting"
36
- Requires-Dist: isort==6.0.0; extra == "dev-formatting"
36
+ Requires-Dist: isort==6.0.1; extra == "dev-formatting"
37
37
  Provides-Extra: dev-mkdocs
38
38
  Requires-Dist: black==25.1.0; extra == "dev-mkdocs"
39
- Requires-Dist: Markdown==3.7; extra == "dev-mkdocs"
39
+ Requires-Dist: Markdown==3.8; extra == "dev-mkdocs"
40
40
  Requires-Dist: mike==2.1.3; extra == "dev-mkdocs"
41
41
  Requires-Dist: mkdocs-gen-files==0.5.0; extra == "dev-mkdocs"
42
- Requires-Dist: mkdocs-literate-nav==0.6.1; extra == "dev-mkdocs"
42
+ Requires-Dist: mkdocs-literate-nav==0.6.2; extra == "dev-mkdocs"
43
43
  Requires-Dist: mkdocs-macros-plugin==1.3.7; extra == "dev-mkdocs"
44
- Requires-Dist: mkdocs-material==9.6.1; extra == "dev-mkdocs"
45
- Requires-Dist: mkdocstrings[python]==0.27.0; extra == "dev-mkdocs"
46
- Requires-Dist: mkdocstrings-python==1.13.0; extra == "dev-mkdocs"
47
- Requires-Dist: frequenz-repo-config[lib]==0.11.0; extra == "dev-mkdocs"
44
+ Requires-Dist: mkdocs-material==9.6.12; extra == "dev-mkdocs"
45
+ Requires-Dist: mkdocstrings[python]==0.29.1; extra == "dev-mkdocs"
46
+ Requires-Dist: mkdocstrings-python==1.16.10; extra == "dev-mkdocs"
47
+ Requires-Dist: frequenz-repo-config[lib]==0.13.3; extra == "dev-mkdocs"
48
48
  Requires-Dist: frequenz-client-base; extra == "dev-mkdocs"
49
49
  Provides-Extra: dev-mypy
50
- Requires-Dist: mypy==1.14.1; extra == "dev-mypy"
51
- Requires-Dist: types-Markdown==3.7.0.20241204; extra == "dev-mypy"
52
- Requires-Dist: types-protobuf==5.29.1.20241207; extra == "dev-mypy"
53
- Requires-Dist: grpc-stubs==1.53.0.5; extra == "dev-mypy"
50
+ Requires-Dist: mypy==1.15.0; extra == "dev-mypy"
51
+ Requires-Dist: types-Markdown==3.8.0.20250415; extra == "dev-mypy"
52
+ Requires-Dist: types-protobuf==5.29.1.20250403; extra == "dev-mypy"
53
+ Requires-Dist: grpc-stubs==1.53.0.6; extra == "dev-mypy"
54
54
  Requires-Dist: frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]; extra == "dev-mypy"
55
55
  Provides-Extra: dev-noxfile
56
- Requires-Dist: nox==2024.10.9; extra == "dev-noxfile"
57
- Requires-Dist: frequenz-repo-config[lib]==0.11.0; extra == "dev-noxfile"
56
+ Requires-Dist: nox==2025.5.1; extra == "dev-noxfile"
57
+ Requires-Dist: frequenz-repo-config[lib]==0.13.3; extra == "dev-noxfile"
58
58
  Provides-Extra: dev-pylint
59
- Requires-Dist: pylint==3.3.4; extra == "dev-pylint"
59
+ Requires-Dist: pylint==3.3.6; extra == "dev-pylint"
60
60
  Requires-Dist: frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]; extra == "dev-pylint"
61
61
  Provides-Extra: dev-pytest
62
- Requires-Dist: pytest==8.3.4; extra == "dev-pytest"
63
- Requires-Dist: frequenz-repo-config[extra-lint-examples]==0.11.0; extra == "dev-pytest"
62
+ Requires-Dist: pytest==8.3.5; extra == "dev-pytest"
63
+ Requires-Dist: frequenz-repo-config[extra-lint-examples]==0.13.3; extra == "dev-pytest"
64
64
  Requires-Dist: pytest-mock==3.14.0; extra == "dev-pytest"
65
- Requires-Dist: pytest-asyncio==0.25.3; extra == "dev-pytest"
65
+ Requires-Dist: pytest-asyncio==0.26.0; extra == "dev-pytest"
66
66
  Requires-Dist: async-solipsism==0.7; extra == "dev-pytest"
67
- Requires-Dist: hypothesis==6.124.9; extra == "dev-pytest"
67
+ Requires-Dist: hypothesis==6.131.9; extra == "dev-pytest"
68
68
  Requires-Dist: frequenz-client-base; extra == "dev-pytest"
69
69
  Provides-Extra: dev
70
70
  Requires-Dist: frequenz-client-base[dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]; extra == "dev"
71
+ Dynamic: license-file
71
72
 
72
73
  # Frequenz Client Base Library
73
74
 
@@ -0,0 +1,20 @@
1
+ # Frequenz Client Base Library Release Notes
2
+
3
+ ## Features
4
+
5
+ * Added support for HMAC signing of `UnaryUnary` client messages
6
+ * Added support for HMAC signing of `UnaryStream` client messages
7
+
8
+ ## Upgrading
9
+
10
+ * Updated `protobuf` dependency range: changed from `>=4.21.6, <6` to `>=5.29.2, <7`
11
+ * The minimum dependency for `typing-extensions` is now `4.6.0` to be compatible with Python 3.12
12
+ * The minimum dependency for `grpcio` is now `1.59` to be compatible with Python 3.12
13
+
14
+ ## Bug Fixes
15
+
16
+ * Fixed keys of signature to match what fuse-rs expects
17
+ * `GrpcStreamBroadcaster` will now correctly try to restart on unexpected errors.
18
+
19
+ Before if an unexpected exception was raised by the stream method, the
20
+ internal task would silently finish and never start again.
@@ -3,9 +3,9 @@
3
3
 
4
4
  [build-system]
5
5
  requires = [
6
- "setuptools == 75.8.0",
7
- "setuptools_scm[toml] == 8.1.0",
8
- "frequenz-repo-config[lib] == 0.11.0",
6
+ "setuptools == 80.3.1",
7
+ "setuptools_scm[toml] == 8.3.1",
8
+ "frequenz-repo-config[lib] == 0.13.3",
9
9
  ]
10
10
  build-backend = "setuptools.build_meta"
11
11
 
@@ -27,9 +27,9 @@ classifiers = [
27
27
  requires-python = ">= 3.11, < 4"
28
28
  dependencies = [
29
29
  "frequenz-channels >= 1.6.1, < 2",
30
- "grpcio >= 1.54.2, < 2",
31
- "protobuf >= 4.21.6, < 6",
32
- "typing-extensions >= 4.5.0, < 5",
30
+ "grpcio >= 1.59, < 2",
31
+ "protobuf >= 5.29.2, < 7",
32
+ "typing-extensions >= 4.6.0, < 5",
33
33
  ]
34
34
  dynamic = ["version"]
35
35
 
@@ -39,48 +39,48 @@ email = "floss@frequenz.com"
39
39
 
40
40
  [project.optional-dependencies]
41
41
  dev-flake8 = [
42
- "flake8 == 7.1.1",
42
+ "flake8 == 7.2.0",
43
43
  "flake8-docstrings == 1.7.0",
44
- "flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
45
- "pydoclint == 0.6.0",
44
+ "flake8-pyproject == 1.2.3", # For reading the flake8 config from pyproject.toml
45
+ "pydoclint == 0.6.6",
46
46
  "pydocstyle == 6.3.0",
47
47
  "frequenz-client-base",
48
48
  ]
49
- dev-formatting = ["black == 25.1.0", "isort == 6.0.0"]
49
+ dev-formatting = ["black == 25.1.0", "isort == 6.0.1"]
50
50
  dev-mkdocs = [
51
51
  "black == 25.1.0",
52
- "Markdown==3.7",
52
+ "Markdown==3.8",
53
53
  "mike == 2.1.3",
54
54
  "mkdocs-gen-files == 0.5.0",
55
- "mkdocs-literate-nav == 0.6.1",
55
+ "mkdocs-literate-nav == 0.6.2",
56
56
  "mkdocs-macros-plugin == 1.3.7",
57
- "mkdocs-material == 9.6.1",
58
- "mkdocstrings[python] == 0.27.0",
59
- "mkdocstrings-python == 1.13.0",
60
- "frequenz-repo-config[lib] == 0.11.0",
57
+ "mkdocs-material == 9.6.12",
58
+ "mkdocstrings[python] == 0.29.1",
59
+ "mkdocstrings-python == 1.16.10",
60
+ "frequenz-repo-config[lib] == 0.13.3",
61
61
  "frequenz-client-base",
62
62
  ]
63
63
  dev-mypy = [
64
- "mypy == 1.14.1",
65
- "types-Markdown == 3.7.0.20241204",
66
- "types-protobuf == 5.29.1.20241207",
67
- "grpc-stubs == 1.53.0.5", # This dependency introduces breaking changes in patch releases
64
+ "mypy == 1.15.0",
65
+ "types-Markdown == 3.8.0.20250415",
66
+ "types-protobuf == 5.29.1.20250403",
67
+ "grpc-stubs == 1.53.0.6", # This dependency introduces breaking changes in patch releases
68
68
  # For checking the noxfile, docs/ script, and tests
69
69
  "frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
70
70
  ]
71
- dev-noxfile = ["nox == 2024.10.9", "frequenz-repo-config[lib] == 0.11.0"]
71
+ dev-noxfile = ["nox == 2025.5.1", "frequenz-repo-config[lib] == 0.13.3"]
72
72
  dev-pylint = [
73
- "pylint == 3.3.4",
73
+ "pylint == 3.3.6",
74
74
  # For checking the noxfile, docs/ script, and tests
75
75
  "frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]",
76
76
  ]
77
77
  dev-pytest = [
78
- "pytest == 8.3.4",
79
- "frequenz-repo-config[extra-lint-examples] == 0.11.0",
78
+ "pytest == 8.3.5",
79
+ "frequenz-repo-config[extra-lint-examples] == 0.13.3",
80
80
  "pytest-mock == 3.14.0",
81
- "pytest-asyncio == 0.25.3",
81
+ "pytest-asyncio == 0.26.0",
82
82
  "async-solipsism == 0.7",
83
- "hypothesis == 6.124.9",
83
+ "hypothesis == 6.131.9",
84
84
  "frequenz-client-base",
85
85
  ]
86
86
  dev = [
@@ -155,6 +155,7 @@ disable = [
155
155
  ]
156
156
 
157
157
  [tool.pytest.ini_options]
158
+ addopts = "-W=all -Werror -Wdefault::DeprecationWarning -Wdefault::PendingDeprecationWarning -vv"
158
159
  testpaths = ["tests", "src"]
159
160
  asyncio_mode = "auto"
160
161
  asyncio_default_fixture_loop_scope = "function"
@@ -0,0 +1,116 @@
1
+ # License: MIT
2
+ # Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
+
4
+ """An Interceptor that adds the API key to a gRPC call."""
5
+
6
+ import dataclasses
7
+ from typing import AsyncIterable, Callable
8
+
9
+ from grpc.aio import (
10
+ ClientCallDetails,
11
+ Metadata,
12
+ UnaryStreamCall,
13
+ UnaryStreamClientInterceptor,
14
+ UnaryUnaryCall,
15
+ UnaryUnaryClientInterceptor,
16
+ )
17
+
18
+
19
+ def _add_auth_header(
20
+ key: str,
21
+ client_call_details: ClientCallDetails,
22
+ ) -> None:
23
+ """Add the API key as a metadata field to the call.
24
+
25
+ The API key is used by the later sign interceptor to calculate the HMAC.
26
+ In addition it is used as a first layer of authentication by the server.
27
+
28
+ Args:
29
+ key: The API key to use for the service.
30
+ client_call_details: The call details.
31
+ """
32
+ if client_call_details.metadata is None:
33
+ client_call_details.metadata = Metadata()
34
+
35
+ client_call_details.metadata["key"] = key
36
+
37
+
38
+ @dataclasses.dataclass(frozen=True)
39
+ class AuthenticationOptions:
40
+ """Options for authenticating to the endpoint."""
41
+
42
+ api_key: str
43
+ """The API key to authenticate with."""
44
+
45
+
46
+ # There is an issue in gRPC which means the type can not be specified correctly here.
47
+ class AuthenticationInterceptorUnaryUnary(UnaryUnaryClientInterceptor): # type: ignore[type-arg]
48
+ """An Interceptor that adds HMAC authentication of the metadata fields to a gRPC call."""
49
+
50
+ def __init__(self, options: AuthenticationOptions):
51
+ """Create an instance of the interceptor.
52
+
53
+ Args:
54
+ options: The options for authenticating to the endpoint.
55
+ """
56
+ self._key = options.api_key
57
+
58
+ async def intercept_unary_unary(
59
+ self,
60
+ continuation: Callable[
61
+ [ClientCallDetails, object], UnaryUnaryCall[object, object]
62
+ ],
63
+ client_call_details: ClientCallDetails,
64
+ request: object,
65
+ ) -> object:
66
+ """Intercept the call to add HMAC authentication to the metadata fields.
67
+
68
+ This is a known method from the base class that is overridden.
69
+
70
+ Args:
71
+ continuation: The next interceptor in the chain.
72
+ client_call_details: The call details.
73
+ request: The request object.
74
+
75
+ Returns:
76
+ The response object (this implementation does not modify the response).
77
+ """
78
+ _add_auth_header(self._key, client_call_details)
79
+ return await continuation(client_call_details, request)
80
+
81
+
82
+ # There is an issue in gRPC which means the type can not be specified correctly here.
83
+ class AuthenticationInterceptorUnaryStream(UnaryStreamClientInterceptor): # type: ignore[type-arg]
84
+ """An Interceptor that adds HMAC authentication of the metadata fields to a gRPC call."""
85
+
86
+ def __init__(self, options: AuthenticationOptions):
87
+ """Create an instance of the interceptor.
88
+
89
+ Args:
90
+ options: The options for authenticating to the endpoint.
91
+ """
92
+ self._key = options.api_key
93
+
94
+ async def intercept_unary_stream(
95
+ self,
96
+ continuation: Callable[
97
+ [ClientCallDetails, object], UnaryStreamCall[object, object]
98
+ ],
99
+ client_call_details: ClientCallDetails,
100
+ request: object,
101
+ ) -> AsyncIterable[object] | UnaryStreamCall[object, object]:
102
+ """Intercept the call to add HMAC authentication to the metadata fields.
103
+
104
+ This is a known method from the base class that is overridden.
105
+
106
+ Args:
107
+ continuation: The next interceptor in the chain.
108
+ client_call_details: The call details.
109
+ request: The request object.
110
+
111
+ Returns:
112
+ The response object (this implementation does not modify the response).
113
+ """
114
+ _add_auth_header(self._key, client_call_details)
115
+
116
+ return await continuation(client_call_details, request) # type: ignore
@@ -10,7 +10,23 @@ from typing import assert_never
10
10
  from urllib.parse import parse_qs, urlparse
11
11
 
12
12
  from grpc import ssl_channel_credentials
13
- from grpc.aio import Channel, insecure_channel, secure_channel
13
+ from grpc.aio import (
14
+ Channel,
15
+ ClientInterceptor,
16
+ insecure_channel,
17
+ secure_channel,
18
+ )
19
+
20
+ from .authentication import (
21
+ AuthenticationInterceptorUnaryStream,
22
+ AuthenticationInterceptorUnaryUnary,
23
+ AuthenticationOptions,
24
+ )
25
+ from .signing import (
26
+ SigningInterceptorUnaryStream,
27
+ SigningInterceptorUnaryUnary,
28
+ SigningOptions,
29
+ )
14
30
 
15
31
 
16
32
  @dataclasses.dataclass(frozen=True)
@@ -69,6 +85,12 @@ class ChannelOptions:
69
85
  keep_alive: KeepAliveOptions = KeepAliveOptions()
70
86
  """HTTP2 keep-alive options for the channel."""
71
87
 
88
+ sign: SigningOptions | None = None
89
+ """Signing options for the channel."""
90
+
91
+ auth: AuthenticationOptions | None = None
92
+ """Authentication options for the channel."""
93
+
72
94
 
73
95
  def parse_grpc_uri(
74
96
  uri: str,
@@ -177,6 +199,19 @@ def parse_grpc_uri(
177
199
  else None
178
200
  )
179
201
 
202
+ interceptors: list[ClientInterceptor] = []
203
+ if defaults.auth is not None:
204
+ interceptors += [
205
+ AuthenticationInterceptorUnaryUnary(options=defaults.auth), # type: ignore [list-item]
206
+ AuthenticationInterceptorUnaryStream(options=defaults.auth), # type: ignore [list-item]
207
+ ]
208
+
209
+ if defaults.sign is not None:
210
+ interceptors += [
211
+ SigningInterceptorUnaryUnary(options=defaults.sign), # type: ignore [list-item]
212
+ SigningInterceptorUnaryStream(options=defaults.sign), # type: ignore [list-item]
213
+ ]
214
+
180
215
  ssl = defaults.ssl.enabled if options.ssl is None else options.ssl
181
216
  if ssl:
182
217
  return secure_channel(
@@ -199,8 +234,9 @@ def parse_grpc_uri(
199
234
  ),
200
235
  ),
201
236
  channel_options,
237
+ interceptors=interceptors,
202
238
  )
203
- return insecure_channel(target, channel_options)
239
+ return insecure_channel(target, channel_options, interceptors=interceptors)
204
240
 
205
241
 
206
242
  def _to_bool(value: str) -> bool:
@@ -252,13 +288,13 @@ def _parse_query_params(uri: str, query_string: str) -> _QueryParams:
252
288
  }
253
289
 
254
290
  if ssl is False:
255
- erros = []
291
+ errors = []
256
292
  for opt_name, opt in ssl_opts.items():
257
293
  if opt is not None:
258
- erros.append(opt_name)
259
- if erros:
294
+ errors.append(opt_name)
295
+ if errors:
260
296
  raise ValueError(
261
- f"Option(s) {', '.join(erros)} found in URI {uri!r}, but SSL is disabled",
297
+ f"Option(s) {', '.join(errors)} found in URI {uri!r}, but SSL is disabled",
262
298
  )
263
299
 
264
300
  keep_alive_option = options.pop("keep_alive", None)
@@ -272,13 +308,13 @@ def _parse_query_params(uri: str, query_string: str) -> _QueryParams:
272
308
  }
273
309
 
274
310
  if keep_alive is False:
275
- erros = []
311
+ errors = []
276
312
  for opt_name, opt in keep_alive_opts.items():
277
313
  if opt is not None:
278
- erros.append(opt_name)
279
- if erros:
314
+ errors.append(opt_name)
315
+ if errors:
280
316
  raise ValueError(
281
- f"Option(s) {', '.join(erros)} found in URI {uri!r}, but keep_alive is disabled",
317
+ f"Option(s) {', '.join(errors)} found in URI {uri!r}, but keep_alive is disabled",
282
318
  )
283
319
 
284
320
  if options:
@@ -8,7 +8,10 @@ import inspect
8
8
  from collections.abc import Awaitable, Callable
9
9
  from typing import Any, Generic, Self, TypeVar, overload
10
10
 
11
- from grpc.aio import AioRpcError, Channel
11
+ from grpc.aio import (
12
+ AioRpcError,
13
+ Channel,
14
+ )
12
15
 
13
16
  from .channel import ChannelOptions, parse_grpc_uri
14
17
  from .exception import ApiClientError, ClientNotConnected
@@ -0,0 +1,158 @@
1
+ # License: MIT
2
+ # Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3
+
4
+ """An Interceptor that adds HMAC signature of the metadata fields to a gRPC call."""
5
+
6
+ import dataclasses
7
+ import hmac
8
+ import logging
9
+ import secrets
10
+ import time
11
+ from base64 import urlsafe_b64encode
12
+ from typing import Any, AsyncIterable, Callable
13
+
14
+ from grpc.aio import (
15
+ ClientCallDetails,
16
+ UnaryStreamCall,
17
+ UnaryStreamClientInterceptor,
18
+ UnaryUnaryCall,
19
+ UnaryUnaryClientInterceptor,
20
+ )
21
+
22
+ _logger = logging.getLogger(__name__)
23
+
24
+
25
+ def _add_hmac(
26
+ secret: bytes, client_call_details: ClientCallDetails, ts: int, nonce: bytes
27
+ ) -> None:
28
+ """Add the HMAC authentication to the metadata fields of the call details.
29
+
30
+ The extra headers are directly added to the client_call details.
31
+
32
+ Args:
33
+ secret: The symmetric secret shared with the service.
34
+ client_call_details: The call details.
35
+ ts: The timestamp to use for the HMAC.
36
+ nonce: The nonce to use for the HMAC.
37
+ """
38
+ if client_call_details.metadata is None:
39
+ _logger.error(
40
+ "No metadata found, cannot extract an api key. Therefore, cannot sign the request."
41
+ )
42
+ return
43
+
44
+ key: Any = client_call_details.metadata.get("key")
45
+ if key is None:
46
+ _logger.error("No key found in metadata, cannot sign the request.")
47
+ return
48
+
49
+ # Make into a base10 integer string and then encode to bytes
50
+ # We can not use a raw bytes timestamp as the underlying network library
51
+ # really hates zero bytes in the metadata values
52
+ ts_bytes = str(ts).encode()
53
+ nonce_bytes = urlsafe_b64encode(nonce)
54
+
55
+ hmac_obj = hmac.new(secret, digestmod="sha256")
56
+ hmac_obj.update(key.encode())
57
+ hmac_obj.update(ts_bytes)
58
+ hmac_obj.update(nonce_bytes)
59
+
60
+ # Once again, gRPC is mistyped
61
+ hmac_obj.update(client_call_details.method.split(b"/")[-1]) # type: ignore[arg-type]
62
+
63
+ client_call_details.metadata["ts"] = ts_bytes
64
+ client_call_details.metadata["nonce"] = nonce_bytes
65
+ # By definition the signature is base64 encoded _without_ the padding, so we strip that
66
+ client_call_details.metadata["sig"] = urlsafe_b64encode(hmac_obj.digest()).strip(
67
+ b"="
68
+ )
69
+
70
+
71
+ @dataclasses.dataclass(frozen=True)
72
+ class SigningOptions:
73
+ """Options for message signing of messages."""
74
+
75
+ secret: str
76
+ """The secret to sign the message with."""
77
+
78
+
79
+ # There is an issue in gRPC which means the type can not be specified correctly here.
80
+ class SigningInterceptorUnaryUnary(UnaryUnaryClientInterceptor): # type: ignore[type-arg]
81
+ """An Interceptor that adds HMAC authentication of the metadata fields to a gRPC call."""
82
+
83
+ def __init__(self, options: SigningOptions):
84
+ """Create an instance of the interceptor.
85
+
86
+ Args:
87
+ options: The options for signing the message.
88
+ """
89
+ self._secret = options.secret.encode()
90
+
91
+ async def intercept_unary_unary(
92
+ self,
93
+ continuation: Callable[
94
+ [ClientCallDetails, object], UnaryUnaryCall[object, object]
95
+ ],
96
+ client_call_details: ClientCallDetails,
97
+ request: object,
98
+ ) -> object:
99
+ """Intercept the call to add HMAC authentication to the metadata fields.
100
+
101
+ This is a known method from the base class that is overridden.
102
+
103
+ Args:
104
+ continuation: The next interceptor in the chain.
105
+ client_call_details: The call details.
106
+ request: The request object.
107
+
108
+ Returns:
109
+ The response object (this implementation does not modify the response).
110
+ """
111
+ _add_hmac(
112
+ self._secret,
113
+ client_call_details,
114
+ int(time.time()),
115
+ secrets.token_bytes(16),
116
+ )
117
+ return await continuation(client_call_details, request)
118
+
119
+
120
+ # There is an issue in gRPC which means the type can not be specified correctly here.
121
+ class SigningInterceptorUnaryStream(UnaryStreamClientInterceptor): # type: ignore[type-arg]
122
+ """An Interceptor that adds HMAC authentication of the metadata fields to a gRPC call."""
123
+
124
+ def __init__(self, options: SigningOptions):
125
+ """Create an instance of the interceptor.
126
+
127
+ Args:
128
+ options: The options for signing the message.
129
+ """
130
+ self._secret = options.secret.encode()
131
+
132
+ async def intercept_unary_stream(
133
+ self,
134
+ continuation: Callable[
135
+ [ClientCallDetails, Any], UnaryStreamCall[object, object]
136
+ ],
137
+ client_call_details: ClientCallDetails,
138
+ request: Any,
139
+ ) -> AsyncIterable[object] | UnaryStreamCall[object, object]:
140
+ """Intercept the call to add HMAC authentication to the metadata fields.
141
+
142
+ This is a known method from the base class that is overridden.
143
+
144
+ Args:
145
+ continuation: The next interceptor in the chain.
146
+ client_call_details: The call details.
147
+ request: The request object.
148
+
149
+ Returns:
150
+ The response object (this implementation does not modify the response).
151
+ """
152
+ _add_hmac(
153
+ self._secret,
154
+ client_call_details,
155
+ int(time.time()),
156
+ secrets.token_bytes(16),
157
+ )
158
+ return await continuation(client_call_details, request) # type: ignore
@@ -88,6 +88,7 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
88
88
 
89
89
  async def stop(self) -> None:
90
90
  """Stop the streaming helper."""
91
+ _logger.info("%s: stopping the stream", self._stream_name)
91
92
  if self._task.done():
92
93
  return
93
94
  self._task.cancel()
@@ -110,6 +111,12 @@ class GrpcStreamBroadcaster(Generic[InputT, OutputT]):
110
111
  await sender.send(self._transform(msg))
111
112
  except grpc.aio.AioRpcError as err:
112
113
  error = err
114
+ except Exception as err: # pylint: disable=broad-except
115
+ _logger.exception(
116
+ "%s: raise an unexpected exception",
117
+ self._stream_name,
118
+ )
119
+ error = err
113
120
  if error is None and not self._retry_on_exhausted_stream:
114
121
  _logger.info(
115
122
  "%s: connection closed, stream exhausted", self._stream_name
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: frequenz-client-base
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Utilities for writing Frequenz API clients
5
5
  Author-email: Frequenz Energy-as-a-Service GmbH <floss@frequenz.com>
6
6
  License: MIT
@@ -21,53 +21,54 @@ Requires-Python: <4,>=3.11
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
23
  Requires-Dist: frequenz-channels<2,>=1.6.1
24
- Requires-Dist: grpcio<2,>=1.54.2
25
- Requires-Dist: protobuf<6,>=4.21.6
26
- Requires-Dist: typing-extensions<5,>=4.5.0
24
+ Requires-Dist: grpcio<2,>=1.59
25
+ Requires-Dist: protobuf<7,>=5.29.2
26
+ Requires-Dist: typing-extensions<5,>=4.6.0
27
27
  Provides-Extra: dev-flake8
28
- Requires-Dist: flake8==7.1.1; extra == "dev-flake8"
28
+ Requires-Dist: flake8==7.2.0; extra == "dev-flake8"
29
29
  Requires-Dist: flake8-docstrings==1.7.0; extra == "dev-flake8"
30
30
  Requires-Dist: flake8-pyproject==1.2.3; extra == "dev-flake8"
31
- Requires-Dist: pydoclint==0.6.0; extra == "dev-flake8"
31
+ Requires-Dist: pydoclint==0.6.6; extra == "dev-flake8"
32
32
  Requires-Dist: pydocstyle==6.3.0; extra == "dev-flake8"
33
33
  Requires-Dist: frequenz-client-base; extra == "dev-flake8"
34
34
  Provides-Extra: dev-formatting
35
35
  Requires-Dist: black==25.1.0; extra == "dev-formatting"
36
- Requires-Dist: isort==6.0.0; extra == "dev-formatting"
36
+ Requires-Dist: isort==6.0.1; extra == "dev-formatting"
37
37
  Provides-Extra: dev-mkdocs
38
38
  Requires-Dist: black==25.1.0; extra == "dev-mkdocs"
39
- Requires-Dist: Markdown==3.7; extra == "dev-mkdocs"
39
+ Requires-Dist: Markdown==3.8; extra == "dev-mkdocs"
40
40
  Requires-Dist: mike==2.1.3; extra == "dev-mkdocs"
41
41
  Requires-Dist: mkdocs-gen-files==0.5.0; extra == "dev-mkdocs"
42
- Requires-Dist: mkdocs-literate-nav==0.6.1; extra == "dev-mkdocs"
42
+ Requires-Dist: mkdocs-literate-nav==0.6.2; extra == "dev-mkdocs"
43
43
  Requires-Dist: mkdocs-macros-plugin==1.3.7; extra == "dev-mkdocs"
44
- Requires-Dist: mkdocs-material==9.6.1; extra == "dev-mkdocs"
45
- Requires-Dist: mkdocstrings[python]==0.27.0; extra == "dev-mkdocs"
46
- Requires-Dist: mkdocstrings-python==1.13.0; extra == "dev-mkdocs"
47
- Requires-Dist: frequenz-repo-config[lib]==0.11.0; extra == "dev-mkdocs"
44
+ Requires-Dist: mkdocs-material==9.6.12; extra == "dev-mkdocs"
45
+ Requires-Dist: mkdocstrings[python]==0.29.1; extra == "dev-mkdocs"
46
+ Requires-Dist: mkdocstrings-python==1.16.10; extra == "dev-mkdocs"
47
+ Requires-Dist: frequenz-repo-config[lib]==0.13.3; extra == "dev-mkdocs"
48
48
  Requires-Dist: frequenz-client-base; extra == "dev-mkdocs"
49
49
  Provides-Extra: dev-mypy
50
- Requires-Dist: mypy==1.14.1; extra == "dev-mypy"
51
- Requires-Dist: types-Markdown==3.7.0.20241204; extra == "dev-mypy"
52
- Requires-Dist: types-protobuf==5.29.1.20241207; extra == "dev-mypy"
53
- Requires-Dist: grpc-stubs==1.53.0.5; extra == "dev-mypy"
50
+ Requires-Dist: mypy==1.15.0; extra == "dev-mypy"
51
+ Requires-Dist: types-Markdown==3.8.0.20250415; extra == "dev-mypy"
52
+ Requires-Dist: types-protobuf==5.29.1.20250403; extra == "dev-mypy"
53
+ Requires-Dist: grpc-stubs==1.53.0.6; extra == "dev-mypy"
54
54
  Requires-Dist: frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]; extra == "dev-mypy"
55
55
  Provides-Extra: dev-noxfile
56
- Requires-Dist: nox==2024.10.9; extra == "dev-noxfile"
57
- Requires-Dist: frequenz-repo-config[lib]==0.11.0; extra == "dev-noxfile"
56
+ Requires-Dist: nox==2025.5.1; extra == "dev-noxfile"
57
+ Requires-Dist: frequenz-repo-config[lib]==0.13.3; extra == "dev-noxfile"
58
58
  Provides-Extra: dev-pylint
59
- Requires-Dist: pylint==3.3.4; extra == "dev-pylint"
59
+ Requires-Dist: pylint==3.3.6; extra == "dev-pylint"
60
60
  Requires-Dist: frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]; extra == "dev-pylint"
61
61
  Provides-Extra: dev-pytest
62
- Requires-Dist: pytest==8.3.4; extra == "dev-pytest"
63
- Requires-Dist: frequenz-repo-config[extra-lint-examples]==0.11.0; extra == "dev-pytest"
62
+ Requires-Dist: pytest==8.3.5; extra == "dev-pytest"
63
+ Requires-Dist: frequenz-repo-config[extra-lint-examples]==0.13.3; extra == "dev-pytest"
64
64
  Requires-Dist: pytest-mock==3.14.0; extra == "dev-pytest"
65
- Requires-Dist: pytest-asyncio==0.25.3; extra == "dev-pytest"
65
+ Requires-Dist: pytest-asyncio==0.26.0; extra == "dev-pytest"
66
66
  Requires-Dist: async-solipsism==0.7; extra == "dev-pytest"
67
- Requires-Dist: hypothesis==6.124.9; extra == "dev-pytest"
67
+ Requires-Dist: hypothesis==6.131.9; extra == "dev-pytest"
68
68
  Requires-Dist: frequenz-client-base; extra == "dev-pytest"
69
69
  Provides-Extra: dev
70
70
  Requires-Dist: frequenz-client-base[dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]; extra == "dev"
71
+ Dynamic: license-file
71
72
 
72
73
  # Frequenz Client Base Library
73
74
 
@@ -4,6 +4,7 @@ README.md
4
4
  RELEASE_NOTES.md
5
5
  pyproject.toml
6
6
  src/frequenz/client/base/__init__.py
7
+ src/frequenz/client/base/authentication.py
7
8
  src/frequenz/client/base/channel.py
8
9
  src/frequenz/client/base/client.py
9
10
  src/frequenz/client/base/conftest.py
@@ -11,6 +12,7 @@ src/frequenz/client/base/conversion.py
11
12
  src/frequenz/client/base/exception.py
12
13
  src/frequenz/client/base/py.typed
13
14
  src/frequenz/client/base/retry.py
15
+ src/frequenz/client/base/signing.py
14
16
  src/frequenz/client/base/streaming.py
15
17
  src/frequenz_client_base.egg-info/PKG-INFO
16
18
  src/frequenz_client_base.egg-info/SOURCES.txt
@@ -1,56 +1,56 @@
1
1
  frequenz-channels<2,>=1.6.1
2
- grpcio<2,>=1.54.2
3
- protobuf<6,>=4.21.6
4
- typing-extensions<5,>=4.5.0
2
+ grpcio<2,>=1.59
3
+ protobuf<7,>=5.29.2
4
+ typing-extensions<5,>=4.6.0
5
5
 
6
6
  [dev]
7
7
  frequenz-client-base[dev-flake8,dev-formatting,dev-mkdocs,dev-mypy,dev-noxfile,dev-pylint,dev-pytest]
8
8
 
9
9
  [dev-flake8]
10
- flake8==7.1.1
10
+ flake8==7.2.0
11
11
  flake8-docstrings==1.7.0
12
12
  flake8-pyproject==1.2.3
13
- pydoclint==0.6.0
13
+ pydoclint==0.6.6
14
14
  pydocstyle==6.3.0
15
15
  frequenz-client-base
16
16
 
17
17
  [dev-formatting]
18
18
  black==25.1.0
19
- isort==6.0.0
19
+ isort==6.0.1
20
20
 
21
21
  [dev-mkdocs]
22
22
  black==25.1.0
23
- Markdown==3.7
23
+ Markdown==3.8
24
24
  mike==2.1.3
25
25
  mkdocs-gen-files==0.5.0
26
- mkdocs-literate-nav==0.6.1
26
+ mkdocs-literate-nav==0.6.2
27
27
  mkdocs-macros-plugin==1.3.7
28
- mkdocs-material==9.6.1
29
- mkdocstrings[python]==0.27.0
30
- mkdocstrings-python==1.13.0
31
- frequenz-repo-config[lib]==0.11.0
28
+ mkdocs-material==9.6.12
29
+ mkdocstrings[python]==0.29.1
30
+ mkdocstrings-python==1.16.10
31
+ frequenz-repo-config[lib]==0.13.3
32
32
  frequenz-client-base
33
33
 
34
34
  [dev-mypy]
35
- mypy==1.14.1
36
- types-Markdown==3.7.0.20241204
37
- types-protobuf==5.29.1.20241207
38
- grpc-stubs==1.53.0.5
35
+ mypy==1.15.0
36
+ types-Markdown==3.8.0.20250415
37
+ types-protobuf==5.29.1.20250403
38
+ grpc-stubs==1.53.0.6
39
39
  frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
40
40
 
41
41
  [dev-noxfile]
42
- nox==2024.10.9
43
- frequenz-repo-config[lib]==0.11.0
42
+ nox==2025.5.1
43
+ frequenz-repo-config[lib]==0.13.3
44
44
 
45
45
  [dev-pylint]
46
- pylint==3.3.4
46
+ pylint==3.3.6
47
47
  frequenz-client-base[dev-mkdocs,dev-noxfile,dev-pytest]
48
48
 
49
49
  [dev-pytest]
50
- pytest==8.3.4
51
- frequenz-repo-config[extra-lint-examples]==0.11.0
50
+ pytest==8.3.5
51
+ frequenz-repo-config[extra-lint-examples]==0.13.3
52
52
  pytest-mock==3.14.0
53
- pytest-asyncio==0.25.3
53
+ pytest-asyncio==0.26.0
54
54
  async-solipsism==0.7
55
- hypothesis==6.124.9
55
+ hypothesis==6.131.9
56
56
  frequenz-client-base
@@ -1,6 +0,0 @@
1
- # Frequenz Client Base Library Release Notes
2
-
3
- ## New Features
4
-
5
- * Add the `warn_on_overflow` option to the streaming receivers to allow ignoring overflow warnings
6
-