diff --git a/sdk/keyvault/azure-keyvault-certificates/README.md b/sdk/keyvault/azure-keyvault-certificates/README.md index e32279a3a949..9b710e50e7f4 100644 --- a/sdk/keyvault/azure-keyvault-certificates/README.md +++ b/sdk/keyvault/azure-keyvault-certificates/README.md @@ -266,8 +266,8 @@ See [azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport) for more information. -Async clients should be closed when they're no longer needed. Each async -client is an async context manager and defines an async `close` method. For +Async clients and credentials should be closed when they're no longer needed. These +objects are async context managers and define async `close` methods. For example: ```py @@ -276,15 +276,17 @@ from azure.keyvault.certificates.aio import CertificateClient credential = DefaultAzureCredential() -# call close when the client is no longer needed +# call close when the client and credential are no longer needed client = CertificateClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) ... await client.close() +await credential.close() -# alternatively, use the client as an async context manager +# alternatively, use them as async context managers (contextlib.AsyncExitStack can help) client = CertificateClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) async with client: - ... + async with credential: + ... ``` ### Asynchronously create a Certificate diff --git a/sdk/keyvault/azure-keyvault-certificates/tests/test_examples_certificates_async.py b/sdk/keyvault/azure-keyvault-certificates/tests/test_examples_certificates_async.py index 9ebabd6b9160..0e92974f1793 100644 --- a/sdk/keyvault/azure-keyvault-certificates/tests/test_examples_certificates_async.py +++ b/sdk/keyvault/azure-keyvault-certificates/tests/test_examples_certificates_async.py @@ -8,6 +8,7 @@ from azure.keyvault.certificates import CertificatePolicy, CertificateContentType, WellKnownIssuerNames from azure.keyvault.certificates.aio import CertificateClient from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer +import pytest from _shared.preparer_async import KeyVaultClientPreparer as _KeyVaultClientPreparer from _shared.test_case_async import KeyVaultTestCase @@ -20,7 +21,8 @@ def print(*args): assert all(arg is not None for arg in args) -def test_create_certificate(): +@pytest.mark.asyncio +async def test_create_certificate(): vault_url = "vault_url" # pylint:disable=unused-variable # [START create_certificate_client] @@ -30,6 +32,11 @@ def test_create_certificate(): # Create a KeyVaultCertificate using default Azure credentials credential = DefaultAzureCredential() certificate_client = CertificateClient(vault_url=vault_url, credential=credential) + + # the client and credential should be closed when no longer needed + # (both are also async context managers) + await certificate_client.close() + await credential.close() # [END create_certificate_client] diff --git a/sdk/keyvault/azure-keyvault-keys/README.md b/sdk/keyvault/azure-keyvault-keys/README.md index 2f050af47b8b..f3e288a52e7f 100644 --- a/sdk/keyvault/azure-keyvault-keys/README.md +++ b/sdk/keyvault/azure-keyvault-keys/README.md @@ -281,8 +281,8 @@ See [azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport) for more information. -Async clients should be closed when they're no longer needed. Each async -client is an async context manager and defines an async `close` method. For +Async clients and credentials should be closed when they're no longer needed. These +objects are async context managers and define async `close` methods. For example: ```py @@ -291,15 +291,17 @@ from azure.keyvault.keys.aio import KeyClient credential = DefaultAzureCredential() -# call close when the client is no longer needed +# call close when the client and credential are no longer needed client = KeyClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) ... await client.close() +await credential.close() -# alternatively, use the client as an async context manager +# alternatively, use them as async context managers (contextlib.AsyncExitStack can help) client = KeyClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) async with client: - ... + async with credential: + ... ``` ### Asynchronously create a Key diff --git a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py index 1ab02bf9a19f..926f5e554893 100644 --- a/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py +++ b/sdk/keyvault/azure-keyvault-keys/tests/test_examples_crypto_async.py @@ -51,6 +51,11 @@ async def test_encrypt_decrypt_async(self, azure_keyvault_url, **kwargs): # or a key's id, which must include a version key_id = "https://.vault.azure.net/keys//fe4fdcab688c479a9aa80f01ffeac26" crypto_client = CryptographyClient(key_id, credential) + + # the client and credential should be closed when no longer needed + # (both are also async context managers) + await crypto_client.close() + await credential.close() # [END create_client] client = CryptographyClient(key, credential) diff --git a/sdk/keyvault/azure-keyvault-keys/tests/test_samples_keys_async.py b/sdk/keyvault/azure-keyvault-keys/tests/test_samples_keys_async.py index de47b7503a81..2bfffd9f8c74 100644 --- a/sdk/keyvault/azure-keyvault-keys/tests/test_samples_keys_async.py +++ b/sdk/keyvault/azure-keyvault-keys/tests/test_samples_keys_async.py @@ -9,6 +9,7 @@ from azure.keyvault.keys.aio import KeyClient from azure.keyvault.keys._shared import HttpChallengeCache from devtools_testutils import PowerShellPreparer +import pytest from _shared.test_case_async import KeyVaultTestCase @@ -24,7 +25,8 @@ def print(*args): assert all(arg is not None for arg in args) -def test_create_key_client(): +@pytest.mark.asyncio +async def test_create_key_client(): vault_url = "vault_url" # pylint:disable=unused-variable # [START create_key_client] @@ -34,6 +36,11 @@ def test_create_key_client(): # Create a KeyClient using default Azure credentials credential = DefaultAzureCredential() key_client = KeyClient(vault_url, credential) + + # the client and credential should be closed when no longer needed + # (both are also async context managers) + await key_client.close() + await credential.close() # [END create_key_client] diff --git a/sdk/keyvault/azure-keyvault-secrets/README.md b/sdk/keyvault/azure-keyvault-secrets/README.md index 3c37e0425180..da406b6c662c 100644 --- a/sdk/keyvault/azure-keyvault-secrets/README.md +++ b/sdk/keyvault/azure-keyvault-secrets/README.md @@ -263,8 +263,8 @@ See [azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport) for more information. -Async clients should be closed when they're no longer needed. Each async -client is an async context manager and defines an async `close` method. For +Async clients and credentials should be closed when they're no longer needed. These +objects are async context managers and define async `close` methods. For example: ```py @@ -273,15 +273,17 @@ from azure.keyvault.secrets.aio import SecretClient credential = DefaultAzureCredential() -# call close when the client is no longer needed +# call close when the client and credential are no longer needed client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) ... await client.close() +await credential.close() -# alternatively, use the client as an async context manager +# alternatively, use them as async context managers (contextlib.AsyncExitStack can help) client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential) async with client: - ... + async with credential: + ... ``` ### Asynchronously create a secret diff --git a/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets.py b/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets.py index d907b09f42e0..ea428e742846 100644 --- a/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets.py +++ b/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets.py @@ -28,8 +28,8 @@ def test_create_secret_client(): from azure.keyvault.secrets import SecretClient # Create a SecretClient using default Azure credentials - credentials = DefaultAzureCredential() - secret_client = SecretClient(vault_url, credentials) + credential = DefaultAzureCredential() + secret_client = SecretClient(vault_url, credential) # [END create_secret_client] diff --git a/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets_async.py b/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets_async.py index 8bd4775fb238..67b8be69623f 100644 --- a/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets_async.py +++ b/sdk/keyvault/azure-keyvault-secrets/tests/test_samples_secrets_async.py @@ -7,6 +7,7 @@ from azure.keyvault.secrets.aio import SecretClient from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer +import pytest from _shared.preparer_async import KeyVaultClientPreparer as _KeyVaultClientPreparer from _shared.test_case_async import KeyVaultTestCase @@ -20,7 +21,8 @@ def print(*args): assert all(arg is not None for arg in args) -def test_create_secret_client(): +@pytest.mark.asyncio +async def test_create_secret_client(): vault_url = "vault_url" # pylint:disable=unused-variable # [START create_secret_client] @@ -28,8 +30,13 @@ def test_create_secret_client(): from azure.keyvault.secrets.aio import SecretClient # Create a SecretClient using default Azure credentials - credentials = DefaultAzureCredential() - secret_client = SecretClient(vault_url, credentials) + credential = DefaultAzureCredential() + secret_client = SecretClient(vault_url, credential) + + # the client and credential should be closed when no longer needed + # (both are also async context managers) + await secret_client.close() + await credential.close() # [END create_secret_client]