Skip to content

Commit

Permalink
fix: resolve issue with wait operation mixin (#2218)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea authored Oct 10, 2024
1 parent 296cd3e commit 095d060
Show file tree
Hide file tree
Showing 12 changed files with 653 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# the request.
# gRPC handles serialization and deserialization, so we just need
# to pass in the functions for each.
if "delete_operation" not in self._stubs:
if "wait_operation" not in self._stubs:
self._stubs["wait_operation"] = self.grpc_channel.unary_unary(
"/google.longrunning.Operations/WaitOperation",
request_serializer=operations_pb2.WaitOperationRequest.SerializeToString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
# the request.
# gRPC handles serialization and deserialization, so we just need
# to pass in the functions for each.
if "delete_operation" not in self._stubs:
if "wait_operation" not in self._stubs:
self._stubs["wait_operation"] = self.grpc_channel.unary_unary(
"/google.longrunning.Operations/WaitOperation",
request_serializer=operations_pb2.WaitOperationRequest.SerializeToString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,62 @@ async def cancel_operation(
# Send the request.
await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

async def wait_operation(
self,
request: Optional[operations_pb2.WaitOperationRequest] = None,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> operations_pb2.Operation:
r"""Waits until the specified long-running operation is done or reaches at most
a specified timeout, returning the latest state.
If the operation is already done, the latest state is immediately returned.
If the timeout specified is greater than the default HTTP/RPC timeout, the HTTP/RPC
timeout is used. If the server does not support this method, it returns
`google.rpc.Code.UNIMPLEMENTED`.
Args:
request (:class:`~.operations_pb2.WaitOperationRequest`):
The request object. Request message for
`WaitOperation` method.
retry (google.api_core.retry_async.AsyncRetry): Designation of what errors,
if any, should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
~.operations_pb2.Operation:
An ``Operation`` object.
"""
# Create or coerce a protobuf request object.
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
request = operations_pb2.WaitOperationRequest(**request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self.transport._wrapped_methods[self._client._transport.wait_operation]

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata(
(("name", request.name),)),
)

# Validate the universe domain.
self._client._validate_universe_domain()

# Send the request.
response = await rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response

async def get_location(
self,
request: Optional[locations_pb2.GetLocationRequest] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,62 @@ def cancel_operation(
# Send the request.
rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

def wait_operation(
self,
request: Optional[operations_pb2.WaitOperationRequest] = None,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> operations_pb2.Operation:
r"""Waits until the specified long-running operation is done or reaches at most
a specified timeout, returning the latest state.
If the operation is already done, the latest state is immediately returned.
If the timeout specified is greater than the default HTTP/RPC timeout, the HTTP/RPC
timeout is used. If the server does not support this method, it returns
`google.rpc.Code.UNIMPLEMENTED`.
Args:
request (:class:`~.operations_pb2.WaitOperationRequest`):
The request object. Request message for
`WaitOperation` method.
retry (google.api_core.retry.Retry): Designation of what errors,
if any, should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
~.operations_pb2.Operation:
An ``Operation`` object.
"""
# Create or coerce a protobuf request object.
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
request = operations_pb2.WaitOperationRequest(**request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self._transport._wrapped_methods[self._transport.wait_operation]

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata(
(("name", request.name),)),
)

# Validate the universe domain.
self._validate_universe_domain()

# Send the request.
response = rpc(
request, retry=retry, timeout=timeout, metadata=metadata,)

# Done; return the response.
return response

def get_location(
self,
request: Optional[locations_pb2.GetLocationRequest] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def _prep_wrapped_messages(self, client_info):
default_timeout=None,
client_info=client_info,
),
self.wait_operation: gapic_v1.method.wrap_method(
self.wait_operation,
default_timeout=None,
client_info=client_info,
),
}

def close(self):
Expand Down Expand Up @@ -358,6 +363,15 @@ def delete_operation(
]:
raise NotImplementedError()

@property
def wait_operation(
self,
) -> Callable[
[operations_pb2.WaitOperationRequest],
Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
]:
raise NotImplementedError()

@property
def get_location(self,
) -> Callable[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,24 @@ def cancel_operation(
)
return self._stubs["cancel_operation"]

@property
def wait_operation(
self,
) -> Callable[[operations_pb2.WaitOperationRequest], None]:
r"""Return a callable for the wait_operation method over gRPC.
"""
# Generate a "stub function" on-the-fly which will actually make
# the request.
# gRPC handles serialization and deserialization, so we just need
# to pass in the functions for each.
if "wait_operation" not in self._stubs:
self._stubs["wait_operation"] = self.grpc_channel.unary_unary(
"/google.longrunning.Operations/WaitOperation",
request_serializer=operations_pb2.WaitOperationRequest.SerializeToString,
response_deserializer=None,
)
return self._stubs["wait_operation"]

@property
def get_operation(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ def _prep_wrapped_messages(self, client_info):
default_timeout=None,
client_info=client_info,
),
self.wait_operation: self._wrap_method(
self.wait_operation,
default_timeout=None,
client_info=client_info,
),
}

def _wrap_method(self, func, *args, **kwargs):
Expand Down Expand Up @@ -749,6 +754,24 @@ def cancel_operation(
)
return self._stubs["cancel_operation"]

@property
def wait_operation(
self,
) -> Callable[[operations_pb2.WaitOperationRequest], None]:
r"""Return a callable for the wait_operation method over gRPC.
"""
# Generate a "stub function" on-the-fly which will actually make
# the request.
# gRPC handles serialization and deserialization, so we just need
# to pass in the functions for each.
if "wait_operation" not in self._stubs:
self._stubs["wait_operation"] = self.grpc_channel.unary_unary(
"/google.longrunning.Operations/WaitOperation",
request_serializer=operations_pb2.WaitOperationRequest.SerializeToString,
response_deserializer=None,
)
return self._stubs["wait_operation"]

@property
def get_operation(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,27 @@ def post_list_operations(
"""
return response

def pre_wait_operation(
self, request: operations_pb2.WaitOperationRequest, metadata: Sequence[Tuple[str, str]]
) -> Tuple[operations_pb2.WaitOperationRequest, Sequence[Tuple[str, str]]]:
"""Pre-rpc interceptor for wait_operation
Override in a subclass to manipulate the request or metadata
before they are sent to the CloudRedis server.
"""
return request, metadata

def post_wait_operation(
self, response: operations_pb2.Operation
) -> operations_pb2.Operation:
"""Post-rpc interceptor for wait_operation
Override in a subclass to manipulate the response
after it is returned by the CloudRedis server but before
it is returned to user code.
"""
return response


@dataclasses.dataclass
class CloudRedisRestStub:
Expand Down Expand Up @@ -618,6 +639,13 @@ def operations_client(self) -> operations_v1.AbstractOperationsClient:
'uri': '/v1/{name=projects/*/locations/*}/operations',
},
],
'google.longrunning.Operations.WaitOperation': [
{
'method': 'post',
'uri': '/v2/{name=projects/*/locations/*/operations/*}:wait',
'body': '*',
},
],
}

rest_transport = operations_v1.OperationsRestTransport(
Expand Down Expand Up @@ -1968,6 +1996,82 @@ def __call__(self,
resp = self._interceptor.post_list_operations(resp)
return resp

@property
def wait_operation(self):
return self._WaitOperation(self._session, self._host, self._interceptor) # type: ignore

class _WaitOperation(_BaseCloudRedisRestTransport._BaseWaitOperation, CloudRedisRestStub):
def __hash__(self):
return hash("CloudRedisRestTransport.WaitOperation")

@staticmethod
def _get_response(
host,
metadata,
query_params,
session,
timeout,
transcoded_request,
body=None):

uri = transcoded_request['uri']
method = transcoded_request['method']
headers = dict(metadata)
headers['Content-Type'] = 'application/json'
response = getattr(session, method)(
"{host}{uri}".format(host=host, uri=uri),
timeout=timeout,
headers=headers,
params=rest_helpers.flatten_query_params(query_params, strict=True),
data=body,
)
return response

def __call__(self,
request: operations_pb2.WaitOperationRequest, *,
retry: OptionalRetry=gapic_v1.method.DEFAULT,
timeout: Optional[float]=None,
metadata: Sequence[Tuple[str, str]]=(),
) -> operations_pb2.Operation:

r"""Call the wait operation method over HTTP.
Args:
request (operations_pb2.WaitOperationRequest):
The request object for WaitOperation method.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
operations_pb2.Operation: Response from WaitOperation method.
"""

http_options = _BaseCloudRedisRestTransport._BaseWaitOperation._get_http_options()
request, metadata = self._interceptor.pre_wait_operation(request, metadata)
transcoded_request = _BaseCloudRedisRestTransport._BaseWaitOperation._get_transcoded_request(http_options, request)

body = _BaseCloudRedisRestTransport._BaseWaitOperation._get_request_body_json(transcoded_request)

# Jsonify the query params
query_params = _BaseCloudRedisRestTransport._BaseWaitOperation._get_query_params_json(transcoded_request)

# Send the request
response = CloudRedisRestTransport._WaitOperation._get_response(self._host, metadata, query_params, self._session, timeout, transcoded_request, body)

# In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception
# subclass.
if response.status_code >= 400:
raise core_exceptions.from_http_response(response)

content = response.content.decode("utf-8")
resp = operations_pb2.Operation()
resp = json_format.Parse(content, resp)
resp = self._interceptor.post_wait_operation(resp)
return resp

@property
def kind(self) -> str:
return "rest"
Expand Down
Loading

0 comments on commit 095d060

Please sign in to comment.