Skip to content

Commit 5e6c365

Browse files
partheaohmayr
authored andcommitted
WIP Operations rest async client
1 parent acdadee commit 5e6c365

File tree

9 files changed

+1428
-430
lines changed

9 files changed

+1428
-430
lines changed

google/api_core/operations_v1/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
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
2018
from google.api_core.operations_v1.operations_async_client import OperationsAsyncClient
2119
from google.api_core.operations_v1.operations_client import OperationsClient
2220
from google.api_core.operations_v1.transports.rest import OperationsRestTransport
@@ -29,10 +27,14 @@
2927
]
3028

3129
try:
32-
from google.api_core.operations_v1.transports.rest_asyncio import OperationsRestAsyncTransport # noqa: F401
33-
__all__.append("OperationsRestAsyncTransport")
30+
from google.api_core.operations_v1.transports.rest_asyncio import (
31+
OperationsRestAsyncTransport,
32+
)
33+
from google.api_core.operations_v1.abstract_operations_async_client import AbstractOperationsAsyncClient
34+
35+
__all__.extend(["AbstractOperationsAsyncClient", "OperationsRestAsyncTransport"])
3436
except ImportError:
3537
# This import requires the `async_rest` extra
3638
# Don't raise an exception if `OperationsRestAsyncTransport` cannot be imported
37-
# as other transports are still available.
39+
# as other transports are still available
3840
pass
Lines changed: 331 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,331 @@
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 typing import Optional, Sequence, Tuple, Union
17+
18+
from google.api_core import client_options as client_options_lib # type: ignore
19+
from google.api_core import gapic_v1 # type: ignore
20+
from google.api_core.operations_v1 import pagers_async as pagers
21+
from google.api_core.operations_v1.transports.base import (
22+
DEFAULT_CLIENT_INFO,
23+
OperationsTransport,
24+
)
25+
from google.api_core.operations_v1.abstract_operations_base_client import (
26+
AbstractOperationsBaseClient,
27+
)
28+
29+
try:
30+
from google.auth.aio import credentials as ga_credentials # type: ignore
31+
except ImportError as e: # pragma: NO COVER
32+
raise ImportError(
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+
) from e
37+
38+
from google.auth.aio import credentials as ga_credentials # type: ignore
39+
from google.longrunning import operations_pb2
40+
41+
42+
class AbstractOperationsAsyncClient(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+
async def list_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+
if page_size is not None:
155+
request.page_size = page_size
156+
if page_token is not None:
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(metadata or ()) + (
166+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
167+
)
168+
169+
# Send the request.
170+
response = await rpc(
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+
return response
187+
188+
async def get_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(metadata or ()) + (
223+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
224+
)
225+
226+
# Send the request.
227+
response = await rpc(
228+
request,
229+
timeout=timeout,
230+
metadata=metadata,
231+
)
232+
233+
# Done; return the response.
234+
return response
235+
236+
async def delete_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(metadata or ()) + (
270+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
271+
)
272+
273+
# Send the request.
274+
await rpc(
275+
request,
276+
timeout=timeout,
277+
metadata=metadata,
278+
)
279+
280+
async def cancel_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(metadata or ()) + (
323+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
324+
)
325+
326+
# Send the request.
327+
await rpc(
328+
request,
329+
timeout=timeout,
330+
metadata=metadata,
331+
)

0 commit comments

Comments
 (0)