Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/errorreporting_v1beta1/services/error_group_service/transports/rest.py: 47%

116 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 07:30 +0000

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

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

17from google.auth.transport.requests import AuthorizedSession # type: ignore 

18import json # type: ignore 

19import grpc # type: ignore 

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

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

22from google.api_core import exceptions as core_exceptions 

23from google.api_core import retry as retries 

24from google.api_core import rest_helpers 

25from google.api_core import rest_streaming 

26from google.api_core import path_template 

27from google.api_core import gapic_v1 

28 

29from google.protobuf import json_format 

30from requests import __version__ as requests_version 

31import dataclasses 

32import re 

33from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union 

34import warnings 

35 

36try: 

37 OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] 

38except AttributeError: # pragma: NO COVER 

39 OptionalRetry = Union[retries.Retry, object] # type: ignore 

40 

41 

42from google.cloud.errorreporting_v1beta1.types import common 

43from google.cloud.errorreporting_v1beta1.types import error_group_service 

44 

45from .base import ( 

46 ErrorGroupServiceTransport, 

47 DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO, 

48) 

49 

50 

51DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

52 gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, 

53 grpc_version=None, 

54 rest_version=requests_version, 

55) 

56 

57 

58class ErrorGroupServiceRestInterceptor: 

59 """Interceptor for ErrorGroupService. 

60 

61 Interceptors are used to manipulate requests, request metadata, and responses 

62 in arbitrary ways. 

63 Example use cases include: 

64 * Logging 

65 * Verifying requests according to service or custom semantics 

66 * Stripping extraneous information from responses 

67 

68 These use cases and more can be enabled by injecting an 

69 instance of a custom subclass when constructing the ErrorGroupServiceRestTransport. 

70 

71 .. code-block:: python 

72 class MyCustomErrorGroupServiceInterceptor(ErrorGroupServiceRestInterceptor): 

73 def pre_get_group(self, request, metadata): 

74 logging.log(f"Received request: {request}") 

75 return request, metadata 

76 

77 def post_get_group(self, response): 

78 logging.log(f"Received response: {response}") 

79 return response 

80 

81 def pre_update_group(self, request, metadata): 

82 logging.log(f"Received request: {request}") 

83 return request, metadata 

84 

85 def post_update_group(self, response): 

86 logging.log(f"Received response: {response}") 

87 return response 

88 

89 transport = ErrorGroupServiceRestTransport(interceptor=MyCustomErrorGroupServiceInterceptor()) 

90 client = ErrorGroupServiceClient(transport=transport) 

91 

92 

93 """ 

94 

95 def pre_get_group( 

96 self, 

97 request: error_group_service.GetGroupRequest, 

98 metadata: Sequence[Tuple[str, str]], 

99 ) -> Tuple[error_group_service.GetGroupRequest, Sequence[Tuple[str, str]]]: 

100 """Pre-rpc interceptor for get_group 

101 

102 Override in a subclass to manipulate the request or metadata 

103 before they are sent to the ErrorGroupService server. 

104 """ 

105 return request, metadata 

106 

107 def post_get_group(self, response: common.ErrorGroup) -> common.ErrorGroup: 

108 """Post-rpc interceptor for get_group 

109 

110 Override in a subclass to manipulate the response 

111 after it is returned by the ErrorGroupService server but before 

112 it is returned to user code. 

113 """ 

114 return response 

115 

116 def pre_update_group( 

117 self, 

118 request: error_group_service.UpdateGroupRequest, 

119 metadata: Sequence[Tuple[str, str]], 

120 ) -> Tuple[error_group_service.UpdateGroupRequest, Sequence[Tuple[str, str]]]: 

121 """Pre-rpc interceptor for update_group 

122 

123 Override in a subclass to manipulate the request or metadata 

124 before they are sent to the ErrorGroupService server. 

125 """ 

126 return request, metadata 

127 

128 def post_update_group(self, response: common.ErrorGroup) -> common.ErrorGroup: 

129 """Post-rpc interceptor for update_group 

130 

131 Override in a subclass to manipulate the response 

132 after it is returned by the ErrorGroupService server but before 

133 it is returned to user code. 

134 """ 

135 return response 

136 

137 

138@dataclasses.dataclass 

139class ErrorGroupServiceRestStub: 

140 _session: AuthorizedSession 

141 _host: str 

142 _interceptor: ErrorGroupServiceRestInterceptor 

143 

144 

145class ErrorGroupServiceRestTransport(ErrorGroupServiceTransport): 

146 """REST backend transport for ErrorGroupService. 

147 

148 Service for retrieving and updating individual error groups. 

149 

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

151 primary client can load the underlying transport implementation 

152 and call it. 

153 

154 It sends JSON representations of protocol buffers over HTTP/1.1 

155 

156 """ 

