-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[webpubsub] support AAD, Api management proxy #20533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
a21afb3
codegen
msyyc a4d3d15
update test
msyyc c3995d5
update readme.md
msyyc 95d910a
add test
msyyc 090c8c1
from connection string
msyyc a4fdbbb
example
msyyc 156eaa6
Update _version.py
msyyc c374338
api management proxy
msyyc d25309a
Update _web_pub_sub_service_client.py
msyyc ec2ddb6
review
msyyc bebb486
api management
msyyc 859ccc6
fix dependency
msyyc 18ee661
fix dependency
msyyc fe01cff
apim proxy
msyyc 3983c03
edit code in _patch.py instead of generated code
msyyc 9cc207e
review
msyyc b4bfd53
fix broken link
msyyc be9026e
Update setup.py
msyyc e296dbe
Update dev_requirements.txt
msyyc a367a37
fix ci
msyyc 9f954b2
fix ci
msyyc 0fd5b47
fix ci failure of windows_python27
msyyc b95cbee
Update sdk/webpubsub/azure-messaging-webpubsubservice/examples/send_m…
msyyc 1ce7f49
Update sdk/webpubsub/azure-messaging-webpubsubservice/examples/send_m…
msyyc d34c3a0
Update README.md
msyyc 3dd7bea
review
msyyc 8dcfcb9
regenerate code
msyyc 5fa9d25
fix ci
msyyc 55537f7
fix ci
msyyc f49f435
move operation onto client
msyyc 132f1bc
review
msyyc 9534e09
Update send_messages_connection_string_apim_proxy.py
msyyc 3bf7b82
Update send_messages_aad_apim_proxy.py
msyyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,14 +43,48 @@ python -m pip install azure-messaging-webpubsubservice | |
|
|
||
| ### Authenticating the client | ||
|
|
||
| In order to interact with the Azure WebPubSub service, you'll need to create an instance of the [WebPubSubServiceClient][webpubsubservice_client_class] class. In order to authenticate against the service, you need to pass in an AzureKeyCredential instance with endpoint and api key. The endpoint and api key can be found on the azure portal. | ||
| #### 1. Create with an API Key Credential | ||
|
|
||
| You can get the [API key][api_key] or [Connection string][connection_string] in the [Azure Portal][azure_portal]. | ||
| Once you have the value for the API key, you can pass it as a string into an instance of [AzureKeyCredential][azure-key-credential]. | ||
| Use the key as the credential parameter to authenticate the client: | ||
|
|
||
| ```python | ||
| >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient | ||
| >>> from azure.core.credentials import AzureKeyCredential | ||
| >>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret')) | ||
| >>> client | ||
| <WebPubSubServiceClient endpoint:'<endpoint>'> | ||
|
|
||
| >>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential("<api_key>")) | ||
| ``` | ||
|
|
||
| Once you have the value for the connection string, you can pass it as a string into the function `from_connection_string` and it will | ||
msyyc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| authenticate the client: | ||
| ```python | ||
| >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient | ||
|
|
||
| >>> client = WebPubSubServiceClient.from_connection_string(connection_string='<connection_string>') | ||
| ``` | ||
|
|
||
| #### 2. Create with an Azure Active Directory Credential | ||
| To use an [Azure Active Directory (AAD) token credential][authenticate_with_token], | ||
msyyc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| provide an instance of the desired credential type obtained from the | ||
| [azure-identity][azure_identity_credentials] library. | ||
|
|
||
| To authenticate with AAD, you must first [pip][pip] install [`azure-identity`][azure_identity_pip] and | ||
| [enable AAD authentication on your Webpubsub resource][enable_aad] | ||
|
|
||
| After setup, you can choose which type of [credential][azure_identity_credentials] from azure.identity to use. | ||
| As an example, [DefaultAzureCredential][default_azure_credential] | ||
| can be used to authenticate the client: | ||
|
|
||
| Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: | ||
| AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET | ||
|
|
||
| Use the returned token credential to authenticate the client: | ||
|
|
||
| ```python | ||
| >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient | ||
| >>> from azure.identity import DefaultAzureCredential | ||
| >>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=DefaultAzureCredential()) | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
@@ -59,22 +93,16 @@ In order to interact with the Azure WebPubSub service, you'll need to create an | |
|
|
||
| ```python | ||
| >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient | ||
| >>> from azure.core.credentials import AzureKeyCredential | ||
| >>> from azure.messaging.webpubsubservice.rest import build_send_to_all_request | ||
| >>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret')) | ||
| >>> request = build_send_to_all_request('default', json={ 'Hello': 'webpubsub!' }) | ||
| >>> request | ||
| <HttpRequest [POST], url: '/api/hubs/default/:send?api-version=2020-10-01'> | ||
| >>> response = client.send_request(request) | ||
| >>> response | ||
| <RequestsTransportResponse: 202 Accepted> | ||
| >>> response.status_code | ||
| 202 | ||
| >>> from azure.identity import DefaultAzureCredential | ||
| >>> from azure.core.exceptions import HttpResponseError | ||
|
|
||
| >>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=DefaultAzureCredential()) | ||
| >>> with open('file.json', 'r') as f: | ||
| >>> request = build_send_to_all_request('ahub', content=f, content_type='application/json') | ||
| >>> response = client.send_request(request) | ||
| >>> print(response) | ||
| <RequestsTransportResponse: 202 Accepted> | ||
| try: | ||
msyyc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| client.send_to_all('ahub', content=f, content_type='application/json') | ||
| except HttpResponseError as e: | ||
| print('service responds error: {}'.format(e.response.json())) | ||
|
|
||
| ``` | ||
|
|
||
| ## Key concepts | ||
|
|
@@ -107,10 +135,32 @@ This SDK uses Python standard logging library. | |
| You can configure logging print out debugging information to the stdout or anywhere you want. | ||
|
|
||
| ```python | ||
| import sys | ||
| import logging | ||
| from azure.identity import DefaultAzureCredential | ||
| >>> from azure.messaging.webpubsubservice import WebPubSubServiceClient | ||
|
|
||
| # Create a logger for the 'azure' SDK | ||
| logger = logging.getLogger('azure') | ||
| logger.setLevel(logging.DEBUG) | ||
|
|
||
| logging.basicConfig(level=logging.DEBUG) | ||
| ```` | ||
| # Configure a console output | ||
| handler = logging.StreamHandler(stream=sys.stdout) | ||
| logger.addHandler(handler) | ||
|
|
||
| endpoint = "<endpoint>" | ||
| credential = DefaultAzureCredential() | ||
|
|
||
| # This client will log detailed information about its HTTP sessions, at DEBUG level | ||
| client = WebPubSubServiceClient(endpoint=endpoint, credential=credential, logging_enable=True) | ||
| ``` | ||
|
|
||
| Similarly, `logging_enable` can enable detailed logging for a single call, | ||
| even when it isn't enabled for the client: | ||
|
|
||
| ```python | ||
| result = client.send_to_all(..., logging_enable=True) | ||
| ``` | ||
|
|
||
| Http request and response details are printed to stdout with this logging config. | ||
|
|
||
|
|
@@ -139,10 +189,19 @@ additional questions or comments. | |
| [webpubsubservice_docs]: https://aka.ms/awps/doc | ||
| [azure_cli]: https://docs.microsoft.com/cli/azure | ||
| [azure_sub]: https://azure.microsoft.com/free/ | ||
| [webpubsubservice_client_class]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/webpubsub/azure-messaging-webpubsubservice/azure/messaging/webpubsubservice/__init__.py | ||
| [package]: https://pypi.org/project/azure-messaging-webpubsubservice/ | ||
| [default_cred_ref]: https://aka.ms/azsdk-python-identity-default-cred-ref | ||
| [cla]: https://cla.microsoft.com | ||
| [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ | ||
| [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
| [coc_contact]: mailto:[email protected] | ||
| [authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token | ||
| [azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials | ||
| [azure_identity_pip]: https://pypi.org/project/azure-identity/ | ||
| [default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential | ||
| [pip]: https://pypi.org/project/pip/ | ||
| [enable_aad]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance | ||
| [api_key]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization | ||
| [connection_string]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-websocket-connect?tabs=browser#authorization | ||
| [azure_portal]: https://docs.microsoft.com/azure/azure-web-pubsub/howto-develop-create-instance | ||
| [azure-key-credential]: https://aka.ms/azsdk-python-core-azurekeycredential | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.