Skip to content
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

Azure webpubsub service initial client library version #18199

Merged
merged 40 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9dce353
Initial revision
johanste Mar 23, 2021
dcbd2c8
Update example and README
johanste Mar 23, 2021
ced586b
Rename preparers to builder, add auth builder.
johanste Mar 27, 2021
96eed1d
Added license.txt and setup.cfg
johanste Mar 27, 2021
23dbcb7
Update the sign algo to generate the expected jwt
vicancy Apr 6, 2021
942eb30
Merge pull request #2 from johanste/vicancy-patch-1
johanste Apr 13, 2021
425458f
Remove use of datetime.utcnow
johanste Apr 13, 2021
c2c1fcb
Fix spelling mistake in roles claim
johanste Apr 13, 2021
84290d5
Fix misspelled role
johanste Apr 14, 2021
c5d189b
Merge remote-tracking branch 'private/webpubsub' into webpubsub
johanste Apr 21, 2021
c301168
fixed typos in the README examples
johanste Apr 21, 2021
cba5a7b
Added from_connection_string constructor, fixed UA string
johanste Apr 21, 2021
c5e6890
update readme.md to follow conventions
johanste Apr 22, 2021
ae8df54
Pylint fix
johanste Apr 22, 2021
5833035
Readme fixes, typing_extension removed as dependency
johanste Apr 22, 2021
5645f0b
Suppressed warning from wrong version of pylint. Fixed
johanste Apr 22, 2021
e41354b
Update to later codegen
johanste Apr 23, 2021
8d3dad8
Added async client
johanste Apr 23, 2021
62e5140
Newcodegen (#4)
iscai-msft Apr 23, 2021
c745cdb
Updated to newer codegen for web pubsub (#3)
johanste Apr 23, 2021
a73b516
Fix encoding/decoding issue with pyjwt, removed decodepolicy
johanste Apr 23, 2021
b25ee68
Merge branch 'webpubsub' of github.com:johanste/azure-sdk-for-python …
johanste Apr 23, 2021
a7e86f3
Actually remove decode policy
johanste Apr 23, 2021
fcb9b9e
Changed to correct version of pylint, suppressed errors
johanste Apr 23, 2021
3f4c547
pushed ignore for pypi link in the readme. it'll come in later
scbedd Apr 24, 2021
0bf467c
Updated dev_requirement to local nspkg
johanste Apr 24, 2021
ab190eb
Merge branch 'webpubsub' of github.com:johanste/azure-sdk-for-python …
johanste Apr 24, 2021
a0c012d
Fix structure of README.md
johanste Apr 24, 2021
d050ec0
Merge remote-tracking branch 'upstream/master' into webpubsub
johanste Apr 24, 2021
6496bdc
Fix path for azure-messaging-nspkg
johanste Apr 24, 2021
67644aa
Ignore README for azure-messaging-nspkg
johanste Apr 24, 2021
ce0c1b6
Update shared reqs
johanste Apr 24, 2021
421ba53
Updated examples
johanste Apr 24, 2021
f4d8569
Merge branch 'webpubsub' of github.com:johanste/azure-sdk-for-python …
johanste Apr 24, 2021
8323796
Allow pyjwt, other frozen dependencies for webpubsub
johanste Apr 24, 2021
4d43382
Include examples in MANIFEST
johanste Apr 24, 2021
d4d3b59
Freeze pyjwt
johanste Apr 24, 2021
e9f9abd
Open dependency range for nspkg deps. Cannot change
johanste Apr 25, 2021
8f65a84
Merge branch 'master' into webpubsub
johanste Apr 26, 2021
60fb473
add support from build_authentication_token from connection strings
johanste Apr 26, 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
4 changes: 4 additions & 0 deletions sdk/signalr/azure-messaging-webpubsubservice/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## 1.0.0b1

Initial version

21 changes: 21 additions & 0 deletions sdk/signalr/azure-messaging-webpubsubservice/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions sdk/signalr/azure-messaging-webpubsubservice/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include *.md
include azure/__init__.py
include azure/messaging/__init__.py
include LICENSE.txt
recursive-include tests *.py
recursive-include samples *.py *.md
142 changes: 142 additions & 0 deletions sdk/signalr/azure-messaging-webpubsubservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# AzureWebPubSub Service client library for Python

[Azure Web PubSub Service](https://aka.ms/awps/doc) is a service that enables you to build real-time messaging web applications using WebSockets and the publish-subscribe pattern. Any platform supporting WebSocket APIs can connect to the service easily, e.g. web pages, mobile applications, edge devices, etc. The service manages the WebSocket connections for you and allows up to 100K \*concurrent connections. It provides powerful APIs for you to manage these clients and deliver real-time messages.

Any scenario that requires real-time publish-subscribe messaging between server and clients or among clients, can use Azure Web PubSub service. Traditional real-time features that often require polling from server or submitting HTTP requests, can also use Azure Web PubSub service.

We list some examples that are good to use Azure Web PubSub service:

- **High frequency data updates:** gaming, voting, polling, auction.
- **Live dashboards and monitoring:** company dashboard, financial market data, instant sales update, multi-player game leader board, and IoT monitoring.
- **Cross-platform live chat:** live chat room, chat bot, on-line customer support, real-time shopping assistant, messenger, in-game chat, and so on.
- **Real-time location on map:** logistic tracking, delivery status tracking, transportation status updates, GPS apps.
- **Real-time targeted ads:** personalized real-time push ads and offers, interactive ads.
- **Collaborative apps:** coauthoring, whiteboard apps and team meeting software.
- **Push instant notifications:** social network, email, game, travel alert.
- **Real-time broadcasting:** live audio/video broadcasting, live captioning, translating, events/news broadcasting.
- **IoT and connected devices:** real-time IoT metrics, remote control, real-time status, and location tracking.
- **Automation:** real-time trigger from upstream events.

Use the client library to:

- Send messages to hubs and groups.
- Send messages to particular users and connections.
- Organize users and connections into groups.
- Close connections
- Grant/revoke/check permissions for an existing connection

[Source code](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/signalr/azure-messaging-webpubsubservice) | [Package (Pypi)][package] | [API reference documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/signalr/azure-messaging-webpubsubservice) | [Product documentation][webpubsubservice_docs]

## Getting started

### Installating the package

```bash
python -m pip install azure-messaging-webpubsubservice
```

#### Prequisites

- Python 2.7, or 3.6 or later is required to use this package.
- You need an [Azure subscription][azure_sub], and a [Azure WebPubSub service instance][webpubsubservice_docs] to use this package.
- An existing Azure Web PubSub service instance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a duplicate of the line above.


### 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.

```python
>>> from azure.messaging.webpubsubservice import WebPubSubServiceClient
>>> from azure.core.credentials import AzureKeyCredential
>>> client = WebPubSubServiceClient(endpoint='<endpoint>', credential=AzureKeyCredential('somesecret'))
>>> client
<WebPubSubServiceClient endpoint:'<endpoint>'>
```

### Sending a request

```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
>>> 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>
```

## Key concepts

### Hub

Hub is a logical set of connections. All connections to Web PubSub connect to a specific hub. Messages that are broadcast to the hub are dispatched to all connections to that hub. For example, hub can be used for different applications, different applications can share one Azure Web PubSub service by using different hub names.

### Group

Group allow broadcast messages to a subset of connections to the hub. You can add and remove users and connections as needed. A client can join multiple groups, and a group can contain multiple clients.

### User

Connections to Web PubSub can belong to one user. A user might have multiple connections, for example when a single user is connected across multiple devices or multiple browser tabs.

### Connection

Connections, represented by a connection id, represent an individual websocket connection to the Web PubSub service. Connection id is always unique.

### Message

A message is either a UTF-8 encoded string, json or raw binary data.

## Troubleshooting

### Logging

This SDK uses Python standard logging library.
You can configure logging print out debugging information to the stdout or anywhere you want.

```python
import logging

logging.basicConfig(level=logging.DEBUG)
````

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

## Contributing

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.

This project has adopted the
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.

<!-- LINKS -->
[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/master/sdk/signalr/azure-messaging-webpubsubservice/azure/messaging/webpubsubservice/__init__.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the azure-messaging-webpubsubservice into a new webpubsub folder instead of using signalr?

[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:opencode@microsoft.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

__all__ = ["build_authentication_token", "WebPubSubServiceClient"]

from datetime import datetime, timedelta
from typing import TYPE_CHECKING

import jwt

import azure.core.credentials as corecredentials
import azure.core.pipeline as corepipeline
import azure.core.pipeline.policies as corepolicies
import azure.core.pipeline.transport as coretransport


# Temporary location for types that eventually graduate to Azure Core
from .core import rest as corerest
from ._version import VERSION as _VERSION
from ._policies import JwtCredentialPolicy
from ._utils import UTC as _UTC

if TYPE_CHECKING:
from azure.core.pipeline.policies import HTTPPolicy, SansIOHTTPPolicy
from typing import Any, List, cast, Type, TypeVar
ClientType = TypeVar('ClientType', bound='WebPubSubServiceClient')


def build_authentication_token(endpoint, hub, key, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a from_connection_string version of this function.
Do you have any comments on this?

"""Build an authentication token for the given endpoint, hub using the provided key.

:param endpoint: HTTP or HTTPS endpoint for the WebPubSub service instance.
:type endpoint: ~str
:param hub: The hub to give access to.
:type hub: ~str
:param key: Key to sign the token with.
:type key: ~str
:keyword ttl: Optional ttl timedelta for the token. Default is 1 hour.
:type ttl: ~datetime.timedelta
:keyword user: Optional user name (subject) for the token. Default is no user.
:type user: ~str
:keyword roles: Roles for the token.
:type roles: typing.List[str]. Default is no roles.
:returns: ~dict containing the web socket endpoint, the token and a url with the generated access token.
:rtype: ~dict


Example:
>>> build_authentication_token('https://contoso.com/api/webpubsub', hub='theHub', key='123')
{
'baseUrl': 'wss://contoso.com/api/webpubsub/client/hubs/theHub',
'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ...',
'url': 'wss://contoso.com/api/webpubsub/client/hubs/theHub?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ...'
}
"""
user = kwargs.pop("user", None)
ttl = kwargs.pop("ttl", timedelta(hours=1))
roles = kwargs.pop('roles', [])
endpoint = endpoint.lower()
if not endpoint.startswith("http://") and not endpoint.startswith("https://"):
raise ValueError(
"Invalid endpoint: '{}' has unknown scheme - expected 'http://' or 'https://'".format(
endpoint
)
)

# Ensure endpoint has no trailing slash
endpoint = endpoint.rstrip("/")

# Switch from http(s) to ws(s) scheme
client_endpoint = "ws" + endpoint[4:]
client_url = "{}/client/hubs/{}".format(client_endpoint, hub)
audience = "{}/client/hubs/{}".format(endpoint, hub)

payload = {"aud": audience, "iat": datetime.now(tz=_UTC), "exp": datetime.now(tz=_UTC) + ttl}
if user:
payload["sub"] = user
if roles:
payload["role"] = roles

token = jwt.encode(payload, key, algorithm="HS256")
return {
"baseUrl": client_url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the casing here a particular convention for this value?

"token": token,
"url": "{}?access_token={}".format(client_url, token),
}


class WebPubSubServiceClient(object):
def __init__(self, endpoint, credential, **kwargs):
# type: (str, corecredentials.AzureKeyCredential, Any) -> None
"""Create a new WebPubSubServiceClient instance

:param endpoint: Endpoint to connect to.
:type endpoint: ~str
:param credential: Credentials to use to connect to endpoint.
:type credential: ~azure.core.credentials.AzureKeyCredentials
:keyword api_version: Api version to use when communicating with the service.
:type api_version: str
:keyword user: User to connect as. Optional.
:type user: ~str
"""
self.endpoint = endpoint.rstrip("/")
transport = kwargs.pop("transport", None) or coretransport.RequestsTransport(
**kwargs
)
kwargs.setdefault('sdk_moniker', 'messaging-webpubsubservice/{}'.format(_VERSION))
policies = [
corepolicies.HeadersPolicy(**kwargs),
corepolicies.UserAgentPolicy(**kwargs),
corepolicies.RetryPolicy(**kwargs),
corepolicies.ProxyPolicy(**kwargs),
corepolicies.CustomHookPolicy(**kwargs),
corepolicies.RedirectPolicy(**kwargs),
JwtCredentialPolicy(credential, kwargs.get("user", None)),
corepolicies.NetworkTraceLoggingPolicy(**kwargs),
] # type: Any
self._pipeline = corepipeline.Pipeline(
transport,
policies,
) # type: corepipeline.Pipeline


@classmethod
def from_connection_string(cls, connection_string, **kwargs):
# type: (Type[ClientType], str, Any) -> ClientType
for invalid_keyword_arg in ('endpoint', 'accesskey'):
if invalid_keyword_arg in kwargs:
raise TypeError('Unknown argument {}'.format(invalid_keyword_arg))

for segment in connection_string.split(";"):
if '=' in segment:
key, value = segment.split('=', maxsplit=1)
key = key.lower()
if key == 'version':
# The name in the connection string != the name for the constructor.
# Let's map it to whatthe constructor actually wants...
key = 'api_version'
kwargs[key] = value
else:
raise ValueError("Malformed connection string - expected 'key=value', got {}".format(segment))

if 'endpoint' not in kwargs:
raise ValueError("connection_string missing 'endpoint' field")

if 'accesskey' not in kwargs:
raise ValueError("connection_string missing 'accesskey' field")

kwargs['credential'] = corecredentials.AzureKeyCredential(kwargs.pop('accesskey'))
return cls(**kwargs)

def __repr__(self):
return "<WebPubSubServiceClient> endpoint:'{}'".format(self.endpoint)

def _format_url(self, url):
# type: (str) -> str
assert self.endpoint[-1] != "/", "My endpoint should not have a trailing slash"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be kind of strange to encounter an AssertError when constructing the client?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that scenario, this is truly an assert (we are stripping the trailing slash before calling the method). However, when looking at the code, the endpoint is a read/write attribute (which it probably shouldn't be) and the user could have set it to a value that does not conform to our assumptions. I may want to change self.endpoint to a read-only property...

return "/".join([self.endpoint, url.lstrip("/")])

def send_request(self, request, **kwargs):
# type: (corerest.HttpRequest, Any) -> corerest.HttpResponse
"""Runs the network request through the client's chained policies.

:param request: The network request you want to make. Required.
:type request: ~corerest.HttpRequest
:keyword bool stream: Whether the response payload will be streamed. Defaults to False
:return: The response of your network call.
:rtype: ~corerest.HttpResponse
"""
kwargs.setdefault("stream", False)
request.url = self._format_url(
request.url
) # BUGBUG - should create new request, not mutate the existing one...
pipeline_response = self._pipeline.run(
request._internal_request, **kwargs # pylint: disable=W0212
)
return corerest.HttpResponse(
request=request,
_internal_response=pipeline_response.http_response,
)
Loading