Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/pubsub_v1/services/schema_service/transports/grpc.py: 43%

118 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:25 +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# 

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.iam.v1 import iam_policy_pb2 # type: ignore 

28from google.iam.v1 import policy_pb2 # type: ignore 

29from google.protobuf import empty_pb2 # type: ignore 

30from google.pubsub_v1.types import schema 

31from google.pubsub_v1.types import schema as gp_schema 

32from .base import SchemaServiceTransport, DEFAULT_CLIENT_INFO 

33 

34 

35class SchemaServiceGrpcTransport(SchemaServiceTransport): 

36 """gRPC backend transport for SchemaService. 

37 

38 Service for doing schema-related operations. 

39 

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

41 primary client can load the underlying transport implementation 

42 and call it. 

43 

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

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

46 """ 

47 

48 _stubs: Dict[str, Callable] 

49 

50 def __init__( 

51 self, 

52 *, 

53 host: str = "pubsub.googleapis.com", 

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

55 credentials_file: Optional[str] = None, 

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

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

58 api_mtls_endpoint: Optional[str] = None, 

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

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

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

62 quota_project_id: Optional[str] = None, 

63 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

64 always_use_jwt_access: Optional[bool] = False, 

65 api_audience: Optional[str] = None, 

66 ) -> None: 

67 """Instantiate the transport. 

68 

69 Args: 

70 host (Optional[str]): 

71 The hostname to connect to. 

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

73 authorization credentials to attach to requests. These 

74 credentials identify the application to the service; if none 

75 are specified, the client will attempt to ascertain the 

76 credentials from the environment. 

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

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

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

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

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

82 ignored if ``channel`` is provided. 

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

84 which to make calls. 

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

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

87 a mutual TLS channel with client SSL credentials from 

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

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

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

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

92 ``api_mtls_endpoint`` is None. 

93 ssl_channel_credentials (grpc.ChannelCredentials): SSL credentials 

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

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

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

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

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

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

100 and quota. 

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

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

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

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

105 your own client library. 

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

107 be used for service account credentials. 

108 

109 Raises: 

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

111 creation failed for any reason. 

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

113 and ``credentials_file`` are passed. 

114 """ 

115 self._grpc_channel = None 

116 self._ssl_channel_credentials = ssl_channel_credentials 

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

118 

119 if api_mtls_endpoint: 

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

121 if client_cert_source: 

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

123 

124 if channel: 

125 # Ignore credentials if a channel was passed. 

126 credentials = False 

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

128 self._grpc_channel = channel 

129 self._ssl_channel_credentials = None 

130 

131 else: 

132 if api_mtls_endpoint: 

133 host = api_mtls_endpoint 

134 

135 # Create SSL credentials with client_cert_source or application 

136 # default SSL credentials. 

137 if client_cert_source: 

138 cert, key = client_cert_source() 

139 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

140 certificate_chain=cert, private_key=key 

141 ) 

142 else: 

143 self._ssl_channel_credentials = SslCredentials().ssl_credentials 

144 

145 else: 

146 if client_cert_source_for_mtls and not ssl_channel_credentials: 

147 cert, key = client_cert_source_for_mtls() 

148 self._ssl_channel_credentials = grpc.ssl_channel_credentials( 

149 certificate_chain=cert, private_key=key 

150 ) 

151 

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

153 super().__init__( 

154 host=host, 

155 credentials=credentials, 

156 credentials_file=credentials_file, 

157 scopes=scopes, 

158 quota_project_id=quota_project_id, 

159 client_info=client_info, 

160 always_use_jwt_access=always_use_jwt_access, 

161 api_audience=api_audience, 

162 ) 

163 

164 if not self._grpc_channel: 

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

166 self._host, 

167 # use the credentials which are saved 

168 credentials=self._credentials, 

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

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

171 credentials_file=None, 

172 scopes=self._scopes, 

173 ssl_credentials=self._ssl_channel_credentials, 

174 quota_project_id=quota_project_id, 

175 options=[ 

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

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

178 ("grpc.max_metadata_size", 4 * 1024 * 1024), 

179 ("grpc.keepalive_time_ms", 30000), 

180 ], 

181 ) 

182 

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

184 self._prep_wrapped_messages(client_info) 

185 

186 @classmethod 

187 def create_channel( 

188 cls, 

189 host: str = "pubsub.googleapis.com", 

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

191 credentials_file: Optional[str] = None, 

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

193 quota_project_id: Optional[str] = None, 

194 **kwargs, 

195 ) -> grpc.Channel: 

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

197 Args: 

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

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

