From 7271b23afddb032e49e957525704d0cd5bfa4c65 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Thu, 11 Feb 2021 18:06:02 -0700 Subject: [PATCH] fix: add operation name to x-goog-request-params in async client (#137) Pass the operation name in the `x-goog-request-params`header. Same as #133 for the async operations client. --- .../operations_v1/operations_async_client.py | 19 ++++++++++++++++++ .../test_operations_async_client.py | 20 +++++++++++-------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/google/api_core/operations_v1/operations_async_client.py b/google/api_core/operations_v1/operations_async_client.py index 5d7b26cf..b3d0135b 100644 --- a/google/api_core/operations_v1/operations_async_client.py +++ b/google/api_core/operations_v1/operations_async_client.py @@ -120,6 +120,11 @@ async def get_operation( subclass will be raised. """ request = operations_pb2.GetOperationRequest(name=name) + + # Add routing header + metadata = metadata or [] + metadata.append(gapic_v1.routing_header.to_grpc_metadata({"name": name})) + return await self._get_operation(request, retry=retry, timeout=timeout, metadata=metadata) async def list_operations( @@ -182,6 +187,10 @@ async def list_operations( # Create the request object. request = operations_pb2.ListOperationsRequest(name=name, filter=filter_) + # Add routing header + metadata = metadata or [] + metadata.append(gapic_v1.routing_header.to_grpc_metadata({"name": name})) + # Create the method used to fetch pages method = functools.partial(self._list_operations, retry=retry, timeout=timeout, metadata=metadata) @@ -246,6 +255,11 @@ async def cancel_operation( """ # Create the request object. request = operations_pb2.CancelOperationRequest(name=name) + + # Add routing header + metadata = metadata or [] + metadata.append(gapic_v1.routing_header.to_grpc_metadata({"name": name})) + await self._cancel_operation(request, retry=retry, timeout=timeout, metadata=metadata) async def delete_operation( @@ -292,4 +306,9 @@ async def delete_operation( """ # Create the request object. request = operations_pb2.DeleteOperationRequest(name=name) + + # Add routing header + metadata = metadata or [] + metadata.append(gapic_v1.routing_header.to_grpc_metadata({"name": name})) + await self._delete_operation(request, retry=retry, timeout=timeout, metadata=metadata) diff --git a/tests/asyncio/operations_v1/test_operations_async_client.py b/tests/asyncio/operations_v1/test_operations_async_client.py index 830cd465..a6469016 100644 --- a/tests/asyncio/operations_v1/test_operations_async_client.py +++ b/tests/asyncio/operations_v1/test_operations_async_client.py @@ -36,10 +36,11 @@ async def test_get_operation(): operations_pb2.Operation(name="meep")) client = operations_v1.OperationsAsyncClient(mocked_channel) - response = await client.get_operation("name", metadata=[("x-goog-request-params", "foo")]) + response = await client.get_operation("name", metadata=[("header", "foo")]) assert method.call_count == 1 assert tuple(method.call_args_list[0])[0][0].name == "name" - assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("header", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("x-goog-request-params", "name=name") in tuple(method.call_args_list[0])[1]["metadata"] assert response == fake_call.response @@ -54,7 +55,7 @@ async def test_list_operations(): mocked_channel, method, fake_call = _mock_grpc_objects(list_response) client = operations_v1.OperationsAsyncClient(mocked_channel) - pager = await client.list_operations("name", "filter", metadata=[("x-goog-request-params", "foo")]) + pager = await client.list_operations("name", "filter", metadata=[("header", "foo")]) assert isinstance(pager, page_iterator_async.AsyncIterator) responses = [] @@ -64,7 +65,8 @@ async def test_list_operations(): assert responses == operations assert method.call_count == 1 - assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("header", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("x-goog-request-params", "name=name") in tuple(method.call_args_list[0])[1]["metadata"] request = tuple(method.call_args_list[0])[0][0] assert isinstance(request, operations_pb2.ListOperationsRequest) assert request.name == "name" @@ -77,11 +79,12 @@ async def test_delete_operation(): empty_pb2.Empty()) client = operations_v1.OperationsAsyncClient(mocked_channel) - await client.delete_operation("name", metadata=[("x-goog-request-params", "foo")]) + await client.delete_operation("name", metadata=[("header", "foo")]) assert method.call_count == 1 assert tuple(method.call_args_list[0])[0][0].name == "name" - assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("header", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("x-goog-request-params", "name=name") in tuple(method.call_args_list[0])[1]["metadata"] @pytest.mark.asyncio @@ -90,8 +93,9 @@ async def test_cancel_operation(): empty_pb2.Empty()) client = operations_v1.OperationsAsyncClient(mocked_channel) - await client.cancel_operation("name", metadata=[("x-goog-request-params", "foo")]) + await client.cancel_operation("name", metadata=[("header", "foo")]) assert method.call_count == 1 assert tuple(method.call_args_list[0])[0][0].name == "name" - assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("header", "foo") in tuple(method.call_args_list[0])[1]["metadata"] + assert ("x-goog-request-params", "name=name") in tuple(method.call_args_list[0])[1]["metadata"]