Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1492985

Browse files
committed
WIP Operations rest async client
1 parent 055a3ad commit 1492985

File tree

10 files changed

+1473
-422
lines changed

10 files changed

+1473
-422
lines changed

google/api_core/operations_v1/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414

1515
"""Package for interacting with the google.longrunning.operations meta-API."""
1616

17-
from google.api_core.operations_v1.abstract_operations_client import (
18-
AbstractOperationsClient,
19-
)
17+
from google.api_core.operations_v1.abstract_operations_client import AbstractOperationsClient
18+
from google.api_core.operations_v1.abstract_operations_async_client import AbstractOperationsAsyncClient
2019
from google.api_core.operations_v1.operations_async_client import OperationsAsyncClient
2120
from google.api_core.operations_v1.operations_client import OperationsClient
2221
from google.api_core.operations_v1.transports.rest import OperationsRestTransport
@@ -35,6 +34,7 @@
3534

3635
__all__ = [
3736
"AbstractOperationsClient",
37+
"AbstractOperationsAsyncClient",
3838
"OperationsAsyncClient",
3939
"OperationsClient",
4040
"OperationsRestTransport",
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
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+
from collections import OrderedDict
17+
import os
18+
import re
19+
from typing import Dict, Optional, Sequence, Tuple, Type, Union
20+
21+
from google.api_core import client_options as client_options_lib # type: ignore
22+
from google.api_core import gapic_v1 # type: ignore
23+
from google.api_core import retry_async as retries # type: ignore
24+
from google.api_core.operations_v1 import pagers_async as pagers
25+
from google.api_core.operations_v1.transports.base import (
26+
DEFAULT_CLIENT_INFO,
27+
OperationsTransport,
28+
)
29+
from google.api_core.operations_v1.abstract_operations_base_client import (
30+
AbstractOperationsBaseClient,
31+
)
32+
from google.api_core.operations_v1.transports.rest import OperationsRestTransport
33+
from google.auth import credentials as ga_credentials # type: ignore
34+
from google.auth.exceptions import MutualTLSChannelError # type: ignore
35+
from google.auth.transport import mtls # type: ignore
36+
from google.longrunning import operations_pb2
37+
from google.oauth2 import service_account # type: ignore
38+
import grpc
39+
40+
OptionalRetry = Union[retries.AsyncRetry, object]
41+
42+
43+
class AbstractOperationsAsyncClient(AbstractOperationsBaseClient):
44+
"""Manages long-running operations with an API service for the asynchronous client.
45+
46+
When an API method normally takes long time to complete, it can be
47+
designed to return [Operation][google.api_core.operations_v1.Operation] to the
48+
client, and the client can use this interface to receive the real
49+
response asynchronously by polling the operation resource, or pass
50+
the operation resource to another API (such as Google Cloud Pub/Sub
51+
API) to receive the response. Any API service that returns
52+
long-running operations should implement the ``Operations``
53+
interface so developers can have a consistent client experience.
54+
"""
55+
56+
def __init__(
57+
self,
58+
*,
59+
credentials: Optional[ga_credentials.Credentials] = None,
60+
transport: Union[str, OperationsTransport, None] = None,
61+
client_options: Optional[client_options_lib.ClientOptions] = None,
62+
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
63+
) -> None:
64+
"""Instantiates the operations client.
65+
66+
Args:
67+
credentials (Optional[google.auth.credentials.Credentials]): The
68+
authorization credentials to attach to requests. These
69+
credentials identify the application to the service; if none
70+
are specified, the client will attempt to ascertain the
71+
credentials from the environment.
72+
transport (Union[str, OperationsTransport]): The
73+
transport to use. If set to None, a transport is chosen
74+
automatically.
75+
client_options (google.api_core.client_options.ClientOptions): Custom options for the
76+
client. It won't take effect if a ``transport`` instance is provided.
77+
(1) The ``api_endpoint`` property can be used to override the
78+
default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
79+
environment variable can also be used to override the endpoint:
80+
"always" (always use the default mTLS endpoint), "never" (always
81+
use the default regular endpoint) and "auto" (auto switch to the
82+
default mTLS endpoint if client certificate is present, this is
83+
the default value). However, the ``api_endpoint`` property takes
84+
precedence if provided.
85+
(2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
86+
is "true", then the ``client_cert_source`` property can be used
87+
to provide client certificate for mutual TLS transport. If
88+
not provided, the default SSL client certificate will be used if
89+
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
90+
set, no client certificate will be used.
91+
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
92+
The client info used to send a user-agent string along with
93+
API requests. If ``None``, then default info will be used.
94+
Generally, you only need to set this if you're developing
95+
your own client library.
96+
97+
Raises:
98+
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
99+
creation failed for any reason.
100+
"""
101+
super().__init__(
102+
credentials=credentials,
103+
transport=transport,
104+
client_options=client_options,
105+
client_info=client_info,
106+
)
107+
108+
async def list_operations(
109+
self,
110+
name: str,
111+
filter_: Optional[str] = None,
112+
*,
113+
page_size: Optional[int] = None,
114+
page_token: Optional[str] = None,
115+
retry: OptionalRetry = gapic_v1.method.DEFAULT,
116+
timeout: Optional[float] = None,
117+
compression: Optional[grpc.Compression] = gapic_v1.method.DEFAULT,
118+
metadata: Sequence[Tuple[str, str]] = (),
119+
) -> pagers.ListOperationsAsyncPager:
120+
r"""Lists operations that match the specified filter in the request.
121+
If the server doesn't support this method, it returns
122+
``UNIMPLEMENTED``.
123+
124+
NOTE: the ``name`` binding allows API services to override the
125+
binding to use different resource name schemes, such as
126+
``users/*/operations``. To override the binding, API services
127+
can add a binding such as ``"/v1/{name=users/*}/operations"`` to
128+
their service configuration. For backwards compatibility, the
129+
default name includes the operations collection id, however
130+
overriding users must ensure the name binding is the parent
131+
resource, without the operations collection id.
132+
133+
Args:
134+
name (str):
135+
The name of the operation's parent
136+
resource.
137+
filter_ (str):
138+
The standard list filter.
139+
This corresponds to the ``filter`` field
140+
on the ``request`` instance; if ``request`` is provided, this
141+
should not be set.
142+
retry (google.api_core.retry_async.AsyncRetry): Designation of what errors, if any,
143+
should be retried.
144+
timeout (float): The timeout for this request.
145+
metadata (Sequence[Tuple[str, str]]): Strings which should be
146+
sent along with the request as metadata.
147+
148+
Returns:
149+
google.api_core.operations_v1.pagers.ListOperationsPager:
150+
The response message for
151+
[Operations.ListOperations][google.api_core.operations_v1.Operations.ListOperations].
152+
153+
Iterating over this object will yield results and
154+
resolve additional pages automatically.
155+
156+
"""
157+
# Create a protobuf request object.
158+
request = operations_pb2.ListOperationsRequest(name=name, filter=filter_)
159+
if page_size is not None:
160+
request.page_size = page_size
161+
if page_token is not None:
162+
request.page_token = page_token
163+
164+
# Wrap the RPC method; this adds retry and timeout information,
165+
# and friendly error handling.
166+
rpc = self._transport._wrapped_methods[self._transport.list_operations]
167+
168+
# Certain fields should be provided within the metadata header;
169+
# add these here.
170+
metadata = tuple(metadata or ()) + (
171+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
172+
)
173+
174+
# Send the request.
175+
response = await rpc(
176+
request,
177+
retry=retry,
178+
timeout=timeout,
179+
compression=compression,
180+
metadata=metadata,
181+
)
182+
183+
# This method is paged; wrap the response in a pager, which provides
184+
# an `__iter__` convenience method.
185+
response = pagers.ListOperationsAsyncPager(
186+
method=rpc,
187+
request=request,
188+
response=response,
189+
metadata=metadata,
190+
)
191+
192+
# Done; return the response.
193+
return response
194+
195+
async def get_operation(
196+
self,
197+
name: str,
198+
*,
199+
retry: OptionalRetry = gapic_v1.method.DEFAULT,
200+
timeout: Optional[float] = None,
201+
compression: Optional[grpc.Compression] = gapic_v1.method.DEFAULT,
202+
metadata: Sequence[Tuple[str, str]] = (),
203+
) -> operations_pb2.Operation:
204+
r"""Gets the latest state of a long-running operation.
205+
Clients can use this method to poll the operation result
206+
at intervals as recommended by the API service.
207+
208+
Args:
209+
name (str):
210+
The name of the operation resource.
211+
retry (google.api_core.retry.Retry): Designation of what errors, if any,
212+
should be retried.
213+
timeout (float): The timeout for this request.
214+
metadata (Sequence[Tuple[str, str]]): Strings which should be
215+
sent along with the request as metadata.
216+
217+
Returns:
218+
google.longrunning.operations_pb2.Operation:
219+
This resource represents a long-
220+
running operation that is the result of a
221+
network API call.
222+
223+
"""
224+
225+
request = operations_pb2.GetOperationRequest(name=name)
226+
227+
# Wrap the RPC method; this adds retry and timeout information,
228+
# and friendly error handling.
229+
rpc = self._transport._wrapped_methods[self._transport.get_operation]
230+
231+
# Certain fields should be provided within the metadata header;
232+
# add these here.
233+
metadata = tuple(metadata or ()) + (
234+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
235+
)
236+
237+
# Send the request.
238+
response = await rpc(
239+
request,
240+
retry=retry,
241+
timeout=timeout,
242+
compression=compression,
243+
metadata=metadata,
244+
)
245+
246+
# Done; return the response.
247+
return response
248+
249+
async def delete_operation(
250+
self,
251+
name: str,
252+
*,
253+
retry: OptionalRetry = gapic_v1.method.DEFAULT,
254+
timeout: Optional[float] = None,
255+
compression: Optional[grpc.Compression] = gapic_v1.method.DEFAULT,
256+
metadata: Sequence[Tuple[str, str]] = (),
257+
) -> None:
258+
r"""Deletes a long-running operation. This method indicates that the
259+
client is no longer interested in the operation result. It does
260+
not cancel the operation. If the server doesn't support this
261+
method, it returns ``google.rpc.Code.UNIMPLEMENTED``.
262+
263+
Args:
264+
name (str):
265+
The name of the operation resource to
266+
be deleted.
267+
268+
This corresponds to the ``name`` field
269+
on the ``request`` instance; if ``request`` is provided, this
270+
should not be set.
271+
retry (google.api_core.retry.Retry): Designation of what errors, if any,
272+
should be retried.
273+
timeout (float): The timeout for this request.
274+
metadata (Sequence[Tuple[str, str]]): Strings which should be
275+
sent along with the request as metadata.
276+
"""
277+
# Create the request object.
278+
request = operations_pb2.DeleteOperationRequest(name=name)
279+
280+
# Wrap the RPC method; this adds retry and timeout information,
281+
# and friendly error handling.
282+
rpc = self._transport._wrapped_methods[self._transport.delete_operation]
283+
284+
# Certain fields should be provided within the metadata header;
285+
# add these here.
286+
metadata = tuple(metadata or ()) + (
287+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
288+
)
289+
290+
# Send the request.
291+
await rpc(
292+
request,
293+
retry=retry,
294+
timeout=timeout,
295+
compression=compression,
296+
metadata=metadata,
297+
)
298+
299+
async def cancel_operation(
300+
self,
301+
name: Optional[str] = None,
302+
*,
303+
retry: OptionalRetry = gapic_v1.method.DEFAULT,
304+
timeout: Optional[float] = None,
305+
compression: Optional[grpc.Compression] = gapic_v1.method.DEFAULT,
306+
metadata: Sequence[Tuple[str, str]] = (),
307+
) -> None:
308+
r"""Starts asynchronous cancellation on a long-running operation.
309+
The server makes a best effort to cancel the operation, but
310+
success is not guaranteed. If the server doesn't support this
311+
method, it returns ``google.rpc.Code.UNIMPLEMENTED``. Clients
312+
can use
313+
[Operations.GetOperation][google.api_core.operations_v1.Operations.GetOperation]
314+
or other methods to check whether the cancellation succeeded or
315+
whether the operation completed despite cancellation. On
316+
successful cancellation, the operation is not deleted; instead,
317+
it becomes an operation with an
318+
[Operation.error][google.api_core.operations_v1.Operation.error] value with
319+
a [google.rpc.Status.code][google.rpc.Status.code] of 1,
320+
corresponding to ``Code.CANCELLED``.
321+
322+
Args:
323+
name (str):
324+
The name of the operation resource to
325+
be cancelled.
326+
327+
This corresponds to the ``name`` field
328+
on the ``request`` instance; if ``request`` is provided, this
329+
should not be set.
330+
retry (google.api_core.retry.Retry): Designation of what errors, if any,
331+
should be retried.
332+
timeout (float): The timeout for this request.
333+
metadata (Sequence[Tuple[str, str]]): Strings which should be
334+
sent along with the request as metadata.
335+
"""
336+
# Create the request object.
337+
request = operations_pb2.CancelOperationRequest(name=name)
338+
339+
# Wrap the RPC method; this adds retry and timeout information,
340+
# and friendly error handling.
341+
rpc = self._transport._wrapped_methods[self._transport.cancel_operation]
342+
343+
# Certain fields should be provided within the metadata header;
344+
# add these here.
345+
metadata = tuple(metadata or ()) + (
346+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
347+
)
348+
349+
# Send the request.
350+
await rpc(
351+
request,
352+
retry=retry,
353+
timeout=timeout,
354+
compression=compression,
355+
metadata=metadata,
356+
)

0 commit comments

Comments
 (0)