2424# request user-agent directly by the google-api-core package:
2525# https://github.com/googleapis/python-api-core/issues/416
2626from importlib import metadata
27+ from typing import List , Optional , Tuple , Union
2728
29+ from google .protobuf .internal import api_implementation
30+ from google .protobuf .message import Message as ProtobufMessageType
31+ import grpc
32+
33+ from google .ads .googleads .interceptors import Interceptor , MetadataType , ContinuationType
34+
35+
36+ # Determine which version of the package is installed.
2837try :
2938 _PROTOBUF_VERSION = metadata .version ("protobuf" )
3039except metadata .PackageNotFoundError :
3140 _PROTOBUF_VERSION = None
3241
33-
34- from google .protobuf .internal import api_implementation
35- from grpc import UnaryUnaryClientInterceptor , UnaryStreamClientInterceptor
36-
37- from .interceptor import Interceptor
38-
3942# Determine which protobuf implementation is being used.
4043if api_implementation .Type () == "cpp" :
4144 _PB_IMPL_HEADER = "+c"
4447else :
4548 _PB_IMPL_HEADER = ""
4649
47-
4850class MetadataInterceptor (
49- Interceptor , UnaryUnaryClientInterceptor , UnaryStreamClientInterceptor
51+ Interceptor ,
52+ grpc .UnaryUnaryClientInterceptor ,
53+ grpc .UnaryStreamClientInterceptor
5054):
5155 """An interceptor that appends custom metadata to requests."""
5256
5357 def __init__ (
5458 self ,
55- developer_token ,
56- login_customer_id ,
57- linked_customer_id = None ,
58- use_cloud_org_for_api_access = None ,
59+ developer_token : str ,
60+ login_customer_id : Optional [ str ] = None ,
61+ linked_customer_id : Optional [ str ] = None ,
62+ use_cloud_org_for_api_access : Optional [ bool ] = None ,
5963 ):
6064 """Initialization method for this class.
6165
@@ -69,21 +73,26 @@ def __init__(
6973 levels. Use this flag only if you are enrolled into a limited
7074 pilot that supports this configuration
7175 """
72- self .developer_token_meta = ("developer-token" , developer_token )
73- self .login_customer_id_meta = (
76+ self .developer_token_meta : Tuple [str , str ] = (
77+ "developer-token" ,
78+ developer_token ,
79+ )
80+ self .login_customer_id_meta : Optional [Tuple [str , str ]] = (
7481 ("login-customer-id" , login_customer_id )
7582 if login_customer_id
7683 else None
7784 )
78- self .linked_customer_id_meta = (
85+ self .linked_customer_id_meta : Optional [ Tuple [ str , str ]] = (
7986 ("linked-customer-id" , linked_customer_id )
8087 if linked_customer_id
8188 else None
8289 )
83- self .use_cloud_org_for_api_access = use_cloud_org_for_api_access
90+ self .use_cloud_org_for_api_access : Optional [
91+ bool
92+ ] = use_cloud_org_for_api_access
8493
8594 def _update_client_call_details_metadata (
86- self , client_call_details , metadata
95+ self , client_call_details : grpc . ClientCallDetails , metadata : MetadataType
8796 ):
8897 """Updates the client call details with additional metadata.
8998
@@ -95,7 +104,7 @@ def _update_client_call_details_metadata(
95104 An new instance of grpc.ClientCallDetails with additional metadata
96105 from the GoogleAdsClient.
97106 """
98- client_call_details = self .get_client_call_details_instance (
107+ client_call_details : grpc . ClientCallDetails = self .get_client_call_details_instance (
99108 client_call_details .method ,
100109 client_call_details .timeout ,
101110 metadata ,
@@ -104,7 +113,12 @@ def _update_client_call_details_metadata(
104113
105114 return client_call_details
106115
107- def _intercept (self , continuation , client_call_details , request ):
116+ def _intercept (
117+ self ,
118+ continuation : ContinuationType ,
119+ client_call_details : grpc .ClientCallDetails ,
120+ request : ProtobufMessageType ,
121+ ) -> Union [grpc .Call , grpc .Future ]:
108122 """Generic interceptor used for Unary-Unary and Unary-Stream requests.
109123
110124 Args:
@@ -118,9 +132,9 @@ def _intercept(self, continuation, client_call_details, request):
118132 A grpc.Call/grpc.Future instance representing a service response.
119133 """
120134 if client_call_details .metadata is None :
121- metadata = []
135+ metadata : MetadataType = []
122136 else :
123- metadata = list (client_call_details .metadata )
137+ metadata : MetadataType = list (client_call_details .metadata )
124138
125139 # If self.use_cloud_org_for_api_access is not True, add the developer
126140 # token to the request's metadata
@@ -135,31 +149,36 @@ def _intercept(self, continuation, client_call_details, request):
135149
136150 # TODO: This logic should be updated or removed once the following is
137151 # fixed: https://github.com/googleapis/python-api-core/issues/416
138- for i , metadatum in enumerate (metadata ):
152+ for i , metadatum_tuple in enumerate (metadata ):
139153 # Check if the user agent header key is in the current metadatum
140- if "x-goog-api-client" in metadatum and _PROTOBUF_VERSION :
154+ if "x-goog-api-client" in metadatum_tuple and _PROTOBUF_VERSION :
141155 # Convert the tuple to a list so it can be modified.
142- metadatum = list (metadatum )
156+ metadatum : List [ str ] = list (metadatum_tuple )
143157 # Check that "pb" isn't already included in the user agent.
144158 if "pb" not in metadatum [1 ]:
145159 # Append the protobuf version key value pair to the end of
146160 # the string.
147161 metadatum [1 ] += f" pb/{ _PROTOBUF_VERSION } { _PB_IMPL_HEADER } "
148162 # Convert the metadatum back to a tuple.
149- metadatum = tuple (metadatum )
163+ metadatum_tuple : Tuple [ str , str ] = tuple (metadatum )
150164 # Splice the metadatum back in its original position in
151165 # order to preserve the order of the metadata list.
152- metadata [i ] = metadatum
166+ metadata [i ] = metadatum_tuple
153167 # Exit the loop since we already found the user agent.
154168 break
155169
156- client_call_details = self ._update_client_call_details_metadata (
170+ client_call_details : grpc . ClientCallDetails = self ._update_client_call_details_metadata (
157171 client_call_details , metadata
158172 )
159173
160174 return continuation (client_call_details , request )
161175
162- def intercept_unary_unary (self , continuation , client_call_details , request ):
176+ def intercept_unary_unary (
177+ self ,
178+ continuation : ContinuationType ,
179+ client_call_details : grpc .ClientCallDetails ,
180+ request : ProtobufMessageType ,
181+ ) -> Union [grpc .Call , grpc .Future ]:
163182 """Intercepts and appends custom metadata for Unary-Unary requests.
164183
165184 Overrides abstract method defined in grpc.UnaryUnaryClientInterceptor.
@@ -177,8 +196,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
177196 return self ._intercept (continuation , client_call_details , request )
178197
179198 def intercept_unary_stream (
180- self , continuation , client_call_details , request
181- ):
199+ self ,
200+ continuation : ContinuationType ,
201+ client_call_details : grpc .ClientCallDetails ,
202+ request : ProtobufMessageType ,
203+ ) -> Union [grpc .Call , grpc .Future ]:
182204 """Intercepts and appends custom metadata to Unary-Stream requests.
183205
184206 Overrides abstract method defined in grpc.UnaryStreamClientInterceptor.
0 commit comments