200 authorization credentials to attach to requests. These 

201 credentials identify this application to the service. If 

202 none are specified, the client will attempt to ascertain 

203 the credentials from the environment. 

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

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

206 This argument is mutually exclusive with credentials. 

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

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

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

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

211 and quota. 

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

213 channel creation. 

214 Returns: 

215 grpc.Channel: A gRPC channel object. 

216 

217 Raises: 

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

219 and ``credentials_file`` are passed. 

220 """ 

221 

222 return grpc_helpers.create_channel( 

223 host, 

224 credentials=credentials, 

225 credentials_file=credentials_file, 

226 quota_project_id=quota_project_id, 

227 default_scopes=cls.AUTH_SCOPES, 

228 scopes=scopes, 

229 default_host=cls.DEFAULT_HOST, 

230 **kwargs, 

231 ) 

232 

233 @property 

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

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

236 return self._grpc_channel 

237 

238 @property 

239 def create_schema( 

240 self, 

241 ) -> Callable[[gp_schema.CreateSchemaRequest], gp_schema.Schema]: 

242 r"""Return a callable for the create schema method over gRPC. 

243 

244 Creates a schema. 

245 

246 Returns: 

247 Callable[[~.CreateSchemaRequest], 

248 ~.Schema]: 

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

250 on the server. 

251 """ 

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

253 # the request. 

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

255 # to pass in the functions for each. 

256 if "create_schema" not in self._stubs: 

257 self._stubs["create_schema"] = self.grpc_channel.unary_unary( 

258 "/google.pubsub.v1.SchemaService/CreateSchema", 

259 request_serializer=gp_schema.CreateSchemaRequest.serialize, 

260 response_deserializer=gp_schema.Schema.deserialize, 

261 ) 

262 return self._stubs["create_schema"] 

263 

264 @property 

265 def get_schema(self) -> Callable[[schema.GetSchemaRequest], schema.Schema]: 

266 r"""Return a callable for the get schema method over gRPC. 

267 

268 Gets a schema. 

269 

270 Returns: 

271 Callable[[~.GetSchemaRequest], 

272 ~.Schema]: 

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

274 on the server. 

275 """ 

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

277 # the request. 

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

279 # to pass in the functions for each. 

280 if "get_schema" not in self._stubs: 

281 self._stubs["get_schema"] = self.grpc_channel.unary_unary( 

282 "/google.pubsub.v1.SchemaService/GetSchema", 

283 request_serializer=schema.GetSchemaRequest.serialize, 

284 response_deserializer=schema.Schema.deserialize, 

285 ) 

286 return self._stubs["get_schema"] 

287 

288 @property 

289 def list_schemas( 

290 self, 

291 ) -> Callable[[schema.ListSchemasRequest], schema.ListSchemasResponse]: 

292 r"""Return a callable for the list schemas method over gRPC. 

293 

294 Lists schemas in a project. 

295 

296 Returns: 

297 Callable[[~.ListSchemasRequest], 

298 ~.ListSchemasResponse]: 

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

300 on the server. 

301 """ 

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

303 # the request. 

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

305 # to pass in the functions for each. 

306 if "list_schemas" not in self._stubs: 

307 self._stubs["list_schemas"] = self.grpc_channel.unary_unary( 

308 "/google.pubsub.v1.SchemaService/ListSchemas", 

309 request_serializer=schema.ListSchemasRequest.serialize, 

310 response_deserializer=schema.ListSchemasResponse.deserialize, 

311 ) 

312 return self._stubs["list_schemas"] 

313 

314 @property 

315 def list_schema_revisions( 

316 self, 

317 ) -> Callable[ 

318 [schema.ListSchemaRevisionsRequest], schema.ListSchemaRevisionsResponse 

319 ]: 

320 r"""Return a callable for the list schema revisions method over gRPC. 

321 

322 Lists all schema revisions for the named schema. 

323 

324 Returns: 

325 Callable[[~.ListSchemaRevisionsRequest], 

326 ~.ListSchemaRevisionsResponse]: 

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

328 on the server. 

329 """ 

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

331 # the request. 

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

333 # to pass in the functions for each. 

334 if "list_schema_revisions" not in self._stubs: 

335 self._stubs["list_schema_revisions"] = self.grpc_channel.unary_unary( 

336 "/google.pubsub.v1.SchemaService/ListSchemaRevisions", 

337 request_serializer=schema.ListSchemaRevisionsRequest.serialize, 

338 response_deserializer=schema.ListSchemaRevisionsResponse.deserialize, 

339 ) 

340 return self._stubs["list_schema_revisions"] 

341 

342 @property 

343 def commit_schema( 

344 self, 

345 ) -> Callable[[gp_schema.CommitSchemaRequest], gp_schema.Schema]: 

346 r"""Return a callable for the commit schema method over gRPC. 

