Skip to content

Commit

Permalink
Go SDK for Azure Web PubSub Data plane (Azure#21929)
Browse files Browse the repository at this point in the history
* generated codespec for web pubsub

* Updating readme and autorest

* Adding custom client and test code

* Add GenerateClientAccessUrl

* Resolve comments

* resolve comments

* Update readme

* fix some comments, remove hub parameter when constructing the client, and add HealthAPIClient constructions

* Fix go vet error

* Fix test failure

* Fix comments

* Adding recording

* Use asset repo

* Remove skip

* Fix comment

* adding main test logic to start the test-proxy

* update CI settings

* Fix test failure

* Update link to temp URL to pass CI

* Fix doc comment

* Update CHANGELOG.md

* resolve comments

* Update sdk/messaging/azwebpubsub/ci.yml

Co-authored-by: Rick Winter <[email protected]>

* Update sdk/messaging/azwebpubsub/sample.env

Co-authored-by: Rick Winter <[email protected]>

* Resolving comments

* resolve comments

* remove healthclient

* Resolve comments

---------

Co-authored-by: MBSolomon <[email protected]>
Co-authored-by: Rick Winter <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2024
1 parent 0aa2409 commit 00f2b8b
Show file tree
Hide file tree
Showing 27 changed files with 3,096 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sdk/messaging/azwebpubsub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Release History

## 0.1.0 (2024-01-21)

### Features Added

- Initial preview for the Web PubSub Service
21 changes: 21 additions & 0 deletions sdk/messaging/azwebpubsub/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation.

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.
31 changes: 31 additions & 0 deletions sdk/messaging/azwebpubsub/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
azwebpubsub

NOTICES AND INFORMATION
Do Not Translate or Localize

This software incorporates material from third parties. Microsoft makes certain
open source code available at https://3rdpartysource.microsoft.com, or you may
send a check or money order for US $5.00, including the product name, the open
source component name, and version number, to:

Source Code Compliance Team
Microsoft Corporation
One Microsoft Way
Redmond, WA 98052
USA

Notwithstanding any other terms, you may reverse engineer this software to the
extent required to debug changes to any libraries licensed under the GNU Lesser
General Public License.

------------------------------------------------------------------------------

Azure SDK for Go uses third-party libraries or other resources that may be
distributed under licenses different than the Azure SDK for Go software.

In the event that we accidentally failed to list a required notice, please
bring it to our attention. Post an issue or email us:

@microsoft.com

The attached notices are provided for information only.
192 changes: 192 additions & 0 deletions sdk/messaging/azwebpubsub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# Azure Web PubSub service client library for Go

[Azure Web PubSub service](https://aka.ms/awps/doc) is an Azure-managed service that helps developers easily build web applications with real-time features and publish-subscribe pattern. 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.

You can use this library in your app server side to manage the WebSocket client connections, as shown in below diagram:

![overflow](https://user-images.githubusercontent.com/668244/140014067-25a00959-04dc-47e8-ac25-6957bd0a71ce.png).

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

Details about the terms used here are described in [Key concepts](#key-concepts) section.

Key links:
- [Source code][source]
- [API Reference Documentation][godoc]
- [Product documentation][product]
- [Samples][godoc_examples]

## Getting started

### Install the package

Install the Azure Web PubSub service client module for Go with `go get`:

```bash
go get github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub
```

### Prerequisites

- Go, version 1.18 or higher
- An [Azure subscription](https://azure.microsoft.com/free/)
- An existing Azure Web PubSub service instance.


### Authenticate the client

Web PubSub service clients are created using a TokenCredential from the [Azure Identity package][azure_identity_pkg], like [DefaultAzureCredential][default_azure_credential].
You can also create a client using a connection string.

#### Using a service principal

Constructing the client requires your Web PubSub's endpoint URL, which you can get from the Azure Portal (`Host name` value on overview page with `https` scheme).

```go
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub"
"log"
)

func main() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
log.Fatalf("failed to obtain a credential: %v", err)
}

client, err := azwebpubsub.NewClient("<your Web PubSub's endpoint URL>", cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
}
```

#### Using a connection string

ConnectionString can be found in the **Keys** tab from your Web PubSub resource portal.

```go
import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub"
"log"
)

func main() {
client, err := azwebpubsub.NewClientFromConnectionString("<your Web PubSub's connection string>", nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
}
```

# Key concepts

### Connection

A connection, also known as a client or a client connection, represents an individual WebSocket connection connected to the Web PubSub service. When successfully connected, a unique connection ID is assigned to this connection by the Web PubSub service.

### Hub

A hub is a logical concept for a set of client connections. Usually you use one hub for one purpose, for example, a chat hub, or a notification hub. When a client connection is created, it connects to a hub, and during its lifetime, it belongs to that hub. Different applications can share one Azure Web PubSub service by using different hub names.

### Group

A group is a subset of connections to the hub. You can add a client connection to a group, or remove the client connection from the group, anytime you want. For example, when a client joins a chat room, or when a client leaves the chat room, this chat room can be considered to be a group. 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.

### Message

When the client is connected, it can send messages to the upstream application, or receive messages from the upstream application, through the WebSocket connection.

# Examples

Examples for various scenarios can be found on [pkg.go.dev][godoc_examples] or in the example*_test.go files in our GitHub repo for [azwebpubsub][source].

# Troubleshooting

### Live Trace

Use **Live Trace** from the Web PubSub service portal to view the live traffic.

### Logging

This module uses the classification-based logging implementation in `azcore`. To enable console logging for all SDK modules, set the environment variable `AZURE_SDK_GO_LOGGING` to `all`.

Use the `azcore/log` package to control log event output or to enable logs for `azwebpubsub` only. For example:

```go
import (
"fmt"
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
)

// print log output to stdout
azlog.SetListener(func(event azlog.Event, s string) {
fmt.Printf("[%s] %s\n", event, s)
})

// pick the set of events to log
azlog.SetEvents(
azwebpubsub
)
```

## Contributing
For details on contributing to this repository, see the [contributing guide][azure_sdk_for_go_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](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

### Additional Helpful Links for Contributors
Many people all over the world have helped make this project better. You'll want to check out:

* [What are some good first issues for new contributors to the repo?](https://github.com/azure/azure-sdk-for-go/issues?q=is%3Aopen+is%3Aissue+label%3A%22up+for+grabs%22)
* [How to build and test your change][azure_sdk_for_go_contributing_developer_guide]
* [How you can make a change happen!][azure_sdk_for_go_contributing_pull_requests]
* Frequently Asked Questions (FAQ) and Conceptual Topics in the detailed [Azure SDK for Go wiki](https://github.com/azure/azure-sdk-for-go/wiki).

<!-- ### Community-->
### Reporting security issues and security bugs

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) <[email protected]>. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the [Security TechCenter](https://www.microsoft.com/msrc/faqs-report-an-issue).

### License

Azure SDK for Go is licensed under the [MIT](https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/template/aztemplate/LICENSE.txt) license.

<!-- LINKS -->
[azure_sdk_for_go_contributing]: https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md
[azure_sdk_for_go_contributing_developer_guide]: https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md#developer-guide
[azure_sdk_for_go_contributing_pull_requests]: https://github.com/Azure/azure-sdk-for-go/blob/main/CONTRIBUTING.md#pull-requests
[azure_cli]: https://docs.microsoft.com/cli/azure
[azure_pattern_circuit_breaker]: https://docs.microsoft.com/azure/architecture/patterns/circuit-breaker
[azure_pattern_retry]: https://docs.microsoft.com/azure/architecture/patterns/retry
[azure_portal]: https://portal.azure.com
[azure_sub]: https://azure.microsoft.com/free/
[cloud_shell]: https://docs.microsoft.com/azure/cloud-shell/overview
[cloud_shell_bash]: https://shell.azure.com/bash

[azure_identity_pkg]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity
[default_azure_credential]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#NewDefaultAzureCredential
[source]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk
[godoc]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk
[godoc_examples]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go#pkg-examples
[product]: https://aka.ms/awps/doc
6 changes: 6 additions & 0 deletions sdk/messaging/azwebpubsub/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/messaging/azwebpubsub",
"Tag": "go/messaging/azwebpubsub_94be93d99b"
}
75 changes: 75 additions & 0 deletions sdk/messaging/azwebpubsub/autorest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
## Go

```yaml
title: WebPubSub
description: Azure Web PubSub client
clear-output-folder: false
slice-elements-byval: true
remove-non-reference-schema: true
go: true
input-file: https://github.com/Azure/azure-rest-api-specs/blob/052a4b8d50bfd5595a8b5b506015d18f2b65998d/specification/webpubsub/data-plane/WebPubSub/stable/2023-07-01/webpubsub.json
license-header: MICROSOFT_MIT_NO_VERSION
module: github.com/Azure/azure-sdk-for-go/sdk/messaging/azwebpubsub
openapi-type: "data-plane"
output-folder: ../azwebpubsub
use: "@autorest/[email protected]"
directive:
# Remove HealthAPI
- from: swagger-document
remove-operation: 'HealthApi_GetServiceStatus'
# Rename enum WebPubSubPermission to Permission since the package name already contains WebPubSub.
- from:
- constants.go
- client.go
where: $
transform: return $.replace(/WebPubSubPermission/g, "Permission");
# Make GenerateClientToken internal.
- from: client.go
where: $
transform: return $.replace(/\bGenerateClientToken\b/g, "generateClientToken");
# Make *Exists internal until SDK supports it.
- from: client.go
where: $
transform: return $.replace(/\b(Group|Connection|User)Exists\b/g, function(match, group) { return group.toLowerCase() + "Exists";});
# Make CheckPermission internal until SDK supports it, since it leverage 404 status code
- from: client.go
where: $
transform: return $.replace(/\bCheckPermission\b/g, "checkPermission");
# Add more properties to the client
- from: client.go
where: $
transform: >-
return $.replace(
/(type Client struct[^}]+})/s,
"type Client struct {\n internal *azcore.Client\n endpoint string\n key *string\n}")
# Add comments to type Permission
- from: constants.go
where: $
transform: >-
return $.replace(
/type Permission string/s,
"// Permission contains the allowed permissions\ntype Permission string")
# Add comments to InnerError
- from: models.go
where: $
transform: >-
return $.replace(
/type InnerError struct/s,
"// InnerError - The inner error object\ntype InnerError struct")
# delete unused error models
- from: models.go
where: $
transform: return $.replace(/(?:\/\/.*\s)+type (?:ErrorDetail|InnerError).+\{(?:\s.+\s)+\}\s/g, "");
- from: models_serde.go
where: $
transform: return $.replace(/(?:\/\/.*\s)+func \(\w \*?(?:ErrorDetail|InnerError)\).*\{\s(?:.+\s)+\}\s/g, "");
# delete client name prefix from method options and response types
- from:
- client.go
- models.go
- models_serde.go
- options.go
- response_types.go
where: $
transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2");
```
10 changes: 10 additions & 0 deletions sdk/messaging/azwebpubsub/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//go:build go1.18
// +build go1.18

//go:generate autorest ./autorest.md
//go:generate goimports -w .
//go:generate gofmt -w .

package azwebpubsub
30 changes: 30 additions & 0 deletions sdk/messaging/azwebpubsub/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# NOTE: Please refer to https://aka.ms/azsdk/engsys/ci-yaml before editing this file.
trigger:
branches:
include:
- main
- feature/*
- hotfix/*
- release/*
paths:
include:
- sdk/messaging/azwebpubsub/

pr:
branches:
include:
- main
- feature/*
- hotfix/*
- release/*
paths:
include:
- sdk/messaging/azwebpubsub/


stages:
- template: /eng/pipelines/templates/jobs/archetype-sdk-client.yml
parameters:
ServiceDirectory: 'messaging/azwebpubsub'
UsePipelineProxy: false

Loading

0 comments on commit 00f2b8b

Please sign in to comment.