157 

158 def __init__( 

159 self, 

160 *, 

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

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

163 credentials_file: Optional[str] = None, 

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

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

166 quota_project_id: Optional[str] = None, 

167 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

168 always_use_jwt_access: Optional[bool] = False, 

169 url_scheme: str = "https", 

170 interceptor: Optional[ErrorGroupServiceRestInterceptor] = None, 

171 api_audience: Optional[str] = None, 

172 ) -> None: 

173 """Instantiate the transport. 

174 

175 Args: 

176 host (Optional[str]): 

177 The hostname to connect to. 

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

179 authorization credentials to attach to requests. These 

180 credentials identify the application to the service; if none 

181 are specified, the client will attempt to ascertain the 

182 credentials from the environment. 

183 

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

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

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

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

188 ignored if ``channel`` is provided. 

189 client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client 

190 certificate to configure mutual TLS HTTP channel. It is ignored 

191 if ``channel`` is provided. 

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

193 and quota. 

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

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

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

197 Generally, you only need to set this if you are developing 

198 your own client library. 

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

200 be used for service account credentials. 

201 url_scheme: the protocol scheme for the API endpoint. Normally 

202 "https", but for testing or local servers, 

203 "http" can be specified. 

204 """ 

205 # Run the base constructor 

206 # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. 

207 # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the 

208 # credentials object 

209 maybe_url_match = re.match("^(?P<scheme>http(?:s)?://)?(?P<host>.*)$", host) 

210 if maybe_url_match is None: 

211 raise ValueError( 

212 f"Unexpected hostname structure: {host}" 

213 ) # pragma: NO COVER 

214 

215 url_match_items = maybe_url_match.groupdict() 

216 

217 host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host 

218 

219 super().__init__( 

220 host=host, 

221 credentials=credentials, 

222 client_info=client_info, 

223 always_use_jwt_access=always_use_jwt_access, 

224 api_audience=api_audience, 

225 ) 

226 self._session = AuthorizedSession( 

227 self._credentials, default_host=self.DEFAULT_HOST 

228 ) 

229 if client_cert_source_for_mtls: 

230 self._session.configure_mtls_channel(client_cert_source_for_mtls) 

231 self._interceptor = interceptor or ErrorGroupServiceRestInterceptor() 

232 self._prep_wrapped_messages(client_info) 

233 

234 class _GetGroup(ErrorGroupServiceRestStub): 

235 def __hash__(self): 

236 return hash("GetGroup") 

237 

238 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} 

239 

240 @classmethod 

241 def _get_unset_required_fields(cls, message_dict): 

242 return { 

243 k: v 

244 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() 

245 if k not in message_dict 

246 } 

247 

248 def __call__( 

249 self, 

250 request: error_group_service.GetGroupRequest, 

251 *, 

252 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

253 timeout: Optional[float] = None, 

254 metadata: Sequence[Tuple[str, str]] = (), 

255 ) -> common.ErrorGroup: 

256 r"""Call the get group method over HTTP. 

257 

258 Args: 

259 request (~.error_group_service.GetGroupRequest): 

260 The request object. A request to return an individual 

261 group. 

262 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

263 should be retried. 

264 timeout (float): The timeout for this request. 

265 metadata (Sequence[Tuple[str, str]]): Strings which should be 

266 sent along with the request as metadata. 

267 

268 Returns: 

269 ~.common.ErrorGroup: 

270 Description of a group of similar 

271 error events. 

272 

273 """ 

274 

275 http_options: List[Dict[str, str]] = [ 

276 { 

277 "method": "get", 

278 "uri": "/v1beta1/{group_name=projects/*/groups/*}", 

279 }, 

280 ] 

281 request, metadata = self._interceptor.pre_get_group(request, metadata) 

282 pb_request = error_group_service.GetGroupRequest.pb(request) 

283 transcoded_request = path_template.transcode(http_options, pb_request) 

284 

285 uri = transcoded_request["uri"] 

286 method = transcoded_request["method"] 

287 

288 # Jsonify the query params 

289 query_params = json.loads( 

290 json_format.MessageToJson( 

291 transcoded_request["query_params"], 

292 including_default_value_fields=False, 

293 use_integers_for_enums=True, 

294 ) 

295 ) 

296 query_params.update(self._get_unset_required_fields(query_params)) 

297 

298 query_params["$alt"] = "json;enum-encoding=int" 

299 

300 # Send the request 

301 headers = dict(metadata) 

302 headers["Content-Type"] = "application/json" 