347 

348 Commits a new schema revision to an existing schema. 

349 

350 Returns: 

351 Callable[[~.CommitSchemaRequest], 

352 ~.Schema]: 

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

354 on the server. 

355 """ 

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

357 # the request. 

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

359 # to pass in the functions for each. 

360 if "commit_schema" not in self._stubs: 

361 self._stubs["commit_schema"] = self.grpc_channel.unary_unary( 

362 "/google.pubsub.v1.SchemaService/CommitSchema", 

363 request_serializer=gp_schema.CommitSchemaRequest.serialize, 

364 response_deserializer=gp_schema.Schema.deserialize, 

365 ) 

366 return self._stubs["commit_schema"] 

367 

368 @property 

369 def rollback_schema( 

370 self, 

371 ) -> Callable[[schema.RollbackSchemaRequest], schema.Schema]: 

372 r"""Return a callable for the rollback schema method over gRPC. 

373 

374 Creates a new schema revision that is a copy of the provided 

375 revision_id. 

376 

377 Returns: 

378 Callable[[~.RollbackSchemaRequest], 

379 ~.Schema]: 

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

381 on the server. 

382 """ 

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

384 # the request. 

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

386 # to pass in the functions for each. 

387 if "rollback_schema" not in self._stubs: 

388 self._stubs["rollback_schema"] = self.grpc_channel.unary_unary( 

389 "/google.pubsub.v1.SchemaService/RollbackSchema", 

390 request_serializer=schema.RollbackSchemaRequest.serialize, 

391 response_deserializer=schema.Schema.deserialize, 

392 ) 

393 return self._stubs["rollback_schema"] 

394 

395 @property 

396 def delete_schema_revision( 

397 self, 

398 ) -> Callable[[schema.DeleteSchemaRevisionRequest], schema.Schema]: 

399 r"""Return a callable for the delete schema revision method over gRPC. 

400 

401 Deletes a specific schema revision. 

402 

403 Returns: 

404 Callable[[~.DeleteSchemaRevisionRequest], 

405 ~.Schema]: 

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

407 on the server. 

408 """ 

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

410 # the request. 

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

412 # to pass in the functions for each. 

413 if "delete_schema_revision" not in self._stubs: 

414 self._stubs["delete_schema_revision"] = self.grpc_channel.unary_unary( 

415 "/google.pubsub.v1.SchemaService/DeleteSchemaRevision", 

416 request_serializer=schema.DeleteSchemaRevisionRequest.serialize, 

417 response_deserializer=schema.Schema.deserialize, 

418 ) 

419 return self._stubs["delete_schema_revision"] 

420 

421 @property 

422 def delete_schema(self) -> Callable[[schema.DeleteSchemaRequest], empty_pb2.Empty]: 

423 r"""Return a callable for the delete schema method over gRPC. 

424 

425 Deletes a schema. 

426 

427 Returns: 

428 Callable[[~.DeleteSchemaRequest], 

429 ~.Empty]: 

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

431 on the server. 

432 """ 

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

434 # the request. 

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

436 # to pass in the functions for each. 

437 if "delete_schema" not in self._stubs: 

438 self._stubs["delete_schema"] = self.grpc_channel.unary_unary( 

439 "/google.pubsub.v1.SchemaService/DeleteSchema", 

440 request_serializer=schema.DeleteSchemaRequest.serialize, 

441 response_deserializer=empty_pb2.Empty.FromString, 

442 ) 

443 return self._stubs["delete_schema"] 

444 

445 @property 

446 def validate_schema( 

447 self, 

448 ) -> Callable[[gp_schema.ValidateSchemaRequest], gp_schema.ValidateSchemaResponse]: 

449 r"""Return a callable for the validate schema method over gRPC. 

450 

451 Validates a schema. 

452 

453 Returns: 

454 Callable[[~.ValidateSchemaRequest], 

455 ~.ValidateSchemaResponse]: 

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

457 on the server. 

458 """ 

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

460 # the request. 

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

462 # to pass in the functions for each. 

463 if "validate_schema" not in self._stubs: 

464 self._stubs["validate_schema"] = self.grpc_channel.unary_unary( 

465 "/google.pubsub.v1.SchemaService/ValidateSchema", 

466 request_serializer=gp_schema.ValidateSchemaRequest.serialize, 

467 response_deserializer=gp_schema.ValidateSchemaResponse.deserialize, 

468 ) 

469 return self._stubs["validate_schema"] 

470 

471 @property 

472 def validate_message( 

473 self, 

474 ) -> Callable[[schema.ValidateMessageRequest], schema.ValidateMessageResponse]: 

475 r"""Return a callable for the validate message method over gRPC. 

