Skip to content

Commit 629cf19

Browse files
authored
refactor: reduce code duplication (#2373)
1 parent 7542410 commit 629cf19

File tree

14 files changed

+53
-87
lines changed
  • gapic/templates/%namespace/%name_%version/%sub/services/%service
  • tests/integration/goldens
    • asset/google/cloud/asset_v1/services/asset_service/transports
    • credentials/google/iam/credentials_v1/services/iam_credentials/transports
    • eventarc/google/cloud/eventarc_v1/services/eventarc/transports
    • logging_internal/google/cloud/logging_v2/services
      • config_service_v2/transports
      • logging_service_v2/transports
      • metrics_service_v2/transports
    • logging/google/cloud/logging_v2/services
      • config_service_v2/transports
      • logging_service_v2/transports
      • metrics_service_v2/transports
    • redis_selective/google/cloud/redis_v1/services/cloud_redis/transports
    • redis/google/cloud/redis_v1/services/cloud_redis/transports

14 files changed

+53
-87
lines changed

gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,42 @@ def _get_http_options():
264264
{% endmacro %}
265265

266266

267+
{% macro unary_request_interceptor_common(service) %}
268+
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
269+
if logging_enabled: # pragma: NO COVER
270+
request_metadata = client_call_details.metadata
271+
if isinstance(request, proto.Message):
272+
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
273+
or wait for next gen protobuf.
274+
#}
275+
request_payload = type(request).to_json(request)
276+
elif isinstance(request, google.protobuf.message.Message):
277+
request_payload = MessageToJson(request)
278+
else:
279+
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"
280+
281+
request_metadata = {
282+
key: value.decode("utf-8") if isinstance(value, bytes) else value
283+
for key, value in request_metadata
284+
}
285+
grpc_request = {
286+
"payload": request_payload,
287+
"requestMethod": "grpc",
288+
"metadata": dict(request_metadata),
289+
}
290+
_LOGGER.debug(
291+
f"Sending request for {client_call_details.method}",
292+
extra = {
293+
"serviceName": "{{ service.meta.address.proto }}",
294+
"rpcName": str(client_call_details.method),
295+
"request": grpc_request,
296+
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport. #}
297+
"metadata": grpc_request["metadata"],
298+
},
299+
)
300+
{%- endmacro %}
301+
302+
267303
{% macro prep_wrapped_messages_async_method(api, service) %}
268304
def _prep_wrapped_messages(self, client_info):
269305
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc.py.j2

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends '_base.py.j2' %}
22

3+
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
4+
35
{% block content %}
46

57
import json
@@ -59,39 +61,7 @@ _LOGGER = std_logging.getLogger(__name__)
5961

6062
class _LoggingClientInterceptor(grpc.UnaryUnaryClientInterceptor): # pragma: NO COVER
6163
def intercept_unary_unary(self, continuation, client_call_details, request):
62-
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
63-
if logging_enabled: # pragma: NO COVER
64-
request_metadata = client_call_details.metadata
65-
if isinstance(request, proto.Message):
66-
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
67-
or wait for next gen protobuf.
68-
#}
69-
request_payload = type(request).to_json(request)
70-
elif isinstance(request, google.protobuf.message.Message):
71-
request_payload = MessageToJson(request)
72-
else:
73-
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"
74-
75-
request_metadata = {
76-
key: value.decode("utf-8") if isinstance(value, bytes) else value
77-
for key, value in request_metadata
78-
}
79-
grpc_request = {
80-
"payload": request_payload,
81-
"requestMethod": "grpc",
82-
"metadata": dict(request_metadata),
83-
}
84-
_LOGGER.debug(
85-
f"Sending request for {client_call_details.method}",
86-
extra = {
87-
"serviceName": "{{ service.meta.address.proto }}",
88-
"rpcName": client_call_details.method,
89-
"request": grpc_request,
90-
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport. #}
91-
"metadata": grpc_request["metadata"],
92-
},
93-
)
94-
64+
{{ shared_macros.unary_request_interceptor_common(service) }}
9565
response = continuation(client_call_details, request)
9666
if logging_enabled: # pragma: NO COVER
9767
response_metadata = response.trailing_metadata()

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{% extends '_base.py.j2' %}
22

3+
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
4+
35
{% block content %}
46
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
57

@@ -64,38 +66,7 @@ _LOGGER = std_logging.getLogger(__name__)
6466

