Skip to content

Commit

Permalink
readme update to opt in code snippet tool (#28889)
Browse files Browse the repository at this point in the history
* app config

* updates

* update readmes

* Update sdk/keyvault/azure-keyvault-certificates/README.md

Co-authored-by: McCoy Patiño <[email protected]>

* update

* update

* update

* update

* Update sdk/keyvault/azure-keyvault-keys/README.md

Co-authored-by: McCoy Patiño <[email protected]>

* update

* update

* update

* Update sdk/keyvault/azure-keyvault-secrets/README.md

Co-authored-by: McCoy Patiño <[email protected]>

* Update sdk/keyvault/azure-keyvault-secrets/samples/hello_world.py

Co-authored-by: McCoy Patiño <[email protected]>

* Updates

* update

* update

* update

* update

* update

* updates

* update

* update

* update

* updates

* update

* update

* update

* update

* update

* update

* update

---------

Co-authored-by: McCoy Patiño <[email protected]>
  • Loading branch information
xiangyan99 and mccoyp committed Feb 24, 2023
1 parent a4e362d commit 49a2f49
Show file tree
Hide file tree
Showing 49 changed files with 1,156 additions and 748 deletions.
72 changes: 41 additions & 31 deletions sdk/appconfiguration/azure-appconfiguration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ Alternatively, get the connection string from the Azure Portal.

Once you have the value of the connection string, you can create the AzureAppConfigurationClient:

<!-- SNIPPET:hello_world_sample.create_app_config_client -->
```python
import os
from azure.appconfiguration import AzureAppConfigurationClient
CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING']

connection_str = "<connection_string>"
client = AzureAppConfigurationClient.from_connection_string(connection_str)
# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)
```
<!-- END SNIPPET -->

#### Use AAD token

Expand Down Expand Up @@ -162,6 +166,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.

<!-- SNIPPET:hello_world_advanced_sample.create_config_setting -->
```python
config_setting = ConfigurationSetting(
key="MyKey",
Expand All @@ -172,86 +177,91 @@ config_setting = ConfigurationSetting(
)
added_config_setting = client.add_configuration_setting(config_setting)
```
<!-- END SNIPPET -->

- set_configuration_setting creates a setting if it doesn't exist or overrides an existing setting.

<!-- SNIPPET:hello_world_advanced_sample.set_config_setting -->
```python
config_setting = ConfigurationSetting(
key="MyKey",
label="MyLabel",
value="my set value",
content_type="my set content type",
tags={"my set tag": "my set tag value"}
)
returned_config_setting = client.set_configuration_setting(config_setting)
added_config_setting.value = "new value"
added_config_setting.content_type = "new content type"
updated_config_setting = client.set_configuration_setting(config_setting)
```
<!-- END SNIPPET -->

### Get a Configuration Setting

Get a previously stored Configuration Setting.

<!-- SNIPPET:hello_world_sample.get_config_setting -->
```python
fetched_config_setting = client.get_configuration_setting(
key="MyKey", label="MyLabel"
key="MyKey"
)
```
<!-- END SNIPPET -->

### Delete a Configuration Setting

Delete an existing Configuration Setting.

<!-- SNIPPET:hello_world_advanced_sample.delete_config_setting -->
```python
deleted_config_setting = client.delete_configuration_setting(
key="MyKey", label="MyLabel"
client.delete_configuration_setting(
key="MyKey",
label="MyLabel",
)
```
<!-- END SNIPPET -->

### List Configuration Settings

List all configuration settings filtered with label_filter and/or key_filter.

<!-- SNIPPET:hello_world_advanced_sample.list_config_setting -->
```python

filtered_listed = client.list_configuration_settings(
label_filter="My*", key_filter="My*"
)
for item in filtered_listed:
pass # do something

config_settings = client.list_configuration_settings(label_filter="MyLabel")
for item in config_settings:
print_configuration_setting(item)
```
<!-- END SNIPPET -->

### Async APIs

Async client is supported.
To use the async client library, import the AzureAppConfigurationClient from package azure.appconfiguration.aio instead of azure.appconfiguration

<!-- SNIPPET:hello_world_sample_async.create_app_config_client -->
```python
import os
from azure.appconfiguration.aio import AzureAppConfigurationClient
CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING']

connection_str = "<connection_string>"
async_client = AzureAppConfigurationClient.from_connection_string(connection_str)
# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)
```
<!-- END SNIPPET -->

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:

<!-- SNIPPET:hello_world_sample_async.get_config_setting -->
```python
fetched_config_setting = await async_client.get_configuration_setting(
key="MyKey", label="MyLabel"
fetched_config_setting = await client.get_configuration_setting(
key="MyKey"
)
```
<!-- END SNIPPET -->

To use list_configuration_settings, call it synchronously and iterate over the returned async iterator asynchronously

<!-- SNIPPET:hello_world_advanced_sample_async.list_config_setting -->
```python

filtered_listed = async_client.list_configuration_settings(
label_filter="My*", key_filter="My*"
)
async for item in filtered_listed:
pass # do something

config_settings = client.list_configuration_settings(label_filter="MyLabel")
async for item in config_settings:
print_configuration_setting(item)
```
<!-- END SNIPPET -->

## Troubleshooting

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def main():
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)

