|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections import namedtuple |
| 4 | +from importlib.metadata import PackageNotFoundError, version |
| 5 | + |
| 6 | +import grpc |
| 7 | + |
3 | 8 | from hiero_sdk_python.hapi.services import ( |
4 | 9 | address_book_service_pb2_grpc, |
5 | 10 | consensus_service_pb2_grpc, |
|
14 | 19 | ) |
15 | 20 |
|
16 | 21 |
|
| 22 | +class _UserAgentInterceptor(grpc.UnaryUnaryClientInterceptor, grpc.UnaryStreamClientInterceptor): |
| 23 | + """ |
| 24 | + gRPC interceptor that appends an x-user-agent header to all outgoing requests. |
| 25 | + """ |
| 26 | + |
| 27 | + _HEADER_KEY = "x-user-agent" |
| 28 | + _SDK_NAME = "hiero-sdk-python" |
| 29 | + _CallDetails = namedtuple( |
| 30 | + "_CallDetails", |
| 31 | + ("method", "timeout", "metadata", "credentials", "wait_for_ready", "compression"), |
| 32 | + ) |
| 33 | + |
| 34 | + def __init__(self) -> None: |
| 35 | + """ |
| 36 | + Initialize the interceptor and compute the user agent value. |
| 37 | + The user agent is computed once during initialization to avoid repeated package metadata lookups on every request. |
| 38 | + """ |
| 39 | + |
| 40 | + try: |
| 41 | + sdk_version = version(self._SDK_NAME) |
| 42 | + except PackageNotFoundError: |
| 43 | + sdk_version = "dev" |
| 44 | + self._user_agent = f"{self._SDK_NAME}/{sdk_version}" |
| 45 | + |
| 46 | + def _with_user_agent(self, details: grpc.ClientCallDetails) -> grpc.ClientCallDetails: |
| 47 | + """ |
| 48 | + Append the user agent header to the call details. |
| 49 | +
|
| 50 | + Args: |
| 51 | + details: The original gRPC call details. |
| 52 | +
|
| 53 | + Returns: |
| 54 | + A new ClientCallDetails object with the x-user-agent header included in the metadata. |
| 55 | + """ |
| 56 | + metadata = [] if details.metadata is None else list(details.metadata) |
| 57 | + metadata = [entry for entry in metadata if entry[0] != self._HEADER_KEY] |
| 58 | + metadata.append((self._HEADER_KEY, self._user_agent)) |
| 59 | + |
| 60 | + return self._CallDetails( |
| 61 | + details.method, |
| 62 | + details.timeout, |
| 63 | + metadata, |
| 64 | + getattr(details, "credentials", None), |
| 65 | + getattr(details, "wait_for_ready", None), |
| 66 | + getattr(details, "compression", None), |
| 67 | + ) |
| 68 | + |
| 69 | + def intercept_unary_unary(self, continuation, client_call_details, request): |
| 70 | + """ |
| 71 | + Intercept unary-unary calls and append the user agent header. |
| 72 | +
|
| 73 | + Args: |
| 74 | + continuation: The gRPC continuation function to call the next interceptor or actual RPC. |
| 75 | + client_call_details: The details of the gRPC call, including method, timeout, metadata, etc. |
| 76 | + request: The request object being sent. |
| 77 | +
|
| 78 | + Returns: |
| 79 | + The result of the gRPC call after appending the user agent header. |
| 80 | + """ |
| 81 | + |
| 82 | + return continuation(self._with_user_agent(client_call_details), request) |
| 83 | + |
| 84 | + def intercept_unary_stream(self, continuation, client_call_details, request): |
| 85 | + """ |
| 86 | + Intercept unary-stream calls and append the user agent header. |
| 87 | +
|
| 88 | + Args: |
| 89 | + continuation: The gRPC continuation function to call the next interceptor or actual RPC. |
| 90 | + client_call_details: The details of the gRPC call, including method, timeout, metadata, etc. |
| 91 | + request: The request object being sent. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + The result of the gRPC call after appending the user agent header. |
| 95 | + """ |
| 96 | + |
| 97 | + return continuation(self._with_user_agent(client_call_details), request) |
| 98 | + |
| 99 | + |
17 | 100 | class _Channel: |
18 | 101 | """ |
19 | 102 | The _Channel class is a wrapper around gRPC channels that provides access to various |
|
0 commit comments