Repository: googleapis/python-api-core
Pull request #133 (2021-01-19 21:13:15)

fix: add operation name to x-goog-request-params
Add the routing header `x-goog-request-params` to operations client requests. Some APIs require that the header be passed on all requests.

Fixes https://github.com/googleapis/gapic-generator-python/issues/732


Patch:
diff --git a/.trampolinerc b/.trampolinerc
index c7d663a..383b6ec 100644
--- a/.trampolinerc
+++ b/.trampolinerc
@@ -18,7 +18,6 @@
 required_envvars+=(
     "STAGING_BUCKET"
     "V2_STAGING_BUCKET"
-    "NOX_SESSION"
 )
 
 # Add env vars which are passed down into the container here.
diff --git a/google/api_core/operations_v1/operations_client.py b/google/api_core/operations_v1/operations_client.py
index b850796..b1218cf 100644
--- a/google/api_core/operations_v1/operations_client.py
+++ b/google/api_core/operations_v1/operations_client.py
@@ -134,6 +134,11 @@ class OperationsClient(object):
                 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 self._get_operation(request, retry=retry, timeout=timeout, metadata=metadata)
 
     def list_operations(
@@ -196,6 +201,10 @@ class OperationsClient(object):
         # 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)
 
@@ -260,6 +269,11 @@ class OperationsClient(object):
         """
         # 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}))
+
         self._cancel_operation(request, retry=retry, timeout=timeout, metadata=metadata)
 
     def delete_operation(
@@ -306,4 +320,9 @@ class OperationsClient(object):
         """
         # 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}))
+
         self._delete_operation(request, retry=retry, timeout=timeout, metadata=metadata)
diff --git a/synth.metadata b/synth.metadata
index d10948c..fd86311 100644
--- a/synth.metadata
+++ b/synth.metadata
@@ -4,14 +4,14 @@
       "git": {
         "name": ".",
         "remote": "https://github.com/googleapis/python-api-core.git",
-        "sha": "abc3d88466a1e55cf459e6c78a6fb7343a29df6f"
+        "sha": "232dab0ad3ef2cca0edfe707d8f90ca0ea200ba2"
       }
     },
     {
       "git": {
         "name": "synthtool",
         "remote": "https://github.com/googleapis/synthtool.git",
-        "sha": "16ec872dd898d7de6e1822badfac32484b5d9031"
+        "sha": "ba960d730416fe05c50547e975ce79fcee52c671"
       }
     }
   ],


Test patch:
diff --git a/tests/unit/operations_v1/test_operations_client.py b/tests/unit/operations_v1/test_operations_client.py
index bd7f373..ed19b7f 100644
--- a/tests/unit/operations_v1/test_operations_client.py
+++ b/tests/unit/operations_v1/test_operations_client.py
@@ -24,9 +24,10 @@ def test_get_operation():
     client = operations_v1.OperationsClient(channel)
     channel.GetOperation.response = operations_pb2.Operation(name="meep")
 
-    response = client.get_operation("name", metadata=[("x-goog-request-params", "foo")])
+    response = client.get_operation("name", metadata=[("header", "foo")])
 
-    assert ("x-goog-request-params", "foo") in channel.GetOperation.calls[0].metadata
+    assert ("header", "foo") in channel.GetOperation.calls[0].metadata
+    assert ("x-goog-request-params", "name=name") in channel.GetOperation.calls[0].metadata
     assert len(channel.GetOperation.requests) == 1
     assert channel.GetOperation.requests[0].name == "name"
     assert response == channel.GetOperation.response
@@ -42,12 +43,13 @@ def test_list_operations():
     list_response = operations_pb2.ListOperationsResponse(operations=operations)
     channel.ListOperations.response = list_response
 
-    response = client.list_operations("name", "filter", metadata=[("x-goog-request-params", "foo")])
+    response = client.list_operations("name", "filter", metadata=[("header", "foo")])
 
     assert isinstance(response, page_iterator.Iterator)
     assert list(response) == operations
 
-    assert ("x-goog-request-params", "foo") in channel.ListOperations.calls[0].metadata
+    assert ("header", "foo") in channel.ListOperations.calls[0].metadata
+    assert ("x-goog-request-params", "name=name") in channel.ListOperations.calls[0].metadata
     assert len(channel.ListOperations.requests) == 1
     request = channel.ListOperations.requests[0]
     assert isinstance(request, operations_pb2.ListOperationsRequest)
@@ -60,9 +62,10 @@ def test_delete_operation():
     client = operations_v1.OperationsClient(channel)
     channel.DeleteOperation.response = empty_pb2.Empty()
 
-    client.delete_operation("name", metadata=[("x-goog-request-params", "foo")])
+    client.delete_operation("name", metadata=[("header", "foo")])
 
-    assert ("x-goog-request-params", "foo") in channel.DeleteOperation.calls[0].metadata
+    assert ("header", "foo") in channel.DeleteOperation.calls[0].metadata
+    assert ("x-goog-request-params", "name=name") in channel.DeleteOperation.calls[0].metadata
     assert len(channel.DeleteOperation.requests) == 1
     assert channel.DeleteOperation.requests[0].name == "name"
 
@@ -72,8 +75,9 @@ def test_cancel_operation():
     client = operations_v1.OperationsClient(channel)
     channel.CancelOperation.response = empty_pb2.Empty()
 
-    client.cancel_operation("name", metadata=[("x-goog-request-params", "foo")])
+    client.cancel_operation("name", metadata=[("header", "foo")])
 
-    assert ("x-goog-request-params", "foo") in channel.CancelOperation.calls[0].metadata
+    assert ("header", "foo") in channel.CancelOperation.calls[0].metadata
+    assert ("x-goog-request-params", "name=name") in channel.CancelOperation.calls[0].metadata
     assert len(channel.CancelOperation.requests) == 1
     assert channel.CancelOperation.requests[0].name == "name"