print("Add new configuration setting")
# [START create_config_setting]
config_setting = ConfigurationSetting(
key="MyKey",
label="MyLabel",
Expand All @@ -31,27 +32,34 @@ def main():
tags={"my tag": "my tag value"}
)
added_config_setting = client.add_configuration_setting(config_setting)
# [END create_config_setting]
print("New configuration setting:")
print_configuration_setting(added_config_setting)
print("")

print("Set configuration setting")
# [START set_config_setting]
added_config_setting.value = "new value"
added_config_setting.content_type = "new content type"
updated_config_setting = client.set_configuration_setting(config_setting)
# [END set_config_setting]
print_configuration_setting(updated_config_setting)
print("")

print("List configuration settings")
# [START list_config_setting]
config_settings = client.list_configuration_settings(label_filter="MyLabel")
for item in config_settings:
print_configuration_setting(item)
# [END list_config_setting]

print("Delete configuration setting")
# [START delete_config_setting]
client.delete_configuration_setting(
key="MyKey",
label="MyLabel",
)
# [END delete_config_setting]

if __name__ == "__main__":
main()
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ async def main():
print("")

print("List configuration settings")
# [START list_config_setting]
config_settings = client.list_configuration_settings(label_filter="MyLabel")
async for item in config_settings:
print_configuration_setting(item)
# [END list_config_setting]

