Create a _Encrypter for a client. :param client: The encrypted AsyncMongoClient. :param opts: The encrypted client's :class:`AutoEncryptionOpts`.
(self, client: AsyncMongoClient[_DocumentTypeArg], opts: AutoEncryptionOpts)
| 388 | """ |
| 389 | |
| 390 | def __init__(self, client: AsyncMongoClient[_DocumentTypeArg], opts: AutoEncryptionOpts): |
| 391 | """Create a _Encrypter for a client. |
| 392 | |
| 393 | :param client: The encrypted AsyncMongoClient. |
| 394 | :param opts: The encrypted client's :class:`AutoEncryptionOpts`. |
| 395 | """ |
| 396 | if opts._schema_map is None: |
| 397 | schema_map = None |
| 398 | else: |
| 399 | schema_map = _dict_to_bson(opts._schema_map, False, _DATA_KEY_OPTS) |
| 400 | |
| 401 | if opts._encrypted_fields_map is None: |
| 402 | encrypted_fields_map = None |
| 403 | else: |
| 404 | encrypted_fields_map = _dict_to_bson(opts._encrypted_fields_map, False, _DATA_KEY_OPTS) |
| 405 | self._bypass_auto_encryption = opts._bypass_auto_encryption |
| 406 | self._internal_client = None |
| 407 | # parsing kms_ssl_contexts here so that parsing errors will be raised before internal clients are created |
| 408 | opts._kms_ssl_contexts(_IS_SYNC) |
| 409 | |
| 410 | def _get_internal_client( |
| 411 | encrypter: _Encrypter, mongo_client: AsyncMongoClient[_DocumentTypeArg] |
| 412 | ) -> AsyncMongoClient[_DocumentTypeArg]: |
| 413 | if mongo_client.options.pool_options.max_pool_size is None: |
| 414 | # Unlimited pool size, use the same client. |
| 415 | return mongo_client |
| 416 | # Else - limited pool size, use an internal client. |
| 417 | if encrypter._internal_client is not None: |
| 418 | return encrypter._internal_client |
| 419 | internal_client = mongo_client._duplicate(minPoolSize=0, auto_encryption_opts=None) |
| 420 | encrypter._internal_client = internal_client |
| 421 | return internal_client |
| 422 | |
| 423 | if opts._key_vault_client is not None: |
| 424 | key_vault_client = opts._key_vault_client |
| 425 | else: |
| 426 | key_vault_client = _get_internal_client(self, client) |
| 427 | |
| 428 | if opts._bypass_auto_encryption: |
| 429 | metadata_client = None |
| 430 | else: |
| 431 | metadata_client = _get_internal_client(self, client) |
| 432 | |
| 433 | db, coll = opts._key_vault_namespace.split(".", 1) |
| 434 | key_vault_coll = key_vault_client[db][coll] |
| 435 | |
| 436 | mongocryptd_client: AsyncMongoClient[Mapping[str, Any]] = AsyncMongoClient( |
| 437 | opts._mongocryptd_uri, connect=False, serverSelectionTimeoutMS=_MONGOCRYPTD_TIMEOUT_MS |
| 438 | ) |
| 439 | |
| 440 | io_callbacks = _EncryptionIO( # type:ignore[misc] |
| 441 | metadata_client, |
| 442 | key_vault_coll, # type:ignore[arg-type] |
| 443 | mongocryptd_client, |
| 444 | opts, |
| 445 | ) |
| 446 | self._auto_encrypter = AsyncAutoEncrypter( |
| 447 | io_callbacks, |
nothing calls this directly
no test coverage detected