From 90d9bdcc33bdd41fa162145c29c2fe34607ca34c Mon Sep 17 00:00:00 2001 From: Krista Pratico Date: Wed, 8 Mar 2023 09:34:42 -0800 Subject: [PATCH] add newline cushion between tags and python snippet in snippet update script (#29170) * add newline cushion between tags and python snippet in snippet updater script * run snippet updater script on libraries that use sample tags in readme --- .../azure-appconfiguration/README.md | 18 +++++++++++++ sdk/eventgrid/azure-eventgrid/README.md | 4 +++ sdk/eventhub/azure-eventhub/README.md | 4 +++ .../azure-keyvault-certificates/README.md | 2 ++ sdk/keyvault/azure-keyvault-keys/README.md | 2 ++ sdk/keyvault/azure-keyvault-secrets/README.md | 2 ++ .../azure-ai-metricsadvisor/README.md | 26 +++++++++++++++++++ sdk/search/azure-search-documents/README.md | 10 +++++++ .../azure-ai-translation-document/README.md | 10 +++++++ .../snippet_update/python_snippet_updater.py | 2 +- 10 files changed, 79 insertions(+), 1 deletion(-) diff --git a/sdk/appconfiguration/azure-appconfiguration/README.md b/sdk/appconfiguration/azure-appconfiguration/README.md index c0db43ee7d0c..59f2da5b4761 100644 --- a/sdk/appconfiguration/azure-appconfiguration/README.md +++ b/sdk/appconfiguration/azure-appconfiguration/README.md @@ -59,6 +59,7 @@ Alternatively, get the connection string from the Azure Portal. Once you have the value of the connection string, you can create the AzureAppConfigurationClient: + ```python import os from azure.appconfiguration import AzureAppConfigurationClient @@ -67,6 +68,7 @@ CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING'] # Create app config client client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING) ``` + #### Use AAD token @@ -167,6 +169,7 @@ There are two ways to store a Configuration Setting: - add_configuration_setting creates a setting only if the setting does not already exist in the store. + ```python config_setting = ConfigurationSetting( key="MyKey", @@ -177,16 +180,19 @@ config_setting = ConfigurationSetting( ) added_config_setting = client.add_configuration_setting(config_setting) ``` + - set_configuration_setting creates a setting if it doesn't exist or overrides an existing setting. + ```python added_config_setting.value = "new value" added_config_setting.content_type = "new content type" updated_config_setting = client.set_configuration_setting(config_setting) ``` + ### Get a Configuration Setting @@ -194,11 +200,13 @@ updated_config_setting = client.set_configuration_setting(config_setting) Get a previously stored Configuration Setting. + ```python fetched_config_setting = client.get_configuration_setting( key="MyKey" ) ``` + ### Delete a Configuration Setting @@ -206,12 +214,14 @@ fetched_config_setting = client.get_configuration_setting( Delete an existing Configuration Setting. + ```python client.delete_configuration_setting( key="MyKey", label="MyLabel", ) ``` + ### List Configuration Settings @@ -219,11 +229,13 @@ client.delete_configuration_setting( List all configuration settings filtered with label_filter and/or key_filter. + ```python config_settings = client.list_configuration_settings(label_filter="MyLabel") for item in config_settings: print_configuration_setting(item) ``` + ### Async APIs @@ -232,6 +244,7 @@ Async client is supported. To use the async client library, import the AzureAppConfigurationClient from package azure.appconfiguration.aio instead of azure.appconfiguration + ```python import os from azure.appconfiguration.aio import AzureAppConfigurationClient @@ -240,27 +253,32 @@ CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING'] # Create app config client client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING) ``` + This async AzureAppConfigurationClient has the same method signatures as the sync ones except that they're async. For instance, to retrieve a Configuration Setting asynchronously, async_client can be used: + ```python fetched_config_setting = await client.get_configuration_setting( key="MyKey" ) ``` + To use list_configuration_settings, call it synchronously and iterate over the returned async iterator asynchronously + ```python config_settings = client.list_configuration_settings(label_filter="MyLabel") async for item in config_settings: print_configuration_setting(item) ``` + ## Troubleshooting diff --git a/sdk/eventgrid/azure-eventgrid/README.md b/sdk/eventgrid/azure-eventgrid/README.md index 1492bc1237f4..caa3536058af 100644 --- a/sdk/eventgrid/azure-eventgrid/README.md +++ b/sdk/eventgrid/azure-eventgrid/README.md @@ -53,6 +53,7 @@ With the `azure-identity` package, you can seamlessly authorize requests in both For example, you can use `DefaultAzureCredential` to construct a client which will authenticate using Azure Active Directory: + ```python from azure.identity import DefaultAzureCredential from azure.eventgrid import EventGridPublisherClient, EventGridEvent @@ -61,6 +62,7 @@ credential = DefaultAzureCredential() endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"] client = EventGridPublisherClient(endpoint, credential) ``` + #### Looking up the endpoint @@ -75,6 +77,7 @@ pass the key as a string into an instance of [AzureKeyCredential][azure-key-cred > **Note:** The Access Key may be found in the azure portal in the "Access Keys" menu of the Event Grid Topic resource. They may also be obtained via the azure CLI, or the `azure-mgmt-eventgrid` library. A guide for getting access keys can be found [here](https://docs.microsoft.com/azure/event-grid/get-access-keys). + ```python import os from azure.eventgrid import EventGridPublisherClient @@ -86,6 +89,7 @@ endpoint = os.environ["EVENTGRID_TOPIC_ENDPOINT"] credential = AzureKeyCredential(topic_key) client = EventGridPublisherClient(endpoint, credential) ``` + > **Note:** A client may also be authenticated via SAS signature, using the `AzureSasCredential`. A sample demonstrating this, is available [here][python-eg-sample-send-using-sas] ([async_version][python-eg-sample-send-using-sas-async]). diff --git a/sdk/eventhub/azure-eventhub/README.md b/sdk/eventhub/azure-eventhub/README.md index 1e63cfc93405..2e2753df8e62 100644 --- a/sdk/eventhub/azure-eventhub/README.md +++ b/sdk/eventhub/azure-eventhub/README.md @@ -112,6 +112,7 @@ The following sections provide several code snippets covering some of the most c Get the partition ids of an Event Hub. + ```python import os from azure.eventhub import EventHubConsumerClient @@ -128,6 +129,7 @@ consumer_client = EventHubConsumerClient.from_connection_string( with consumer_client: pass # consumer_client is now ready to be used. ``` + ### Publish events to an Event Hub @@ -136,6 +138,7 @@ Use the `create_batch` method on `EventHubProducerClient` to create an `EventDat Events may be added to the `EventDataBatch` using the `add` method until the maximum batch size limit in bytes has been reached. + ```python def send_event_data_batch(producer): # Without specifying partition_id or partition_key @@ -144,6 +147,7 @@ def send_event_data_batch(producer): event_data_batch.add(EventData('Single message')) producer.send_batch(event_data_batch) ``` + ### Consume events from an Event Hub diff --git a/sdk/keyvault/azure-keyvault-certificates/README.md b/sdk/keyvault/azure-keyvault-certificates/README.md index 91031b9d13aa..f1a33f772a0e 100644 --- a/sdk/keyvault/azure-keyvault-certificates/README.md +++ b/sdk/keyvault/azure-keyvault-certificates/README.md @@ -39,11 +39,13 @@ See [azure-identity][azure_identity] documentation for more information about ot After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of authentication, you can do the following to create a certificate client (replacing the value of `VAULT_URL` with your vault's URL): + ```python VAULT_URL = os.environ["VAULT_URL"] credential = DefaultAzureCredential() client = CertificateClient(vault_url=VAULT_URL, credential=credential) ``` + > **NOTE:** For an asynchronous client, import `azure.keyvault.certificates.aio`'s `CertificateClient` instead. diff --git a/sdk/keyvault/azure-keyvault-keys/README.md b/sdk/keyvault/azure-keyvault-keys/README.md index 99eb00f497e2..1f9c918d8f7b 100644 --- a/sdk/keyvault/azure-keyvault-keys/README.md +++ b/sdk/keyvault/azure-keyvault-keys/README.md @@ -44,11 +44,13 @@ See [azure-identity][azure_identity] documentation for more information about ot After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of authentication, you can do the following to create a key client (replacing the value of `VAULT_URL` with your vault's URL): + ```python VAULT_URL = os.environ["VAULT_URL"] credential = DefaultAzureCredential() client = KeyClient(vault_url=VAULT_URL, credential=credential) ``` + > **NOTE:** For an asynchronous client, import `azure.keyvault.keys.aio`'s `KeyClient` instead. diff --git a/sdk/keyvault/azure-keyvault-secrets/README.md b/sdk/keyvault/azure-keyvault-secrets/README.md index 324c50e79850..67b04a8271b2 100644 --- a/sdk/keyvault/azure-keyvault-secrets/README.md +++ b/sdk/keyvault/azure-keyvault-secrets/README.md @@ -42,11 +42,13 @@ See [azure-identity][azure_identity] documentation for more information about ot After configuring your environment for the [DefaultAzureCredential][default_cred_ref] to use a suitable method of authentication, you can do the following to create a secret client (replacing the value of `VAULT_URL` with your vault's URL): + ```python VAULT_URL = os.environ["VAULT_URL"] credential = DefaultAzureCredential() client = SecretClient(vault_url=VAULT_URL, credential=credential) ``` + > **NOTE:** For an asynchronous client, import `azure.keyvault.secrets.aio`'s `SecretClient` instead. diff --git a/sdk/metricsadvisor/azure-ai-metricsadvisor/README.md b/sdk/metricsadvisor/azure-ai-metricsadvisor/README.md index 9e0fb09dabf2..a51aaea7a205 100644 --- a/sdk/metricsadvisor/azure-ai-metricsadvisor/README.md +++ b/sdk/metricsadvisor/azure-ai-metricsadvisor/README.md @@ -37,6 +37,7 @@ You will need two keys to authenticate the client: We can use the keys to create a new `MetricsAdvisorClient` or `MetricsAdvisorAdministrationClient`. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorClient @@ -47,9 +48,11 @@ api_key = os.getenv("METRICS_ADVISOR_API_KEY") client = MetricsAdvisorClient(service_endpoint, MetricsAdvisorKeyCredential(subscription_key, api_key)) ``` + + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient @@ -60,6 +63,7 @@ api_key = os.getenv("METRICS_ADVISOR_API_KEY") client = MetricsAdvisorAdministrationClient(service_endpoint, MetricsAdvisorKeyCredential(subscription_key, api_key)) ``` + ## Key concepts @@ -127,6 +131,7 @@ Metrics Advisor lets you create and subscribe to real-time alerts. These alerts Metrics Advisor supports connecting different types of data sources. Here is a sample to ingest data from SQL Server. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient from azure.ai.metricsadvisor.models import ( @@ -180,6 +185,7 @@ data_feed = client.create_data_feed( return data_feed ``` + ### Check ingestion status @@ -187,6 +193,7 @@ return data_feed After we start the data ingestion, we can check the ingestion status. + ```python import datetime from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient @@ -209,6 +216,7 @@ for status in ingestion_status: print("Status: {}".format(status.status)) print("Message: {}\n".format(status.message)) ``` + ### Configure anomaly detection configuration @@ -216,6 +224,7 @@ for status in ingestion_status: While a default detection configuration is automatically applied to each metric, we can tune the detection modes used on our data by creating a customized anomaly detection configuration. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient from azure.ai.metricsadvisor.models import ( @@ -275,6 +284,7 @@ detection_config = client.create_detection_configuration( return detection_config ``` + ### Configure alert configuration @@ -282,6 +292,7 @@ return detection_config Then let's configure in which conditions an alert needs to be triggered. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient from azure.ai.metricsadvisor.models import ( @@ -347,6 +358,7 @@ alert_config = client.create_alert_configuration( return alert_config ``` + ### Query anomaly detection results @@ -354,6 +366,7 @@ return alert_config We can query the alerts and anomalies. + ```python import datetime from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorClient @@ -379,9 +392,11 @@ for result in results: print("Create time: {}".format(result.created_time)) return tolist ``` + + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorClient @@ -401,6 +416,7 @@ for result in results: print("Severity: {}".format(result.severity)) print("Status: {}".format(result.status)) ``` + ### Query incidents @@ -408,6 +424,7 @@ for result in results: We can query the incidents for a detection configuration. + ```python import datetime from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorClient @@ -430,6 +447,7 @@ for result in results: print("Severity: {}".format(result.severity)) print("Status: {}".format(result.status)) ``` + ### Query root causes @@ -437,6 +455,7 @@ for result in results: We can also query the root causes of an incident + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorClient @@ -456,6 +475,7 @@ for result in results: print("Score: {}".format(result.score)) print("Description: {}".format(result.description)) ``` + ### Add hooks for receiving anomaly alerts @@ -463,6 +483,7 @@ for result in results: We can add some hooks so when an alert is triggered, we can get call back. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential, MetricsAdvisorAdministrationClient from azure.ai.metricsadvisor.models import EmailNotificationHook @@ -485,6 +506,7 @@ hook = client.create_hook( return hook ``` + ### Async APIs @@ -496,6 +518,7 @@ See for more information. + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential from azure.ai.metricsadvisor.aio import MetricsAdvisorClient @@ -507,9 +530,11 @@ api_key = os.getenv("METRICS_ADVISOR_API_KEY") client = MetricsAdvisorClient(service_endpoint, MetricsAdvisorKeyCredential(subscription_key, api_key)) ``` + + ```python from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential from azure.ai.metricsadvisor.aio import MetricsAdvisorAdministrationClient @@ -521,6 +546,7 @@ api_key = os.getenv("METRICS_ADVISOR_API_KEY") client = MetricsAdvisorAdministrationClient(service_endpoint, MetricsAdvisorKeyCredential(subscription_key, api_key)) ``` + ## Troubleshooting diff --git a/sdk/search/azure-search-documents/README.md b/sdk/search/azure-search-documents/README.md index 2a356deaa708..10016086a750 100644 --- a/sdk/search/azure-search-documents/README.md +++ b/sdk/search/azure-search-documents/README.md @@ -93,6 +93,7 @@ to get started exploring APIs, but it should be managed carefully.* We can use the api-key to create a new `SearchClient`. + ```python from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchClient @@ -103,6 +104,7 @@ key = os.getenv("AZURE_SEARCH_API_KEY") search_client = SearchClient(service_endpoint, index_name, AzureKeyCredential(key)) ``` + ## Key concepts @@ -197,6 +199,7 @@ defined using convenient `SimpleField`, `SearchableField`, or `ComplexField` models. Indexes can also define suggesters, lexical analyzers, and more. + ```python name = "hotels" fields = [ @@ -218,6 +221,7 @@ index = SearchIndex( result = client.create_index(index) ``` + ### Adding documents to your index @@ -228,6 +232,7 @@ an index in a single batched request. There are to be aware of. + ```python DOCUMENT = { 'Category': 'Hotel', @@ -241,6 +246,7 @@ result = search_client.upload_documents(documents=[DOCUMENT]) print("Upload of new document succeeded: {}".format(result[0].succeeded)) ``` + ### Authenticate in a National Cloud @@ -272,6 +278,7 @@ key. You could get the key from a query, for example, and want to show more information about it or navigate your customer to that document. + ```python from azure.core.credentials import AzureKeyCredential from azure.search.documents import SearchClient @@ -285,6 +292,7 @@ print(" Name: {}".format(result["HotelName"])) print(" Rating: {}".format(result["Rating"])) print(" Category: {}".format(result["Category"])) ``` + ### Async APIs @@ -296,6 +304,7 @@ See for more information. + ```python from azure.core.credentials import AzureKeyCredential from azure.search.documents.aio import SearchClient @@ -309,6 +318,7 @@ async with search_client: async for result in results: print(" Name: {} (rating {})".format(result["HotelName"], result["Rating"])) ``` + ## Troubleshooting diff --git a/sdk/translation/azure-ai-translation-document/README.md b/sdk/translation/azure-ai-translation-document/README.md index 89fa898a02ea..fc05bd25c681 100644 --- a/sdk/translation/azure-ai-translation-document/README.md +++ b/sdk/translation/azure-ai-translation-document/README.md @@ -85,6 +85,7 @@ To use an [API key][cognitive_authentication_api_key] as the `credential` parame pass the key as a string into an instance of [AzureKeyCredential][azure-key-credential]. + ```python from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document import DocumentTranslationClient @@ -94,6 +95,7 @@ key = os.environ["AZURE_DOCUMENT_TRANSLATION_KEY"] document_translation_client = DocumentTranslationClient(endpoint, AzureKeyCredential(key)) ``` + #### Create the client with an Azure Active Directory credential @@ -113,6 +115,7 @@ Once completed, set the values of the client ID, tenant ID, and client secret of `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`. + ```python """DefaultAzureCredential will use the values from these environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET @@ -125,6 +128,7 @@ credential = DefaultAzureCredential() document_translation_client = DocumentTranslationClient(endpoint, credential) ``` + ## Key concepts @@ -166,6 +170,7 @@ poller = document_translation_client.begin_translation("", "< 2) Or multiple different sources can be provided each with their own targets. + ```python import os from azure.core.credentials import AzureKeyCredential @@ -231,6 +236,7 @@ for document in result: elif document.error: print(f"Error Code: {document.error.code}, Message: {document.error.message}\n") ``` + > Note: the target_url for each target language must be unique. @@ -301,6 +307,7 @@ for document in result: Begin translating with documents in multiple source containers to multiple target containers in different languages. + ```python import os from azure.core.credentials import AzureKeyCredential @@ -366,6 +373,7 @@ for document in result: elif document.error: print(f"Error Code: {document.error.code}, Message: {document.error.message}\n") ``` + ### List translation operations @@ -373,6 +381,7 @@ for document in result: Enumerate over the translation operations submitted for the resource. + ```python from azure.core.credentials import AzureKeyCredential from azure.ai.translation.document import DocumentTranslationClient @@ -397,6 +406,7 @@ for operation in operations: print(f"{operation.documents_succeeded_count} succeeded") print(f"{operation.documents_canceled_count} canceled\n") ``` + To see how to use the Document Translation client library with Azure Storage Blob to upload documents, create SAS tokens diff --git a/tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py b/tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py index b91822091e0e..870df01c3696 100644 --- a/tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py +++ b/tools/azure-sdk-tools/ci_tools/snippet_update/python_snippet_updater.py @@ -83,7 +83,7 @@ def update_snippet(file: str) -> None: if name not in snippets.keys(): _LOGGER.error(f'In {file}, failed to find snippet name "{name}".') exit(1) - target_code = "".join([header, "\n```python\n", snippets[name], "\n```\n", ""]) + target_code = "".join([header, "\n\n```python\n", snippets[name], "\n```\n\n", ""]) if body != target_code: _LOGGER.warning(f'Snippet "{name}" is not up to date.') global not_up_to_date