Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/errorreporting_v1beta1/services/error_stats_service/transports/grpc.py: 42%

64 statements  

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

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

2# Copyright 2023 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 warnings 

17from typing import Callable, Dict, Optional, Sequence, Tuple, Union 

18 

19from google.api_core import grpc_helpers 

20from google.api_core import gapic_v1 

21import google.auth # type: ignore 

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

23from google.auth.transport.grpc import SslCredentials # type: ignore 

24 

25import grpc # type: ignore 

26 

27from google.cloud.errorreporting_v1beta1.types import error_stats_service 

28from .base import ErrorStatsServiceTransport, DEFAULT_CLIENT_INFO 

29 

30 

31class ErrorStatsServiceGrpcTransport(ErrorStatsServiceTransport): 

32 """gRPC backend transport for ErrorStatsService. 

33 

34 An API for retrieving and managing error statistics as well 

35 as data for individual events. 

36 

37 This class defines the same methods as the primary client, so the 

38 primary client can load the underlying transport implementation 

39 and call it. 

40 

41 It sends protocol buffers over the wire using gRPC (which is built on 

42 top of HTTP/2); the ``grpcio`` package must be installed. 

43 """ 

44 

45 _stubs: Dict[str, Callable] 

46 

47 def __init__( 

48 self, 

49 *, 

50 host: str = "clouderrorreporting.googleapis.com", 

51 credentials: Optional[ga_credentials.Credentials] = None, 

52 credentials_file: Optional[str] = None, 

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

54 channel: Optional[grpc.Channel] = None, 

55 api_mtls_endpoint: Optional[str] = None, 

56 client_cert_source: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

57 ssl_channel_credentials: Optional[grpc.ChannelCredentials] = None, 

58 client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, 

59 quota_project_id: Optional[str] = None, 

60 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

61 always_use_jwt_access: Optional[bool] = False, 

62 api_audience: Optional[str] = None, 

63 ) -> None: 

64 """Instantiate the transport. 

65 

66 Args: 

67 host (Optional[str]): 

68 The hostname to connect to. 

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

70 authorization credentials to attach to requests. These 

71 credentials identify the application to the service; if none 

72 are specified, the client will attempt to ascertain the 

73 credentials from the environment. 

74 This argument is ignored if ``channel`` is provided. 

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

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

77 This argument is ignored if ``channel`` is provided. 

78 scopes (Optional(Sequence[str])): A list of scopes. This argument is 

79 ignored if ``channel`` is provided. 

80 channel (Optional[grpc.Channel]): A ``Channel`` instance through 

81 which to make calls. 

82 api_mtls_endpoint (Optional[str]): Deprecated. The mutual TLS endpoint. 

83 If provided, it overrides the ``host`` argument and tries to create 

84 a mutual TLS channel with client SSL credentials from 

85 ``client_cert_source`` or application default SSL credentials. 

86 client_cert_source (Optional[Callable[[], Tuple[bytes, bytes]]]): 

87 Deprecated. A callback to provide client SSL certificate bytes and 

88 private key bytes, both in PEM format. It is ignored if 

89 ``api_mtls_endpoint`` is None. 

90 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials 

91 for the grpc channel. It is ignored if ``channel`` is provided. 

92 client_cert_source_for_mtls (Optional[Callable[[], Tuple[bytes, bytes]]]): 

93 A callback to provide client certificate bytes and private key bytes, 

94 both in PEM format. It is used to configure a mutual TLS channel. It is 

95 ignored if ``channel`` or ``ssl_channel_credentials`` is provided. 

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

97 and quota. 

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

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

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

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

102 your own client library. 

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

104 be used for service account credentials. 

105 

106 Raises: 

107 google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport 

108 creation failed for any reason. 

109 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

110 and ``credentials_file`` are passed. 

111 """ 

112 self._grpc_channel = None 

113 self._ssl_channel_credentials = ssl_channel_credentials 

114 self._stubs: Dict[str, Callable] = {} 