303 response = getattr(self._session, method)( 

304 "{host}{uri}".format(host=self._host, uri=uri), 

305 timeout=timeout, 

306 headers=headers, 

307 params=rest_helpers.flatten_query_params(query_params, strict=True), 

308 ) 

309 

310 # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception 

311 # subclass. 

312 if response.status_code >= 400: 

313 raise core_exceptions.from_http_response(response) 

314 

315 # Return the response 

316 resp = common.ErrorGroup() 

317 pb_resp = common.ErrorGroup.pb(resp) 

318 

319 json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) 

320 resp = self._interceptor.post_get_group(resp) 

321 return resp 

322 

323 class _UpdateGroup(ErrorGroupServiceRestStub): 

324 def __hash__(self): 

325 return hash("UpdateGroup") 

326 

327 __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} 

328 

329 @classmethod 

330 def _get_unset_required_fields(cls, message_dict): 

331 return { 

332 k: v 

333 for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() 

334 if k not in message_dict 

335 } 

336 

337 def __call__( 

338 self, 

339 request: error_group_service.UpdateGroupRequest, 

340 *, 

341 retry: OptionalRetry = gapic_v1.method.DEFAULT, 

342 timeout: Optional[float] = None, 

343 metadata: Sequence[Tuple[str, str]] = (), 

344 ) -> common.ErrorGroup: 

345 r"""Call the update group method over HTTP. 

346 

347 Args: 

348 request (~.error_group_service.UpdateGroupRequest): 

349 The request object. A request to replace the existing 

350 data for the given group. 

351 retry (google.api_core.retry.Retry): Designation of what errors, if any, 

352 should be retried. 

353 timeout (float): The timeout for this request. 

354 metadata (Sequence[Tuple[str, str]]): Strings which should be 

355 sent along with the request as metadata. 

356 

357 Returns: 

358 ~.common.ErrorGroup: 

359 Description of a group of similar 

360 error events. 

361 

362 """ 

363 

364 http_options: List[Dict[str, str]] = [ 

365 { 

366 "method": "put", 

367 "uri": "/v1beta1/{group.name=projects/*/groups/*}", 

368 "body": "group", 

369 }, 

370 ] 

371 request, metadata = self._interceptor.pre_update_group(request, metadata) 

372 pb_request = error_group_service.UpdateGroupRequest.pb(request) 

373 transcoded_request = path_template.transcode(http_options, pb_request) 

374 

375 # Jsonify the request body 

376 

377 body = json_format.MessageToJson( 

378 transcoded_request["body"], 

379 including_default_value_fields=False, 

380 use_integers_for_enums=True, 

381 ) 

382 uri = transcoded_request["uri"] 

383 method = transcoded_request["method"] 

384 

385 # Jsonify the query params 

386 query_params = json.loads( 

387 json_format.MessageToJson( 

388 transcoded_request["query_params"], 

389 including_default_value_fields=False, 

390 use_integers_for_enums=True, 

391 ) 

392 ) 

393 query_params.update(self._get_unset_required_fields(query_params)) 

394 

395 query_params["$alt"] = "json;enum-encoding=int" 

396 

397 # Send the request 

398 headers = dict(metadata) 

399 headers["Content-Type"] = "application/json" 

400 response = getattr(self._session, method)( 

401 "{host}{uri}".format(host=self._host, uri=uri), 

402 timeout=timeout, 

403 headers=headers, 

404 params=rest_helpers.flatten_query_params(query_params, strict=True), 

405 data=body, 

406 ) 

407 

408 # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception 

409 # subclass. 

410 if response.status_code >= 400: 

411 raise core_exceptions.from_http_response(response) 

412 

413 # Return the response 

414 resp = common.ErrorGroup() 

415 pb_resp = common.ErrorGroup.pb(resp) 

416 

417 json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) 

418 resp = self._interceptor.post_update_group(resp) 

419 return resp 

420 

421 @property 

422 def get_group( 

423 self, 

424 ) -> Callable[[error_group_service.GetGroupRequest], common.ErrorGroup]: 

425 # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. 

426 # In C++ this would require a dynamic_cast 

427 return self._GetGroup(self._session, self._host, self._interceptor) # type: ignore 

428 

429 @property 

430 def update_group( 

431 self, 

432 ) -> Callable[[error_group_service.UpdateGroupRequest], common.ErrorGroup]: 

433 # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. 

434 # In C++ this would require a dynamic_cast 

435 return self._UpdateGroup(self._session, self._host, self._interceptor) # type: ignore 

436 

437 @property 

438 def kind(self) -> str: 

439 return "rest" 

440 

441 def close(self): 

442 self._session.close() 

443 

444 

445__all__ = ("ErrorGroupServiceRestTransport",)