1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A client for the google.longrunning.operations meta-API.
This is a client that deals with long-running operations that follow the
pattern outlined by the `Google API Style Guide`_.
When an API method normally takes long time to complete, it can be designed to
return ``Operation`` to the client, and the client can use this interface to
receive the real response asynchronously by polling the operation resource to
receive the response.
It is not a separate service, but rather an interface implemented by a larger
service. The protocol-level definition is available at
`google/longrunning/operations.proto`_. Typically, this will be constructed
automatically by another client class to deal with operations.
.. _Google API Style Guide:
https://cloud.google.com/apis/design/design_pattern
s#long_running_operations
.. _google/longrunning/operations.proto:
https://github.com/googleapis/googleapis/blob/master/google/longrunning
/operations.proto
"""
import functools
from google.api_core import exceptions as core_exceptions
from google.api_core import gapic_v1
from google.api_core import page_iterator
from google.api_core import retry as retries
from google.api_core import timeout as timeouts
from google.longrunning import operations_pb2
from grpc import Compression
class OperationsClient(object):
"""Client for interacting with long-running operations within a service.
Args:
channel (grpc.Channel): The gRPC channel associated with the service
that implements the ``google.longrunning.operations`` interface.
client_config (dict):
A dictionary of call options for each method. If not specified
the default configuration is used.
"""
def __init__(self, channel, client_config=None):
# Create the gRPC client stub.
self.operations_stub = operations_pb2.OperationsStub(channel)
default_retry = retries.Retry(
initial=0.1, # seconds
maximum=60.0, # seconds
multiplier=1.3,
predicate=retries.if_exception_type(
core_exceptions.DeadlineExceeded,
core_exceptions.ServiceUnavailable,
),
timeout=600.0, # seconds
)
default_timeout = timeouts.TimeToDeadlineTimeout(timeout=600.0)
default_compression = Compression.NoCompression
self._get_operation = gapic_v1.method.wrap_method(
self.operations_stub.GetOperation,
default_retry=default_retry,
default_timeout=default_timeout,
default_compression=default_compression,
)
self._list_operations = gapic_v1.method.wrap_method(
self.operations_stub.ListOperations,
default_retry=default_retry,
default_timeout=default_timeout,
default_compression=default_compression,
)
self._cancel_operation = gapic_v1.method.wrap_method(
self.operations_stub.CancelOperation,
default_retry=default_retry,
default_timeout=default_timeout,
default_compression=default_compression,
)
self._delete_operation = gapic_v1.method.wrap_method(
self.operations_stub.DeleteOperation,
default_retry=default_retry,
default_timeout=default_timeout,
default_compression=default_compression,
)
# Service calls
def get_operation(
self,
name,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
compression=gapic_v1.method.DEFAULT,
metadata=None,
):
"""Gets the latest state of a long-running operation.
Clients can use this method to poll the operation result at intervals
as recommended by the API service.
Example:
>>> from google.api_core import operations_v1
>>> api = operations_v1.OperationsClient()
>>> name = ''
>>> response = api.get_operation(name)
Args:
name (str): The name of the operation resource.
retry (google.api_core.retry.Retry): The retry strategy to use
when invoking the RPC. If unspecified, the default retry from
the client configuration will be used. If ``None``, then this
method will not retry the RPC at all.
timeout (float): The amount of time in seconds to wait for the RPC
to complete. Note that if ``retry`` is used, this timeout
applies to each individual attempt and the overall time it
takes for this method to complete may be longer. If
unspecified, the the default timeout in the client
configuration is used. If ``None``, then the RPC method will
not time out.
compression (grpc.Compression): An element of grpc.compression
e.g. grpc.compression.Gzip.
metadata (Optional[List[Tuple[str, str]]]):
Additional gRPC metadata.
Returns:
google.longrunning.operations_pb2.Operation: The state of the
operation.
Raises:
google.api_core.exceptions.GoogleAPICallError: If an error occurred
while invoking the RPC, the appropriate ``GoogleAPICallError``
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,
compression=compression,
metadata=metadata,
)
def list_operations(
self,
name,
filter_,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
compression=gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Lists operations that match the specified filter in the request.
Example:
>>> from google.api_core import operations_v1
>>> api = operations_v1.OperationsClient()
>>> name = ''
>>>
>>> # Iterate over all results
>>> for operation in api.list_operations(name):
>>> # process operation
>>> pass
>>>
>>> # Or iterate over results one page at a time
>>> iter = api.list_operations(name)
>>> for page in iter.pages:
>>> for operation in page:
>>> # process operation
>>> pass
Args:
name (str): The name of the operation collection.
filter_ (str): The standard list filter.
retry (google.api_core.retry.Retry): The retry strategy to use
when invoking the RPC. If unspecified, the default retry from
the client configuration will be used. If ``None``, then this
method will not retry the RPC at all.
timeout (float): The amount of time in seconds to wait for the RPC
to complete. Note that if ``retry`` is used, this timeout
applies to each individual attempt and the overall time it
takes for this method to complete may be longer. If
unspecified, the the default timeout in the client
configuration is used. If ``None``, then the RPC method will
not time out.
compression (grpc.Compression): An element of grpc.compression
e.g. grpc.compression.Gzip.
metadata (Optional[List[Tuple[str, str]]]): Additional gRPC
metadata.
Returns:
google.api_core.page_iterator.Iterator: An iterator that yields
:class:`google.longrunning.operations_pb2.Operation` instances.
Raises:
google.api_core.exceptions.MethodNotImplemented: If the server
does not support this method. Services are not required to
implement this method.
google.api_core.exceptions.GoogleAPICallError: If an error occurred
while invoking the RPC, the appropriate ``GoogleAPICallError``
subclass will be raised.
"""
# 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,
compression=compression,
metadata=metadata,
)
iterator = page_iterator.GRPCIterator(
client=None,
method=method,
request=request,
items_field="operations",
request_token_field="page_token",
response_token_field="next_page_token",
)
return iterator
def cancel_operation(
self,
name,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
compression=gapic_v1.method.DEFAULT,
metadata=None,
):
"""Starts asynchronous cancellation on a long-running operation.
The server makes a best effort to cancel the operation, but success is
not guaranteed. Clients can use :meth:`get_operation` or service-
specific methods to check whether the cancellation succeeded or whether
the operation completed despite cancellation. On successful
cancellation, the operation is not deleted; instead, it becomes an
operation with an ``Operation.error`` value with a
``google.rpc.Status.code`` of ``1``, corresponding to
``Code.CANCELLED``.
Example:
>>> from google.api_core import operations_v1
>>> api = operations_v1.OperationsClient()
>>> name = ''
>>> api.cancel_operation(name)
Args:
name (str): The name of the operation resource to be cancelled.
retry (google.api_core.retry.Retry): The retry strategy to use
when invoking the RPC. If unspecified, the default retry from
the client configuration will be used. If ``None``, then this
method will not retry the RPC at all.
timeout (float): The amount of time in seconds to wait for the RPC
to complete. Note that if ``retry`` is used, this timeout
applies to each individual attempt and the overall time it
takes for this method to complete may be longer. If
unspecified, the the default timeout in the client
configuration is used. If ``None``, then the RPC method will
not time out.
compression (grpc.Compression): An element of grpc.compression
e.g. grpc.compression.Gzip.
metadata (Optional[List[Tuple[str, str]]]): Additional gRPC
metadata.
Raises:
google.api_core.exceptions.MethodNotImplemented: If the server
does not support this method. Services are not required to
implement this method.
google.api_core.exceptions.GoogleAPICallError: If an error occurred
while invoking the RPC, the appropriate ``GoogleAPICallError``
subclass will be raised.
"""
# 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,
compression=compression,
metadata=metadata,
)
def delete_operation(
self,
name,
retry=gapic_v1.method.DEFAULT,
timeout=gapic_v1.method.DEFAULT,
compression=gapic_v1.method.DEFAULT,
metadata=None,
):
"""Deletes a long-running operation.
This method indicates that the client is no longer interested in the
operation result. It does not cancel the operation.
Example:
>>> from google.api_core import operations_v1
>>> api = operations_v1.OperationsClient()
>>> name = ''
>>> api.delete_operation(name)
Args:
name (str): The name of the operation resource to be deleted.
retry (google.api_core.retry.Retry): The retry strategy to use
when invoking the RPC. If unspecified, the default retry from
the client configuration will be used. If ``None``, then this
method will not retry the RPC at all.
timeout (float): The amount of time in seconds to wait for the RPC
to complete. Note that if ``retry`` is used, this timeout
applies to each individual attempt and the overall time it
takes for this method to complete may be longer. If
unspecified, the the default timeout in the client
configuration is used. If ``None``, then the RPC method will
not time out.
compression (grpc.Compression): An element of grpc.compression
e.g. grpc.compression.Gzip.
metadata (Optional[List[Tuple[str, str]]]): Additional gRPC
metadata.
Raises:
google.api_core.exceptions.MethodNotImplemented: If the server
does not support this method. Services are not required to
implement this method.
google.api_core.exceptions.GoogleAPICallError: If an error occurred
while invoking the RPC, the appropriate ``GoogleAPICallError``
subclass will be raised.
"""
# 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,
compression=compression,
metadata=metadata,
)
|