diff --git a/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/async_client.py b/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/async_client.py index e8364ae2387f..d62898ccca71 100644 --- a/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/async_client.py +++ b/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/async_client.py @@ -218,9 +218,9 @@ async def list_traces( from google.cloud import trace_v1 - def sample_list_traces(): + async def sample_list_traces(): # Create a client - client = trace_v1.TraceServiceClient() + client = trace_v1.TraceServiceAsyncClient() # Initialize request argument(s) request = trace_v1.ListTracesRequest( @@ -231,7 +231,7 @@ def sample_list_traces(): page_result = client.list_traces(request=request) # Handle the response - for response in page_result: + async for response in page_result: print(response) Args: @@ -295,6 +295,14 @@ def sample_list_traces(): client_info=DEFAULT_CLIENT_INFO, ) + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("project_id", request.project_id),) + ), + ) + # Send the request. response = await rpc( request, @@ -331,9 +339,9 @@ async def get_trace( from google.cloud import trace_v1 - def sample_get_trace(): + async def sample_get_trace(): # Create a client - client = trace_v1.TraceServiceClient() + client = trace_v1.TraceServiceAsyncClient() # Initialize request argument(s) request = trace_v1.GetTraceRequest( @@ -342,7 +350,7 @@ def sample_get_trace(): ) # Make the request - response = client.get_trace(request=request) + response = await client.get_trace(request=request) # Handle the response print(response) @@ -415,6 +423,17 @@ def sample_get_trace(): client_info=DEFAULT_CLIENT_INFO, ) + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + ( + ("project_id", request.project_id), + ("trace_id", request.trace_id), + ) + ), + ) + # Send the request. response = await rpc( request, @@ -448,9 +467,9 @@ async def patch_traces( from google.cloud import trace_v1 - def sample_patch_traces(): + async def sample_patch_traces(): # Create a client - client = trace_v1.TraceServiceClient() + client = trace_v1.TraceServiceAsyncClient() # Initialize request argument(s) request = trace_v1.PatchTracesRequest( @@ -458,7 +477,7 @@ def sample_patch_traces(): ) # Make the request - client.patch_traces(request=request) + await client.patch_traces(request=request) Args: request (Union[google.cloud.trace_v1.types.PatchTracesRequest, dict]): @@ -519,6 +538,14 @@ def sample_patch_traces(): client_info=DEFAULT_CLIENT_INFO, ) + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("project_id", request.project_id),) + ), + ) + # Send the request. await rpc( request, diff --git a/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/client.py b/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/client.py index c26677009d18..2734000dddc9 100644 --- a/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/client.py +++ b/packages/google-cloud-trace/google/cloud/trace_v1/services/trace_service/client.py @@ -491,6 +491,14 @@ def sample_list_traces(): # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.list_traces] + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("project_id", request.project_id),) + ), + ) + # Send the request. response = rpc( request, @@ -601,6 +609,17 @@ def sample_get_trace(): # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.get_trace] + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + ( + ("project_id", request.project_id), + ("trace_id", request.trace_id), + ) + ), + ) + # Send the request. response = rpc( request, @@ -695,6 +714,14 @@ def sample_patch_traces(): # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.patch_traces] + # Certain fields should be provided within the metadata header; + # add these here. + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata( + (("project_id", request.project_id),) + ), + ) + # Send the request. rpc( request, diff --git a/packages/google-cloud-trace/google/cloud/trace_v2/services/trace_service/async_client.py b/packages/google-cloud-trace/google/cloud/trace_v2/services/trace_service/async_client.py index 5cb20c3e2538..df5b820ea220 100644 --- a/packages/google-cloud-trace/google/cloud/trace_v2/services/trace_service/async_client.py +++ b/packages/google-cloud-trace/google/cloud/trace_v2/services/trace_service/async_client.py @@ -224,9 +224,9 @@ async def batch_write_spans( from google.cloud import trace_v2 - def sample_batch_write_spans(): + async def sample_batch_write_spans(): # Create a client - client = trace_v2.TraceServiceClient() + client = trace_v2.TraceServiceAsyncClient() # Initialize request argument(s) spans = trace_v2.Span() @@ -239,7 +239,7 @@ def sample_batch_write_spans(): ) # Make the request - client.batch_write_spans(request=request) + await client.batch_write_spans(request=request) Args: request (Union[google.cloud.trace_v2.types.BatchWriteSpansRequest, dict]): @@ -321,9 +321,9 @@ async def create_span( from google.cloud import trace_v2 - def sample_create_span(): + async def sample_create_span(): # Create a client - client = trace_v2.TraceServiceClient() + client = trace_v2.TraceServiceAsyncClient() # Initialize request argument(s) request = trace_v2.Span( @@ -332,7 +332,7 @@ def sample_create_span(): ) # Make the request - response = client.create_span(request=request) + response = await client.create_span(request=request) # Handle the response print(response) diff --git a/packages/google-cloud-trace/tests/unit/gapic/trace_v1/test_trace_service.py b/packages/google-cloud-trace/tests/unit/gapic/trace_v1/test_trace_service.py index aec8dade7675..119a2965d571 100644 --- a/packages/google-cloud-trace/tests/unit/gapic/trace_v1/test_trace_service.py +++ b/packages/google-cloud-trace/tests/unit/gapic/trace_v1/test_trace_service.py @@ -726,6 +726,67 @@ async def test_list_traces_async_from_dict(): await test_list_traces_async(request_type=dict) +def test_list_traces_field_headers(): + client = TraceServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.ListTracesRequest() + + request.project_id = "project_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_traces), "__call__") as call: + call.return_value = trace.ListTracesResponse() + client.list_traces(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_list_traces_field_headers_async(): + client = TraceServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.ListTracesRequest() + + request.project_id = "project_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.list_traces), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + trace.ListTracesResponse() + ) + await client.list_traces(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value", + ) in kw["metadata"] + + def test_list_traces_flattened(): client = TraceServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -846,11 +907,14 @@ def test_list_traces_pager(transport_name: str = "grpc"): ) metadata = () + metadata = tuple(metadata) + ( + gapic_v1.routing_header.to_grpc_metadata((("project_id", ""),)), + ) pager = client.list_traces(request={}) assert pager._metadata == metadata - results = [i for i in pager] + results = list(pager) assert len(results) == 6 assert all(isinstance(i, trace.Trace) for i in results) @@ -1086,6 +1150,67 @@ async def test_get_trace_async_from_dict(): await test_get_trace_async(request_type=dict) +def test_get_trace_field_headers(): + client = TraceServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.GetTraceRequest() + + request.project_id = "project_id_value" + request.trace_id = "trace_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_trace), "__call__") as call: + call.return_value = trace.Trace() + client.get_trace(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value&trace_id=trace_id_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_trace_field_headers_async(): + client = TraceServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.GetTraceRequest() + + request.project_id = "project_id_value" + request.trace_id = "trace_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.get_trace), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(trace.Trace()) + await client.get_trace(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value&trace_id=trace_id_value", + ) in kw["metadata"] + + def test_get_trace_flattened(): client = TraceServiceClient( credentials=ga_credentials.AnonymousCredentials(), @@ -1257,6 +1382,65 @@ async def test_patch_traces_async_from_dict(): await test_patch_traces_async(request_type=dict) +def test_patch_traces_field_headers(): + client = TraceServiceClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.PatchTracesRequest() + + request.project_id = "project_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.patch_traces), "__call__") as call: + call.return_value = None + client.patch_traces(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_patch_traces_field_headers_async(): + client = TraceServiceAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = trace.PatchTracesRequest() + + request.project_id = "project_id_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object(type(client.transport.patch_traces), "__call__") as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(None) + await client.patch_traces(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "project_id=project_id_value", + ) in kw["metadata"] + + def test_patch_traces_flattened(): client = TraceServiceClient( credentials=ga_credentials.AnonymousCredentials(), diff --git a/packages/google-cloud-trace/tests/unit/gapic/trace_v2/test_trace_service.py b/packages/google-cloud-trace/tests/unit/gapic/trace_v2/test_trace_service.py index 77ef3b5b165f..42d01d3cf140 100644 --- a/packages/google-cloud-trace/tests/unit/gapic/trace_v2/test_trace_service.py +++ b/packages/google-cloud-trace/tests/unit/gapic/trace_v2/test_trace_service.py @@ -735,7 +735,7 @@ def test_batch_write_spans_field_headers(): # a field header. Set these to a non-empty value. request = tracing.BatchWriteSpansRequest() - request.name = "name/value" + request.name = "name_value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -753,7 +753,7 @@ def test_batch_write_spans_field_headers(): _, _, kw = call.mock_calls[0] assert ( "x-goog-request-params", - "name=name/value", + "name=name_value", ) in kw["metadata"] @@ -767,7 +767,7 @@ async def test_batch_write_spans_field_headers_async(): # a field header. Set these to a non-empty value. request = tracing.BatchWriteSpansRequest() - request.name = "name/value" + request.name = "name_value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( @@ -785,7 +785,7 @@ async def test_batch_write_spans_field_headers_async(): _, _, kw = call.mock_calls[0] assert ( "x-goog-request-params", - "name=name/value", + "name=name_value", ) in kw["metadata"] @@ -993,7 +993,7 @@ def test_create_span_field_headers(): # a field header. Set these to a non-empty value. request = trace.Span() - request.name = "name/value" + request.name = "name_value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.create_span), "__call__") as call: @@ -1009,7 +1009,7 @@ def test_create_span_field_headers(): _, _, kw = call.mock_calls[0] assert ( "x-goog-request-params", - "name=name/value", + "name=name_value", ) in kw["metadata"] @@ -1023,7 +1023,7 @@ async def test_create_span_field_headers_async(): # a field header. Set these to a non-empty value. request = trace.Span() - request.name = "name/value" + request.name = "name_value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.create_span), "__call__") as call: @@ -1039,7 +1039,7 @@ async def test_create_span_field_headers_async(): _, _, kw = call.mock_calls[0] assert ( "x-goog-request-params", - "name=name/value", + "name=name_value", ) in kw["metadata"]