476 

477 Validates a message against a schema. 

478 

479 Returns: 

480 Callable[[~.ValidateMessageRequest], 

481 ~.ValidateMessageResponse]: 

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

483 on the server. 

484 """ 

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

486 # the request. 

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

488 # to pass in the functions for each. 

489 if "validate_message" not in self._stubs: 

490 self._stubs["validate_message"] = self.grpc_channel.unary_unary( 

491 "/google.pubsub.v1.SchemaService/ValidateMessage", 

492 request_serializer=schema.ValidateMessageRequest.serialize, 

493 response_deserializer=schema.ValidateMessageResponse.deserialize, 

494 ) 

495 return self._stubs["validate_message"] 

496 

497 @property 

498 def set_iam_policy( 

499 self, 

500 ) -> Callable[[iam_policy_pb2.SetIamPolicyRequest], policy_pb2.Policy]: 

501 r"""Return a callable for the set iam policy method over gRPC. 

502 Sets the IAM access control policy on the specified 

503 function. Replaces any existing policy. 

504 Returns: 

505 Callable[[~.SetIamPolicyRequest], 

506 ~.Policy]: 

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

508 on the server. 

509 """ 

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

511 # the request. 

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

513 # to pass in the functions for each. 

514 if "set_iam_policy" not in self._stubs: 

515 self._stubs["set_iam_policy"] = self.grpc_channel.unary_unary( 

516 "/google.iam.v1.IAMPolicy/SetIamPolicy", 

517 request_serializer=iam_policy_pb2.SetIamPolicyRequest.SerializeToString, 

518 response_deserializer=policy_pb2.Policy.FromString, 

519 ) 

520 return self._stubs["set_iam_policy"] 

521 

522 @property 

523 def get_iam_policy( 

524 self, 

525 ) -> Callable[[iam_policy_pb2.GetIamPolicyRequest], policy_pb2.Policy]: 

526 r"""Return a callable for the get iam policy method over gRPC. 

527 Gets the IAM access control policy for a function. 

528 Returns an empty policy if the function exists and does 

529 not have a policy set. 

530 Returns: 

531 Callable[[~.GetIamPolicyRequest], 

532 ~.Policy]: 

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

534 on the server. 

535 """ 

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

537 # the request. 

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

539 # to pass in the functions for each. 

540 if "get_iam_policy" not in self._stubs: 

541 self._stubs["get_iam_policy"] = self.grpc_channel.unary_unary( 

542 "/google.iam.v1.IAMPolicy/GetIamPolicy", 

543 request_serializer=iam_policy_pb2.GetIamPolicyRequest.SerializeToString, 

544 response_deserializer=policy_pb2.Policy.FromString, 

545 ) 

546 return self._stubs["get_iam_policy"] 

547 

548 @property 

549 def test_iam_permissions( 

550 self, 

551 ) -> Callable[ 

552 [iam_policy_pb2.TestIamPermissionsRequest], 

553 iam_policy_pb2.TestIamPermissionsResponse, 

554 ]: 

555 r"""Return a callable for the test iam permissions method over gRPC. 

556 Tests the specified permissions against the IAM access control 

557 policy for a function. If the function does not exist, this will 

558 return an empty set of permissions, not a NOT_FOUND error. 

559 Returns: 

560 Callable[[~.TestIamPermissionsRequest], 

561 ~.TestIamPermissionsResponse]: 

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

563 on the server. 

564 """ 

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

566 # the request. 

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

568 # to pass in the functions for each. 

569 if "test_iam_permissions" not in self._stubs: 

570 self._stubs["test_iam_permissions"] = self.grpc_channel.unary_unary( 

571 "/google.iam.v1.IAMPolicy/TestIamPermissions", 

572 request_serializer=iam_policy_pb2.TestIamPermissionsRequest.SerializeToString, 

573 response_deserializer=iam_policy_pb2.TestIamPermissionsResponse.FromString, 

574 ) 

575 return self._stubs["test_iam_permissions"] 

576 

577 def close(self): 

578 self.grpc_channel.close() 

579 

580 @property 

581 def kind(self) -> str: 

582 return "grpc" 

583 

584 

585__all__ = ("SchemaServiceGrpcTransport",)