115 

116 if api_mtls_endpoint: 

117 warnings.warn("api_mtls_endpoint is deprecated", DeprecationWarning) 

118 if client_cert_source: 

119 warnings.warn("client_cert_source is deprecated", DeprecationWarning) 

120 

121 if channel: 

122 # Ignore credentials if a channel was passed. 

123 credentials = False 

124 # If a channel was explicitly provided, set it. 

125 self._grpc_channel = channel 

126 self._ssl_channel_credentials = None 

127 

128 else: 

129 if api_mtls_endpoint: 

130 host = api_mtls_endpoint 

131 

132 # Create SSL credentials with client_cert_source or application 

133 # default SSL credentials. 

134 if client_cert_source: 

135 cert, key = client_cert_source() 

136 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

137 certificate_chain=cert, private_key=key 

138 ) 

139 else: 

140 self._ssl_channel_credentials = SslCredentials().ssl_credentials 

141 

142 else: 

143 if client_cert_source_for_mtls and not ssl_channel_credentials: 

144 cert, key = client_cert_source_for_mtls() 

145 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

146 certificate_chain=cert, private_key=key 

147 ) 

148 

149 # The base transport sets the host, credentials and scopes 

150 super().__init__( 

151 host=host, 

152 credentials=credentials, 

153 credentials_file=credentials_file, 

154 scopes=scopes, 

155 quota_project_id=quota_project_id, 

156 client_info=client_info, 

157 always_use_jwt_access=always_use_jwt_access, 

158 api_audience=api_audience, 

159 ) 

160 

161 if not self._grpc_channel: 

162 self._grpc_channel = type(self).create_channel( 

163 self._host, 

164 # use the credentials which are saved 

165 credentials=self._credentials, 

166 # Set ``credentials_file`` to ``None`` here as 

167 # the credentials that we saved earlier should be used. 

168 credentials_file=None, 

169 scopes=self._scopes, 

170 ssl_credentials=self._ssl_channel_credentials, 

171 quota_project_id=quota_project_id, 

172 options=[ 

173 ("grpc.max_send_message_length", -1), 

174 ("grpc.max_receive_message_length", -1), 

175 ], 

176 ) 

177 

178 # Wrap messages. This must be done after self._grpc_channel exists 

179 self._prep_wrapped_messages(client_info) 

180 

181 @classmethod 

182 def create_channel( 

183 cls, 

184 host: str = "clouderrorreporting.googleapis.com", 

185 credentials: Optional[ga_credentials.Credentials] = None, 

186 credentials_file: Optional[str] = None, 

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

188 quota_project_id: Optional[str] = None, 

189 **kwargs, 

190 ) -> grpc.Channel: 

191 """Create and return a gRPC channel object. 

192 Args: 

193 host (Optional[str]): The host for the channel to use. 

194 credentials (Optional[~.Credentials]): The 

195 authorization credentials to attach to requests. These 

196 credentials identify this application to the service. If 

197 none are specified, the client will attempt to ascertain 

198 the credentials from the environment. 

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

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

201 This argument is mutually exclusive with credentials. 

202 scopes (Optional[Sequence[str]]): A optional list of scopes needed for this 

203 service. These are only used when credentials are not specified and 

204 are passed to :func:`google.auth.default`. 

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

206 and quota. 

207 kwargs (Optional[dict]): Keyword arguments, which are passed to the 

208 channel creation. 

209 Returns: 

210 grpc.Channel: A gRPC channel object. 

211 

212 Raises: 

213 google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials`` 

214 and ``credentials_file`` are passed. 

215 """ 

216 

217 return grpc_helpers.create_channel( 

218 host, 

219 credentials=credentials, 

220 credentials_file=credentials_file, 

221 quota_project_id=quota_project_id, 

222 default_scopes=cls.AUTH_SCOPES, 

223 scopes=scopes, 

224 default_host=cls.DEFAULT_HOST, 

225 **kwargs, 

226 ) 

