|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2024 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +fromtypingimportOptional,Sequence,Tuple,Union |
| 17 | + |
| 18 | +fromgoogle.api_coreimportclient_optionsasclient_options_lib# type: ignore |
| 19 | +fromgoogle.api_coreimportgapic_v1# type: ignore |
| 20 | +fromgoogle.api_core.operations_v1importpagers_asyncaspagers |
| 21 | +fromgoogle.api_core.operations_v1.transports.baseimport ( |
| 22 | +DEFAULT_CLIENT_INFO, |
| 23 | +OperationsTransport, |
| 24 | +) |
| 25 | +fromgoogle.api_core.operations_v1.abstract_operations_base_clientimport ( |
| 26 | +AbstractOperationsBaseClient, |
| 27 | +) |
| 28 | + |
| 29 | +try: |
| 30 | +fromgoogle.auth.aioimportcredentialsasga_credentials# type: ignore |
| 31 | +exceptImportErrorase:# pragma: NO COVER |
| 32 | +raiseImportError( |
| 33 | +"`google-api-core[async_rest]` is required to use asynchronous rest streaming. " |
| 34 | +"Install the `async_rest` extra of `google-api-core` using " |
| 35 | +"`pip install google-api-core[async_rest]`." |
| 36 | + )frome |
| 37 | + |
| 38 | +fromgoogle.auth.aioimportcredentialsasga_credentials# type: ignore |
| 39 | +fromgoogle.longrunningimportoperations_pb2 |
| 40 | + |
| 41 | + |
| 42 | +classAbstractOperationsAsyncClient(AbstractOperationsBaseClient): |
| 43 | +"""Manages long-running operations with an API service for the asynchronous client. |
| 44 | +
|
| 45 | + When an API method normally takes long time to complete, it can be |
| 46 | + designed to return [Operation][google.api_core.operations_v1.Operation] to the |
| 47 | + client, and the client can use this interface to receive the real |
| 48 | + response asynchronously by polling the operation resource, or pass |
| 49 | + the operation resource to another API (such as Google Cloud Pub/Sub |
| 50 | + API) to receive the response. Any API service that returns |
| 51 | + long-running operations should implement the ``Operations`` |
| 52 | + interface so developers can have a consistent client experience. |
| 53 | + """ |
| 54 | + |
| 55 | +def__init__( |
| 56 | +self, |
| 57 | +*, |
| 58 | +credentials:Optional[ga_credentials.Credentials]=None, |
| 59 | +transport:Union[str,OperationsTransport,None]=None, |
| 60 | +client_options:Optional[client_options_lib.ClientOptions]=None, |
| 61 | +client_info:gapic_v1.client_info.ClientInfo=DEFAULT_CLIENT_INFO, |
| 62 | + )->None: |
| 63 | +"""Instantiates the operations client. |
| 64 | +
|
| 65 | + Args: |
| 66 | + credentials (Optional[google.auth.aio.credentials.Credentials]): The |
| 67 | + authorization credentials to attach to requests. These |
| 68 | + credentials identify the application to the service; if none |
| 69 | + are specified, the client will attempt to ascertain the |
| 70 | + credentials from the environment. |
| 71 | + transport (Union[str, OperationsTransport]): The |
| 72 | + transport to use. If set to None, a transport is chosen |
| 73 | + automatically. |
| 74 | + client_options (google.api_core.client_options.ClientOptions): Custom options for the |
| 75 | + client. It won't take effect if a ``transport`` instance is provided. |
| 76 | + (1) The ``api_endpoint`` property can be used to override the |
| 77 | + default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT |
| 78 | + environment variable can also be used to override the endpoint: |
| 79 | + "always" (always use the default mTLS endpoint), "never" (always |
| 80 | + use the default regular endpoint) and "auto" (auto switch to the |
| 81 | + default mTLS endpoint if client certificate is present, this is |
| 82 | + the default value). However, the ``api_endpoint`` property takes |
| 83 | + precedence if provided. |
| 84 | + (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable |
| 85 | + is "true", then the ``client_cert_source`` property can be used |
| 86 | + to provide client certificate for mutual TLS transport. If |
| 87 | + not provided, the default SSL client certificate will be used if |
| 88 | + present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not |
| 89 | + set, no client certificate will be used. |
| 90 | + client_info (google.api_core.gapic_v1.client_info.ClientInfo): |
| 91 | + The client info used to send a user-agent string along with |
| 92 | + API requests. If ``None``, then default info will be used. |
| 93 | + Generally, you only need to set this if you're developing |
| 94 | + your own client library. |
| 95 | +
|
| 96 | + Raises: |
| 97 | + google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport |
| 98 | + creation failed for any reason. |
| 99 | + """ |
| 100 | +super().__init__( |
| 101 | +credentials=credentials,# type: ignore |
| 102 | +transport=transport, |
| 103 | +client_options=client_options, |
| 104 | +client_info=client_info, |
| 105 | + ) |
| 106 | + |
| 107 | +asyncdeflist_operations( |
| 108 | +self, |
| 109 | +name:str, |
| 110 | +filter_:Optional[str]=None, |
| 111 | +*, |
| 112 | +page_size:Optional[int]=None, |
| 113 | +page_token:Optional[str]=None, |
| 114 | +timeout:Optional[float]=None, |
| 115 | +metadata:Sequence[Tuple[str,str]]= (), |
| 116 | + )->pagers.ListOperationsAsyncPager: |
| 117 | +r"""Lists operations that match the specified filter in the request. |
| 118 | + If the server doesn't support this method, it returns |
| 119 | + ``UNIMPLEMENTED``. |
| 120 | +
|
| 121 | + NOTE: the ``name`` binding allows API services to override the |
| 122 | + binding to use different resource name schemes, such as |
| 123 | + ``users/*/operations``. To override the binding, API services |
| 124 | + can add a binding such as ``"/v1/{name=users/*}/operations"`` to |
| 125 | + their service configuration. For backwards compatibility, the |
| 126 | + default name includes the operations collection id, however |
| 127 | + overriding users must ensure the name binding is the parent |
| 128 | + resource, without the operations collection id. |
| 129 | +
|
| 130 | + Args: |
| 131 | + name (str): |
| 132 | + The name of the operation's parent |
| 133 | + resource. |
| 134 | + filter_ (str): |
| 135 | + The standard list filter. |
| 136 | + This corresponds to the ``filter`` field |
| 137 | + on the ``request`` instance; if ``request`` is provided, this |
| 138 | + should not be set. |
| 139 | + timeout (float): The timeout for this request. |
| 140 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 141 | + sent along with the request as metadata. |
| 142 | +
|
| 143 | + Returns: |
| 144 | + google.api_core.operations_v1.pagers.ListOperationsPager: |
| 145 | + The response message for |
| 146 | + [Operations.ListOperations][google.api_core.operations_v1.Operations.ListOperations]. |
| 147 | +
|
| 148 | + Iterating over this object will yield results and |
| 149 | + resolve additional pages automatically. |
| 150 | +
|
| 151 | + """ |
| 152 | +# Create a protobuf request object. |
| 153 | +request=operations_pb2.ListOperationsRequest(name=name,filter=filter_) |
| 154 | +ifpage_sizeisnotNone: |
| 155 | +request.page_size=page_size |
| 156 | +ifpage_tokenisnotNone: |
| 157 | +request.page_token=page_token |
| 158 | + |
| 159 | +# Wrap the RPC method; this adds retry and timeout information, |
| 160 | +# and friendly error handling. |
| 161 | +rpc=self._transport._wrapped_methods[self._transport.list_operations] |
| 162 | + |
| 163 | +# Certain fields should be provided within the metadata header; |
| 164 | +# add these here. |
| 165 | +metadata=tuple(metadataor ())+ ( |
| 166 | +gapic_v1.routing_header.to_grpc_metadata((("name",request.name),)), |
| 167 | + ) |
| 168 | + |
| 169 | +# Send the request. |
| 170 | +response=awaitrpc( |
| 171 | +request, |
| 172 | +timeout=timeout, |
| 173 | +metadata=metadata, |
| 174 | + ) |
| 175 | + |
| 176 | +# This method is paged; wrap the response in a pager, which provides |
| 177 | +# an `__iter__` convenience method. |
| 178 | +response=pagers.ListOperationsAsyncPager( |
| 179 | +method=rpc, |
| 180 | +request=request, |
| 181 | +response=response, |
| 182 | +metadata=metadata, |
| 183 | + ) |
| 184 | + |
| 185 | +# Done; return the response. |
| 186 | +returnresponse |
| 187 | + |
| 188 | +asyncdefget_operation( |
| 189 | +self, |
| 190 | +name:str, |
| 191 | +*, |
| 192 | +timeout:Optional[float]=None, |
| 193 | +metadata:Sequence[Tuple[str,str]]= (), |
| 194 | + )->operations_pb2.Operation: |
| 195 | +r"""Gets the latest state of a long-running operation. |
| 196 | + Clients can use this method to poll the operation result |
| 197 | + at intervals as recommended by the API service. |
| 198 | +
|
| 199 | + Args: |
| 200 | + name (str): |
| 201 | + The name of the operation resource. |
| 202 | + timeout (float): The timeout for this request. |
| 203 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 204 | + sent along with the request as metadata. |
| 205 | +
|
| 206 | + Returns: |
| 207 | + google.longrunning.operations_pb2.Operation: |
| 208 | + This resource represents a long- |
| 209 | + running operation that is the result of a |
| 210 | + network API call. |
| 211 | +
|
| 212 | + """ |
| 213 | + |
| 214 | +request=operations_pb2.GetOperationRequest(name=name) |
| 215 | + |
| 216 | +# Wrap the RPC method; this adds retry and timeout information, |
| 217 | +# and friendly error handling. |
| 218 | +rpc=self._transport._wrapped_methods[self._transport.get_operation] |
| 219 | + |
| 220 | +# Certain fields should be provided within the metadata header; |
| 221 | +# add these here. |
| 222 | +metadata=tuple(metadataor ())+ ( |
| 223 | +gapic_v1.routing_header.to_grpc_metadata((("name",request.name),)), |
| 224 | + ) |
| 225 | + |
| 226 | +# Send the request. |
| 227 | +response=awaitrpc( |
| 228 | +request, |
| 229 | +timeout=timeout, |
| 230 | +metadata=metadata, |
| 231 | + ) |
| 232 | + |
| 233 | +# Done; return the response. |
| 234 | +returnresponse |
| 235 | + |
| 236 | +asyncdefdelete_operation( |
| 237 | +self, |
| 238 | +name:str, |
| 239 | +*, |
| 240 | +timeout:Optional[float]=None, |
| 241 | +metadata:Sequence[Tuple[str,str]]= (), |
| 242 | + )->None: |
| 243 | +r"""Deletes a long-running operation. This method indicates that the |
| 244 | + client is no longer interested in the operation result. It does |
| 245 | + not cancel the operation. If the server doesn't support this |
| 246 | + method, it returns ``google.rpc.Code.UNIMPLEMENTED``. |
| 247 | +
|
| 248 | + Args: |
| 249 | + name (str): |
| 250 | + The name of the operation resource to |
| 251 | + be deleted. |
| 252 | +
|
| 253 | + This corresponds to the ``name`` field |
| 254 | + on the ``request`` instance; if ``request`` is provided, this |
| 255 | + should not be set. |
| 256 | + timeout (float): The timeout for this request. |
| 257 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 258 | + sent along with the request as metadata. |
| 259 | + """ |
| 260 | +# Create the request object. |
| 261 | +request=operations_pb2.DeleteOperationRequest(name=name) |
| 262 | + |
| 263 | +# Wrap the RPC method; this adds retry and timeout information, |
| 264 | +# and friendly error handling. |
| 265 | +rpc=self._transport._wrapped_methods[self._transport.delete_operation] |
| 266 | + |
| 267 | +# Certain fields should be provided within the metadata header; |
| 268 | +# add these here. |
| 269 | +metadata=tuple(metadataor ())+ ( |
| 270 | +gapic_v1.routing_header.to_grpc_metadata((("name",request.name),)), |
| 271 | + ) |
| 272 | + |
| 273 | +# Send the request. |
| 274 | +awaitrpc( |
| 275 | +request, |
| 276 | +timeout=timeout, |
| 277 | +metadata=metadata, |
| 278 | + ) |
| 279 | + |
| 280 | +asyncdefcancel_operation( |
| 281 | +self, |
| 282 | +name:Optional[str]=None, |
| 283 | +*, |
| 284 | +timeout:Optional[float]=None, |
| 285 | +metadata:Sequence[Tuple[str,str]]= (), |
| 286 | + )->None: |
| 287 | +r"""Starts asynchronous cancellation on a long-running operation. |
| 288 | + The server makes a best effort to cancel the operation, but |
| 289 | + success is not guaranteed. If the server doesn't support this |
| 290 | + method, it returns ``google.rpc.Code.UNIMPLEMENTED``. Clients |
| 291 | + can use |
| 292 | + [Operations.GetOperation][google.api_core.operations_v1.Operations.GetOperation] |
| 293 | + or other methods to check whether the cancellation succeeded or |
| 294 | + whether the operation completed despite cancellation. On |
| 295 | + successful cancellation, the operation is not deleted; instead, |
| 296 | + it becomes an operation with an |
| 297 | + [Operation.error][google.api_core.operations_v1.Operation.error] value with |
| 298 | + a [google.rpc.Status.code][google.rpc.Status.code] of 1, |
| 299 | + corresponding to ``Code.CANCELLED``. |
| 300 | +
|
| 301 | + Args: |
| 302 | + name (str): |
| 303 | + The name of the operation resource to |
| 304 | + be cancelled. |
| 305 | +
|
| 306 | + This corresponds to the ``name`` field |
| 307 | + on the ``request`` instance; if ``request`` is provided, this |
| 308 | + should not be set. |
| 309 | + timeout (float): The timeout for this request. |
| 310 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 311 | + sent along with the request as metadata. |
| 312 | + """ |
| 313 | +# Create the request object. |
| 314 | +request=operations_pb2.CancelOperationRequest(name=name) |
| 315 | + |
| 316 | +# Wrap the RPC method; this adds retry and timeout information, |
| 317 | +# and friendly error handling. |
| 318 | +rpc=self._transport._wrapped_methods[self._transport.cancel_operation] |
| 319 | + |
| 320 | +# Certain fields should be provided within the metadata header; |
| 321 | +# add these here. |
| 322 | +metadata=tuple(metadataor ())+ ( |
| 323 | +gapic_v1.routing_header.to_grpc_metadata((("name",request.name),)), |
| 324 | + ) |
| 325 | + |
| 326 | +# Send the request. |
| 327 | +awaitrpc( |
| 328 | +request, |
| 329 | +timeout=timeout, |
| 330 | +metadata=metadata, |
| 331 | + ) |