6567
class _LoggingClientAIOInterceptor(grpc.aio.UnaryUnaryClientInterceptor): # pragma: NO COVER
6668
async def intercept_unary_unary(self, continuation, client_call_details, request):
67-
logging_enabled = CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(std_logging.DEBUG)
68-
if logging_enabled: # pragma: NO COVER
69-
request_metadata = client_call_details.metadata
70-
if isinstance(request, proto.Message):
71-
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2293): Investigate if we can improve this logic
72-
or wait for next gen protobuf.
73-
#}
74-
request_payload = type(request).to_json(request)
75-
elif isinstance(request, google.protobuf.message.Message):
76-
request_payload = MessageToJson(request)
77-
else:
78-
request_payload = f"{type(request).__name__}: {pickle.dumps(request)}"
79-
80-
request_metadata = {
81-
key: value.decode("utf-8") if isinstance(value, bytes) else value
82-
for key, value in request_metadata
83-
}
84-
grpc_request = {
85-
"payload": request_payload,
86-
"requestMethod": "grpc",
87-
"metadata": dict(request_metadata),
88-
}
89-
_LOGGER.debug(
90-
f"Sending request for {client_call_details.method}",
91-
extra = {
92-
"serviceName": "{{ service.meta.address.proto }}",
93-
"rpcName": str(client_call_details.method),
94-
"request": grpc_request,
95-
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2275): logging `metadata` seems repetitive and may need to be cleaned up. We're including it within "request" for consistency with REST transport.' #}
96-
"metadata": grpc_request["metadata"],
97-
},
98-
)
69+
{{ shared_macros.unary_request_interceptor_common(service) }}
9970
response = await continuation(client_call_details, request)
10071
if logging_enabled: # pragma: NO COVER
10172
response_metadata = await response.trailing_metadata()

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
7070
f"Sending request for {client_call_details.method}",
7171
extra = {
7272
"serviceName": "google.cloud.asset.v1.AssetService",
73-
"rpcName": client_call_details.method,
73+
"rpcName": str(client_call_details.method),
7474
"request": grpc_request,
7575
"metadata": grpc_request["metadata"],
7676
},
7777
)
78-
7978
response = continuation(client_call_details, request)
8079
if logging_enabled: # pragma: NO COVER
8180
response_metadata = response.trailing_metadata()

tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
6767
f"Sending request for {client_call_details.method}",
6868
extra = {
6969
"serviceName": "google.iam.credentials.v1.IAMCredentials",
70-
"rpcName": client_call_details.method,
70+
"rpcName": str(client_call_details.method),
7171
"request": grpc_request,
7272
"metadata": grpc_request["metadata"],
7373
},
7474
)
75-
7675
response = continuation(client_call_details, request)
7776
if logging_enabled: # pragma: NO COVER
7877
response_metadata = response.trailing_metadata()

tests/integration/goldens/eventarc/google/cloud/eventarc_v1/services/eventarc/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
7878
f"Sending request for {client_call_details.method}",
7979
extra = {
8080
"serviceName": "google.cloud.eventarc.v1.Eventarc",
81-
"rpcName": client_call_details.method,
81+
"rpcName": str(client_call_details.method),
8282
"request": grpc_request,
8383
"metadata": grpc_request["metadata"],
8484
},
8585
)
86-
8786
response = continuation(client_call_details, request)
8887
if logging_enabled: # pragma: NO COVER
8988
response_metadata = response.trailing_metadata()

tests/integration/goldens/logging/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
7070
f"Sending request for {client_call_details.method}",
7171
extra = {
7272
"serviceName": "google.logging.v2.ConfigServiceV2",
73-
"rpcName": client_call_details.method,
73+
"rpcName": str(client_call_details.method),
7474
"request": grpc_request,
7575
"metadata": grpc_request["metadata"],
7676
},
7777
)
78-
7978
response = continuation(client_call_details, request)
8079
if logging_enabled: # pragma: NO COVER
8180
response_metadata = response.trailing_metadata()

tests/integration/goldens/logging/google/cloud/logging_v2/services/logging_service_v2/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
6969
f"Sending request for {client_call_details.method}",
7070
extra = {
7171
"serviceName": "google.logging.v2.LoggingServiceV2",
72-
"rpcName": client_call_details.method,
72+
"rpcName": str(client_call_details.method),
7373
"request": grpc_request,
7474
"metadata": grpc_request["metadata"],
7575
},
7676
)
77-
7877
response = continuation(client_call_details, request)
7978
if logging_enabled: # pragma: NO COVER
8079
response_metadata = response.trailing_metadata()

tests/integration/goldens/logging/google/cloud/logging_v2/services/metrics_service_v2/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
6969
f"Sending request for {client_call_details.method}",
7070
extra = {
7171
"serviceName": "google.logging.v2.MetricsServiceV2",
72-
"rpcName": client_call_details.method,
72+
"rpcName": str(client_call_details.method),
7373
"request": grpc_request,
7474
"metadata": grpc_request["metadata"],
7575
},
7676
)
77-
7877
response = continuation(client_call_details, request)
7978
if logging_enabled: # pragma: NO COVER
8079
response_metadata = response.trailing_metadata()

tests/integration/goldens/logging_internal/google/cloud/logging_v2/services/config_service_v2/transports/grpc.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ def intercept_unary_unary(self, continuation, client_call_details, request):
7070
f"Sending request for {client_call_details.method}",
7171
extra = {
7272
"serviceName": "google.logging.v2.ConfigServiceV2",
73-
"rpcName": client_call_details.method,
73+
"rpcName": str(client_call_details.method),
7474
"request": grpc_request,
7575
"metadata": grpc_request["metadata"],
7676
},
7777
)
78-
7978
response = continuation(client_call_details, request)
8079
if logging_enabled: # pragma: NO COVER
8180
response_metadata = response.trailing_metadata()

0 commit comments

Comments
 (0)