1# Copyright 2019, Google LLC All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from __future__ import absolute_import
16
17import typing
18from typing import Any, Callable, Union
19
20from google.cloud.pubsub_v1 import futures
21
22if typing.TYPE_CHECKING: # pragma: NO COVER
23 from google.cloud import pubsub_v1
24
25
26class Future(futures.Future):
27 """This future object is returned from asychronous Pub/Sub publishing
28 calls.
29
30 Calling :meth:`result` will resolve the future by returning the message
31 ID, unless an error occurs.
32 """
33
34 def cancel(self) -> bool:
35 """Actions in Pub/Sub generally may not be canceled.
36
37 This method always returns ``False``.
38 """
39 return False
40
41 def cancelled(self) -> bool:
42 """Actions in Pub/Sub generally may not be canceled.
43
44 This method always returns ``False``.
45 """
46 return False
47
48 def result(self, timeout: Union[int, float, None] = None) -> str:
49 """Return the message ID or raise an exception.
50
51 This blocks until the message has been published successfully and
52 returns the message ID unless an exception is raised.
53
54 Args:
55 timeout: The number of seconds before this call
56 times out and raises TimeoutError.
57
58 Returns:
59 The message ID.
60
61 Raises:
62 concurrent.futures.TimeoutError: If the request times out.
63 Exception: For undefined exceptions in the underlying
64 call execution.
65 """
66 return super().result(timeout=timeout)
67
68 # This exists to make the type checkers happy.
69 def add_done_callback(
70 self, callback: Callable[["pubsub_v1.publisher.futures.Future"], Any]
71 ) -> None:
72 """Attach a callable that will be called when the future finishes.
73
74 Args:
75 callback:
76 A callable that will be called with this future as its only
77 argument when the future completes or is cancelled. The callable
78 will always be called by a thread in the same process in which
79 it was added. If the future has already completed or been
80 cancelled then the callable will be called immediately. These
81 callables are called in the order that they were added.
82 """
83 return super().add_done_callback(callback) # type: ignore