print("Delete configuration setting")
await client.delete_configuration_setting(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
USAGE: python hello_world_sample.py
"""

from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
from util import print_configuration_setting, get_connection_string
from azure.appconfiguration import ConfigurationSetting
from util import print_configuration_setting

def main():
CONNECTION_STRING = get_connection_string()
# [START create_app_config_client]
import os
from azure.appconfiguration import AzureAppConfigurationClient
CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING']

# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)
# [END create_app_config_client]

print("Set new configuration setting")
config_setting = ConfigurationSetting(
Expand All @@ -35,9 +39,11 @@ def main():
print("")

print("Get configuration setting")
# [START get_config_setting]
fetched_config_setting = client.get_configuration_setting(
key="MyKey"
)
# [END get_config_setting]
print("Fetched configuration setting:")
print_configuration_setting(fetched_config_setting)
print("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@

import asyncio
from azure.appconfiguration import ConfigurationSetting
from azure.appconfiguration.aio import AzureAppConfigurationClient
from util import print_configuration_setting, get_connection_string
from util import print_configuration_setting

async def main():
CONNECTION_STRING = get_connection_string()
# [START create_app_config_client]
import os
from azure.appconfiguration.aio import AzureAppConfigurationClient
CONNECTION_STRING = os.environ['APPCONFIGURATION_CONNECTION_STRING']

# Create app config client
client = AzureAppConfigurationClient.from_connection_string(CONNECTION_STRING)
# [END create_app_config_client]

print("Set new configuration setting")
config_setting = ConfigurationSetting(
Expand All @@ -37,9 +40,11 @@ async def main():
print("")

print("Get configuration setting")
# [START get_config_setting]
fetched_config_setting = await client.get_configuration_setting(
key="MyKey"
)
# [END get_config_setting]
print("Fetched configuration setting:")
print_configuration_setting(fetched_config_setting)
print("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export ACCOUNT_KEY=$(az cognitiveservices account keys list \

Once you've populated the `ACCOUNT_REGION` and `ACCOUNT_KEY` environment variables, you can create the [ComputerVisionClient][ref_computervisionclient] client object.

```Python
```python
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
Expand Down Expand Up @@ -128,7 +128,7 @@ The following sections provide several code snippets covering some of the most c

You can analyze an image for certain features with [`analyze_image`][ref_computervisionclient_analyze_image]. Use the [`visual_features`][ref_computervision_model_visualfeatures] property to set the types of analysis to perform on the image. Common values are `VisualFeatureTypes.tags` and `VisualFeatureTypes.description`.

```Python
```python
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"

image_analysis = client.analyze_image(url,visual_features=[VisualFeatureTypes.tags])
Expand All @@ -141,7 +141,7 @@ for tag in image_analysis.tags:

Review the subject domains used to analyze your image with [`list_models`][ref_computervisionclient_list_models]. These domain names are used when [analyzing an image by domain](#analyze-an-image-by-domain). An example of a domain is `landmarks`.

```Python
```python
models = client.list_models()

for x in models.models_property:
Expand All @@ -152,7 +152,7 @@ for x in models.models_property:

You can analyze an image by subject domain with [`analyze_image_by_domain`][ref_computervisionclient_analyze_image_by_domain]. Get the [list of supported subject domains](#get-subject-domain-list) in order to use the correct domain name.

```Python
```python
domain = "landmarks"
url = "https://images.pexels.com/photos/338515/pexels-photo-338515.jpeg"
language = "en"
Expand All @@ -168,7 +168,7 @@ for landmark in analysis.result["landmarks"]:

You can get a language-based text description of an image with [`describe_image`][ref_computervisionclient_describe_image]. Request several descriptions with the `max_description` property if you are doing text analysis for keywords associated with the image. Examples of a text description for the following image include `a train crossing a bridge over a body of water`, `a large bridge over a body of water`, and `a train crossing a bridge over a large body of water`.

```Python
```python
domain = "landmarks"
url = "http://www.public-domain-photos.com/free-stock-photos-4/travel/san-francisco/golden-gate-bridge-in-san-francisco.jpg"
language = "en"
Expand All @@ -185,7 +185,7 @@ for caption in analysis.captions:

You can get any handwritten or printed text from an image. This requires two calls to the SDK: [`read`][ref_computervisionclient_read] and [`get_read_result`][ref_computervisionclient_get_read_result]. The call to read is asynchronous. In the results of the get_read_result call, you need to check if the first call completed with [`OperationStatusCodes`][ref_computervision_model_operationstatuscodes] before extracting the text data. The results include the text as well as the bounding box coordinates for the text.

```Python
```python
# import models
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes

Expand Down Expand Up @@ -218,7 +218,7 @@ You can generate a thumbnail (JPG) of an image with [`generate_thumbnail`][ref_c

This example uses the [Pillow][pypi_pillow] package to save the new thumbnail image locally.

```Python
```python
from PIL import Image
import io

Expand All @@ -242,7 +242,7 @@ When you interact with the [ComputerVisionClient][ref_computervisionclient] clie

For example, if you try to analyze an image with an invalid key, a `401` error is returned. In the following snippet, the [error][ref_httpfailure] is handled gracefully by catching the exception and displaying additional information about the error.

```Python
```python

domain = "landmarks"
url = "http://www.public-domain-photos.com/free-stock-photos-4/travel/san-francisco/golden-gate-bridge-in-san-francisco.jpg"
Expand Down
Loading

0 comments on commit 49a2f49

Please sign in to comment.