Skip to content
Closed
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a21afb3
codegen
msyyc Sep 3, 2021
a4d3d15
update test
msyyc Sep 6, 2021
c3995d5
update readme.md
msyyc Sep 6, 2021
95d910a
add test
msyyc Sep 8, 2021
090c8c1
from connection string
msyyc Sep 8, 2021
a4fdbbb
example
msyyc Sep 8, 2021
156eaa6
Update _version.py
msyyc Sep 8, 2021
c374338
api management proxy
msyyc Sep 8, 2021
d25309a
Update _web_pub_sub_service_client.py
msyyc Sep 8, 2021
ec2ddb6
review
msyyc Sep 9, 2021
bebb486
api management
msyyc Sep 9, 2021
859ccc6
fix dependency
msyyc Sep 10, 2021
18ee661
fix dependency
msyyc Sep 10, 2021
fe01cff
apim proxy
msyyc Sep 10, 2021
3983c03
edit code in _patch.py instead of generated code
msyyc Sep 13, 2021
9cc207e
review
msyyc Sep 13, 2021
b4bfd53
fix broken link
msyyc Sep 13, 2021
be9026e
Update setup.py
msyyc Sep 13, 2021
e296dbe
Update dev_requirements.txt
msyyc Sep 14, 2021
a367a37
fix ci
msyyc Sep 14, 2021
9f954b2
fix ci
msyyc Sep 14, 2021
0fd5b47
fix ci failure of windows_python27
msyyc Sep 15, 2021
b95cbee
Update sdk/webpubsub/azure-messaging-webpubsubservice/examples/send_m…
msyyc Sep 15, 2021
1ce7f49
Update sdk/webpubsub/azure-messaging-webpubsubservice/examples/send_m…
msyyc Sep 15, 2021
d34c3a0
Update README.md
msyyc Sep 15, 2021
3dd7bea
review
msyyc Sep 16, 2021
8dcfcb9
regenerate code
msyyc Sep 16, 2021
5fa9d25
fix ci
msyyc Sep 16, 2021
55537f7
fix ci
msyyc Sep 16, 2021
f49f435
move operation onto client
msyyc Sep 17, 2021
132f1bc
review
msyyc Sep 22, 2021
9534e09
Update send_messages_connection_string_apim_proxy.py
msyyc Sep 22, 2021
3bf7b82
Update send_messages_aad_apim_proxy.py
msyyc Sep 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/tox/allowed_pylint_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"azure-purview-scanning",
"azure-purview-catalog",
"azure-purview-account",
"azure-messaging-webpubsubservice",
"azure-messaging-nspkg",
"azure-agrifood-farming",
"azure-eventhub",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

## 1.0.0b2 (Unreleased)

- Add operations to client
- Support AAD
- Support Api Management Proxy

## 1.0.0b1 (2021-04-27)

Initial version
- Initial version

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ include azure/__init__.py
include azure/messaging/__init__.py
include LICENSE.txt
recursive-include tests *.py
recursive-include examples *.py *.md
recursive-include samples *.py *.md
103 changes: 81 additions & 22 deletions sdk/webpubsub/azure-messaging-webpubsubservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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],
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
Expand All @@ -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:
client.web_pub_sub.send_to_all('ahub', content=f, content_type='application/json')
except HttpResponseError as e:
print('service responds error: {}'.format(e.response.json()))

```

## Key concepts
Expand Down Expand Up @@ -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.web_pub_sub.send_to_all(..., logging_enable=True)
```

Http request and response details are printed to stdout with this logging config.

Expand Down Expand Up @@ -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
Loading