Skip to content

Commit

Permalink
fix: add operation name to x-goog-request-params in async client (#137)
Browse files Browse the repository at this point in the history
Pass the operation name in the `x-goog-request-params`header.
 
Same as #133 for the async operations client.
  • Loading branch information
busunkim96 committed Feb 12, 2021
1 parent 7273090 commit 7271b23
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
19 changes: 19 additions & 0 deletions google/api_core/operations_v1/operations_async_client.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
20 changes: 12 additions & 8 deletions tests/asyncio/operations_v1/test_operations_async_client.py
Expand Up @@ -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


Expand All @@ -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 = []
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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"]

0 comments on commit 7271b23

Please sign in to comment.