227 

228 @property 

229 def grpc_channel(self) -> grpc.Channel: 

230 """Return the channel designed to connect to this service.""" 

231 return self._grpc_channel 

232 

233 @property 

234 def list_group_stats( 

235 self, 

236 ) -> Callable[ 

237 [error_stats_service.ListGroupStatsRequest], 

238 error_stats_service.ListGroupStatsResponse, 

239 ]: 

240 r"""Return a callable for the list group stats method over gRPC. 

241 

242 Lists the specified groups. 

243 

244 Returns: 

245 Callable[[~.ListGroupStatsRequest], 

246 ~.ListGroupStatsResponse]: 

247 A function that, when called, will call the underlying RPC 

248 on the server. 

249 """ 

250 # Generate a "stub function" on-the-fly which will actually make 

251 # the request. 

252 # gRPC handles serialization and deserialization, so we just need 

253 # to pass in the functions for each. 

254 if "list_group_stats" not in self._stubs: 

255 self._stubs["list_group_stats"] = self.grpc_channel.unary_unary( 

256 "/google.devtools.clouderrorreporting.v1beta1.ErrorStatsService/ListGroupStats", 

257 request_serializer=error_stats_service.ListGroupStatsRequest.serialize, 

258 response_deserializer=error_stats_service.ListGroupStatsResponse.deserialize, 

259 ) 

260 return self._stubs["list_group_stats"] 

261 

262 @property 

263 def list_events( 

264 self, 

265 ) -> Callable[ 

266 [error_stats_service.ListEventsRequest], error_stats_service.ListEventsResponse 

267 ]: 

268 r"""Return a callable for the list events method over gRPC. 

269 

270 Lists the specified events. 

271 

272 Returns: 

273 Callable[[~.ListEventsRequest], 

274 ~.ListEventsResponse]: 

275 A function that, when called, will call the underlying RPC 

276 on the server. 

277 """ 

278 # Generate a "stub function" on-the-fly which will actually make 

279 # the request. 

280 # gRPC handles serialization and deserialization, so we just need 

281 # to pass in the functions for each. 

282 if "list_events" not in self._stubs: 

283 self._stubs["list_events"] = self.grpc_channel.unary_unary( 

284 "/google.devtools.clouderrorreporting.v1beta1.ErrorStatsService/ListEvents", 

285 request_serializer=error_stats_service.ListEventsRequest.serialize, 

286 response_deserializer=error_stats_service.ListEventsResponse.deserialize, 

287 ) 

288 return self._stubs["list_events"] 

289 

290 @property 

291 def delete_events( 

292 self, 

293 ) -> Callable[ 

294 [error_stats_service.DeleteEventsRequest], 

295 error_stats_service.DeleteEventsResponse, 

296 ]: 

297 r"""Return a callable for the delete events method over gRPC. 

298 

299 Deletes all error events of a given project. 

300 

301 Returns: 

302 Callable[[~.DeleteEventsRequest], 

303 ~.DeleteEventsResponse]: 

304 A function that, when called, will call the underlying RPC 

305 on the server. 

306 """ 

307 # Generate a "stub function" on-the-fly which will actually make 

308 # the request. 

309 # gRPC handles serialization and deserialization, so we just need 

310 # to pass in the functions for each. 

311 if "delete_events" not in self._stubs: 

312 self._stubs["delete_events"] = self.grpc_channel.unary_unary( 

313 "/google.devtools.clouderrorreporting.v1beta1.ErrorStatsService/DeleteEvents", 

314 request_serializer=error_stats_service.DeleteEventsRequest.serialize, 

315 response_deserializer=error_stats_service.DeleteEventsResponse.deserialize, 

316 ) 

317 return self._stubs["delete_events"] 

318 

319 def close(self): 

320 self.grpc_channel.close() 

321 

322 @property 

323 def kind(self) -> str: 

324 return "grpc" 

325 

326 

327__all__ = ("ErrorStatsServiceGrpcTransport",)