Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/api_core/operations_v1/transports/base.py: 59%

49 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:45 +0000

1# -*- coding: utf-8 -*- 

2# Copyright 2020 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# 

16import abc 

17from typing import Awaitable, Callable, Optional, Sequence, Union 

18 

19import google.api_core # type: ignore 

20from google.api_core import exceptions as core_exceptions # type: ignore 

21from google.api_core import gapic_v1 # type: ignore 

22from google.api_core import retry as retries # type: ignore 

23from google.api_core import version 

24import google.auth # type: ignore 

25from google.auth import credentials as ga_credentials # type: ignore 

26from google.longrunning import operations_pb2 

27from google.oauth2 import service_account # type: ignore 

28from google.protobuf import empty_pb2 # type: ignore 

29from grpc import Compression 

30 

31 

32DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

33 gapic_version=version.__version__, 

34) 

35 

36 

37class OperationsTransport(abc.ABC): 

38 """Abstract transport class for Operations.""" 

39 

40 AUTH_SCOPES = () 

41 

42 DEFAULT_HOST: str = "longrunning.googleapis.com" 

43 

44 def __init__( 

45 self, 

46 *, 

47 host: str = DEFAULT_HOST, 

48 credentials: ga_credentials.Credentials = None, 

49 credentials_file: Optional[str] = None, 

50 scopes: Optional[Sequence[str]] = None, 

51 quota_project_id: Optional[str] = None, 

52 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

53 always_use_jwt_access: Optional[bool] = False, 

54 **kwargs, 

55 ) -> None: 

56 """Instantiate the transport. 

57 

58 Args: 

59 host (Optional[str]): 

60 The hostname to connect to. 

61 credentials (Optional[google.auth.credentials.Credentials]): The 

62 authorization credentials to attach to requests. These 

63 credentials identify the application to the service; if none 

64 are specified, the client will attempt to ascertain the 

65 credentials from the environment. 

66 credentials_file (Optional[str]): A file with credentials that can 

67 be loaded with :func:`google.auth.load_credentials_from_file`. 

68 This argument is mutually exclusive with credentials. 

69 scopes (Optional[Sequence[str]]): A list of scopes. 

70 quota_project_id (Optional[str]): An optional project to use for billing 

71 and quota. 

72 client_info (google.api_core.gapic_v1.client_info.ClientInfo): 

73 The client info used to send a user-agent string along with 

74 API requests. If ``None``, then default info will be used. 

75 Generally, you only need to set this if you're developing 

76 your own client library. 

77 always_use_jwt_access (Optional[bool]): Whether self signed JWT should 

78 be used for service account credentials. 

79 """ 

80 # Save the hostname. Default to port 443 (HTTPS) if none is specified. 

81 if ":" not in host: 

82 host += ":443" 

83 self._host = host 

84 

85 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES} 

86 

87 # Save the scopes. 

88 self._scopes = scopes 

89 

90 # If no credentials are provided, then determine the appropriate 

91 # defaults. 

92 if credentials and credentials_file: 

93 raise core_exceptions.DuplicateCredentialArgs( 

94 "'credentials_file' and 'credentials' are mutually exclusive" 

95 ) 

96 

97 if credentials_file is not None: 

98 credentials, _ = google.auth.load_credentials_from_file( 

99 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 

100 ) 

101 

102 elif credentials is None: 

103 credentials, _ = google.auth.default( 

104 **scopes_kwargs, quota_project_id=quota_project_id 

105 ) 

106 

107 # If the credentials are service account credentials, then always try to use self signed JWT. 

108 if ( 

109 always_use_jwt_access 

110 and isinstance(credentials, service_account.Credentials) 

111 and hasattr(service_account.Credentials, "with_always_use_jwt_access") 

112 ): 

113 credentials = credentials.with_always_use_jwt_access(True) 

114 

115 # Save the credentials. 

116 self._credentials = credentials 

117 

118 def _prep_wrapped_messages(self, client_info): 

119 # Precompute the wrapped methods. 

120 self._wrapped_methods = { 

121 self.list_operations: gapic_v1.method.wrap_method( 

122 self.list_operations, 

123 default_retry=retries.Retry( 

124 initial=0.5, 

125 maximum=10.0, 

126 multiplier=2.0, 

127 predicate=retries.if_exception_type( 

128 core_exceptions.ServiceUnavailable, 

129 ), 

130 deadline=10.0, 

131 ), 

132 default_timeout=10.0, 

133 default_compression=Compression.NoCompression, 

134 client_info=client_info, 

135 ), 

136 self.get_operation: gapic_v1.method.wrap_method( 

137 self.get_operation, 

138 default_retry=retries.Retry( 

139 initial=0.5, 

140 maximum=10.0, 

141 multiplier=2.0, 

142 predicate=retries.if_exception_type( 

143 core_exceptions.ServiceUnavailable, 

144 ), 

145 deadline=10.0, 

146 ), 

147 default_timeout=10.0, 

148 default_compression=Compression.NoCompression, 

149 client_info=client_info, 

150 ), 

151 self.delete_operation: gapic_v1.method.wrap_method( 

152 self.delete_operation, 

153 default_retry=retries.Retry( 

154 initial=0.5, 

155 maximum=10.0, 

156 multiplier=2.0, 

157 predicate=retries.if_exception_type( 

158 core_exceptions.ServiceUnavailable, 

159 ), 

160 deadline=10.0, 

161 ), 

162 default_timeout=10.0, 

163 default_compression=Compression.NoCompression, 

164 client_info=client_info, 

165 ), 

166 self.cancel_operation: gapic_v1.method.wrap_method( 

167 self.cancel_operation, 

168 default_retry=retries.Retry( 

169 initial=0.5, 

170 maximum=10.0, 

171 multiplier=2.0, 

172 predicate=retries.if_exception_type( 

173 core_exceptions.ServiceUnavailable, 

174 ), 

175 deadline=10.0, 

176 ), 

177 default_timeout=10.0, 

178 default_compression=Compression.NoCompression, 

179 client_info=client_info, 

180 ), 

181 } 

182 

183 def close(self): 

184 """Closes resources associated with the transport. 

185 

186 .. warning:: 

187 Only call this method if the transport is NOT shared 

188 with other clients - this may cause errors in other clients! 

189 """ 

190 raise NotImplementedError() 

191 

192 @property 

193 def list_operations( 

194 self, 

195 ) -> Callable[ 

196 [operations_pb2.ListOperationsRequest], 

197 Union[ 

198 operations_pb2.ListOperationsResponse, 

199 Awaitable[operations_pb2.ListOperationsResponse], 

200 ], 

201 ]: 

202 raise NotImplementedError() 

203 

204 @property 

205 def get_operation( 

206 self, 

207 ) -> Callable[ 

208 [operations_pb2.GetOperationRequest], 

209 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], 

210 ]: 

211 raise NotImplementedError() 

212 

213 @property 

214 def delete_operation( 

215 self, 

216 ) -> Callable[ 

217 [operations_pb2.DeleteOperationRequest], 

218 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]], 

219 ]: 

220 raise NotImplementedError() 

221 

222 @property 

223 def cancel_operation( 

224 self, 

225 ) -> Callable[ 

226 [operations_pb2.CancelOperationRequest], 

227 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]], 

228 ]: 

229 raise NotImplementedError() 

230 

231 

232__all__ = ("OperationsTransport",)