aznamespaces

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 16 Imported by: 1

README

Azure Event Grid Namespaces Client Module for Go

Azure Event Grid is a highly scalable, fully managed Pub Sub message distribution service that offers flexible message consumption patterns. For more information about Event Grid see: link.

This client module allows you to publish events and receive events using the Pull delivery API.

NOTE: This client does not work with Event Grid Basic. Use the azeventgrid.Client in the azeventgrid package instead.

Key links:

Getting started

Install the package

Install the Azure Event Grid Namespaces client module for Go with go get:

go get github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces
Prerequisites
Authenticate the client

Event Grid Namespaces clients (SenderClient and ReceiverClient) authenticate using a TokenCredential, provided by the azidentity module, or using a shared key credential.

Using a TokenCredential:

Using a shared key:

Key concepts

An Event Grid namespace is a container for multiple types of resources, including namespace topics:

Namespaces also offer access using MQTT, although that is not covered in this package.

Examples

Examples for various scenarios can be found on pkg.go.dev or in the example*_test.go files in our GitHub repo for aznamespaces.

Troubleshooting

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.

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)
})

Next steps

More sample code should go here, along with links out to the appropriate example tests.

Contributing

For details on contributing to this repository, see the contributing guide.

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. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Many people all over the world have helped make this project better. You'll want to check out:

Reporting security issues and security bugs

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. 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.

License

Azure SDK for Go is licensed under the MIT license.

Documentation

Overview

Example (PublishAndReceiveCloudEvents)
package main

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	topicName := os.Getenv("EVENTGRID_TOPIC")
	subscriptionName := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || topicName == "" || subscriptionName == "" {
		return
	}

	tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		// TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	sender, err := aznamespaces.NewSenderClient(endpoint, topicName, tokenCredential, nil)

	if err != nil {
		panic(err)
	}

	receiver, err := aznamespaces.NewReceiverClient(endpoint, topicName, subscriptionName, tokenCredential, nil)

	if err != nil {
		panic(err)
	}

	//
	// Publish an event with a string payload
	//
	fmt.Fprintf(os.Stderr, "Published event with a string payload 'hello world'\n")
	eventWithString, err := sendAndReceiveEvent(sender, receiver, "application/json", "hello world")

	if err != nil {
		panic(err)
	}

	fmt.Fprintf(os.Stderr, "Received an event with a string payload\n")
	fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithString.Event.ID)

	var str *string

	if err := json.Unmarshal(eventWithString.Event.Data.([]byte), &str); err != nil {
		panic(err)
	}

	fmt.Fprintf(os.Stderr, "  Body: %s\n", *str) // prints 'Body: hello world'
	fmt.Fprintf(os.Stderr, "  Delivery count: %d\n", eventWithString.BrokerProperties.DeliveryCount)

	//
	// Publish an event with a []byte payload
	//
	eventWithBytes, err := sendAndReceiveEvent(sender, receiver, "application/octet-stream", []byte{0, 1, 2})

	if err != nil {
		panic(err)
	}

	fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithBytes.Event.ID)
	fmt.Fprintf(os.Stderr, "  Body: %#v\n", eventWithBytes.Event.Data.([]byte)) // prints 'Body: []byte{0x0, 0x1, 0x2}'
	fmt.Fprintf(os.Stderr, "  Delivery count: %d\n", eventWithBytes.BrokerProperties.DeliveryCount)

	//
	// Publish an event with a struct as the payload
	//
	type SampleData struct {
		Name string `json:"name"`
	}

	eventWithStruct, err := sendAndReceiveEvent(sender, receiver, "application/json", SampleData{Name: "hello"})

	if err != nil {
		panic(err)
	}

	var sampleData *SampleData
	if err := json.Unmarshal(eventWithStruct.Event.Data.([]byte), &sampleData); err != nil {
		panic(err)
	}

	fmt.Fprintf(os.Stderr, "ID: %s\n", eventWithStruct.Event.ID)
	fmt.Fprintf(os.Stderr, "  Body: %#v\n", sampleData) // prints 'Body: &azeventgrid_test.SampleData{Name:"hello"}'
	fmt.Fprintf(os.Stderr, "  Delivery count: %d\n", eventWithStruct.BrokerProperties.DeliveryCount)

	// Disabled: WARNING: Forcing use of SASKey until https://github.com/Azure/azure-sdk-for-go/issues/22961 is resolved
	// TempDisabledOutput:
}

func sendAndReceiveEvent(sender *aznamespaces.SenderClient, receiver *aznamespaces.ReceiverClient, dataContentType string, payload any) (aznamespaces.ReceiveDetails, error) {
	event, err := messaging.NewCloudEvent("source", "eventType", payload, &messaging.CloudEventOptions{
		DataContentType: &dataContentType,
	})

	if err != nil {
		return aznamespaces.ReceiveDetails{}, err
	}

	eventsToSend := []*messaging.CloudEvent{
		&event,
	}

	// NOTE: we're sending a single event as an example. For better efficiency it's best if you send
	// multiple events at a time.
	_, err = sender.SendEvents(context.TODO(), eventsToSend, nil)

	if err != nil {
		return aznamespaces.ReceiveDetails{}, err
	}

	events, err := receiver.ReceiveEvents(context.TODO(), &aznamespaces.ReceiveEventsOptions{
		MaxEvents: to.Ptr(int32(1)),

		// Wait for 60 seconds for events.
		MaxWaitTime: to.Ptr[int32](60),
	})

	if err != nil {
		return aznamespaces.ReceiveDetails{}, err
	}

	if len(events.Details) == 0 {
		return aznamespaces.ReceiveDetails{}, errors.New("no events received")
	}

	// We can (optionally) renew the lock (multiple times) if we want to continue to
	// extend the lock time on the event.
	_, err = receiver.RenewEventLocks(context.TODO(), []string{
		*events.Details[0].BrokerProperties.LockToken,
	}, nil)

	if err != nil {
		return aznamespaces.ReceiveDetails{}, err
	}

	// This acknowledges the event and causes it to be deleted from the subscription.
	// Other options are:
	// - client.ReleaseCloudEvents, which invalidates our event lock and allows another subscriber to receive the event.
	// - client.RejectCloudEvents, which rejects the event.
	//     If dead-lettering is configured, the event will be moved into the dead letter queue.
	//     Otherwise the event is deleted.
	ackResp, err := receiver.AcknowledgeEvents(context.TODO(), []string{
		*events.Details[0].BrokerProperties.LockToken,
	}, nil)

	if err != nil {
		return aznamespaces.ReceiveDetails{}, err
	}

	if len(ackResp.FailedLockTokens) > 0 {
		// some events failed when we tried to acknowledge them.
		for _, failed := range ackResp.FailedLockTokens {
			fmt.Printf("Failed to acknowledge event with lock token %s: %s\n", *failed.LockToken, failed.Error)
		}

		return aznamespaces.ReceiveDetails{}, errors.New("failed to acknowledge event")
	}

	return events.Details[0], nil
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcknowledgeEventsOptions added in v1.0.0

type AcknowledgeEventsOptions struct {
}

AcknowledgeEventsOptions contains the optional parameters for the ReceiverClient.AcknowledgeEvents method.

type AcknowledgeEventsResponse added in v1.0.0

type AcknowledgeEventsResponse struct {
	// The result of the Acknowledge operation.
	AcknowledgeEventsResult
}

AcknowledgeEventsResponse contains the response from method ReceiverClient.AcknowledgeEvents.

type AcknowledgeEventsResult added in v1.0.0

type AcknowledgeEventsResult struct {
	// REQUIRED; Array of FailedLockToken for failed cloud events. Each FailedLockToken includes the lock token along with the
	// related error information (namely, the error code and description).
	FailedLockTokens []FailedLockToken

	// REQUIRED; Array of lock tokens for the successfully acknowledged cloud events.
	SucceededLockTokens []string
}

AcknowledgeEventsResult - The result of the Acknowledge operation.

func (AcknowledgeEventsResult) MarshalJSON added in v1.0.0

func (a AcknowledgeEventsResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type AcknowledgeResult.

func (*AcknowledgeEventsResult) UnmarshalJSON added in v1.0.0

func (a *AcknowledgeEventsResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type AcknowledgeResult.

type BrokerProperties

type BrokerProperties struct {
	// REQUIRED; The attempt count for delivering the event.
	DeliveryCount *int32

	// REQUIRED; The token of the lock on the event.
	LockToken *string
}

BrokerProperties - Properties of the Event Broker operation.

func (BrokerProperties) MarshalJSON

func (b BrokerProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type BrokerProperties.

func (*BrokerProperties) UnmarshalJSON

func (b *BrokerProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type BrokerProperties.

type Error

type Error struct {
	// REQUIRED; One of a server-defined set of error codes.
	Code *string

	// REQUIRED; A human-readable representation of the error.
	Message *string
}

Error - The error object.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface for type Error. Note that the message contents are not contractual and can change over time.

func (Error) MarshalJSON

func (e Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Error.

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Error.

type FailedLockToken

type FailedLockToken struct {
	// REQUIRED; Error information of the failed operation result for the lock token in the request.
	Error *Error

	// REQUIRED; The lock token of an entry in the request.
	LockToken *string
}

FailedLockToken - Failed LockToken information.

func (FailedLockToken) MarshalJSON

func (f FailedLockToken) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type FailedLockToken.

func (*FailedLockToken) UnmarshalJSON

func (f *FailedLockToken) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type FailedLockToken.

type ReceiveDetails

type ReceiveDetails struct {
	// REQUIRED; The Event Broker details.
	BrokerProperties *BrokerProperties

	// REQUIRED; Cloud Event details.
	Event messaging.CloudEvent
}

ReceiveDetails - Receive operation details per Cloud Event.

func (ReceiveDetails) MarshalJSON

func (r ReceiveDetails) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ReceiveDetails.

func (*ReceiveDetails) UnmarshalJSON

func (r *ReceiveDetails) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ReceiveDetails.

type ReceiveEventsOptions added in v1.0.0

type ReceiveEventsOptions struct {
	// Max Events count to be received. Minimum value is 1, while maximum value is 100 events. If not specified, the default value
	// is 1.
	MaxEvents *int32

	// Max wait time value for receive operation in Seconds. It is the time in seconds that the server approximately waits for
	// the availability of an event and responds to the request. If an event is available, the broker responds immediately to
	// the client. Minimum value is 10 seconds, while maximum value is 120 seconds. If not specified, the default value is 60
	// seconds.
	MaxWaitTime *int32
}

ReceiveEventsOptions contains the optional parameters for the ReceiverClient.ReceiveEvents method.

type ReceiveEventsResponse added in v1.0.0

type ReceiveEventsResponse struct {
	// Details of the Receive operation response.
	ReceiveEventsResult
}

ReceiveEventsResponse contains the response from method ReceiverClient.ReceiveEvents.

type ReceiveEventsResult added in v1.0.0

type ReceiveEventsResult struct {
	// REQUIRED; Array of receive responses, one per cloud event.
	Details []ReceiveDetails
}

ReceiveEventsResult - Details of the Receive operation response.

func (ReceiveEventsResult) MarshalJSON added in v1.0.0

func (r ReceiveEventsResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ReceiveResult.

func (*ReceiveEventsResult) UnmarshalJSON added in v1.0.0

func (r *ReceiveEventsResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ReceiveResult.

type ReceiverClient added in v1.0.0

type ReceiverClient struct {
	// contains filtered or unexported fields
}

ReceiverClient contains the methods for the Microsoft.EventGrid namespace. Don't use this type directly, use a constructor function instead.

func NewReceiverClient added in v1.0.0

func NewReceiverClient(endpoint string, topic string, subscription string, cred azcore.TokenCredential, options *ReceiverClientOptions) (*ReceiverClient, error)

NewReceiverClient creates a ReceiverClient which uses an azcore.TokenCredential for authentication.

  • topicName - Topic Name.
  • subscriptionName - Event Subscription Name.
Example
package main

import (
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	topic := os.Getenv("EVENTGRID_TOPIC")
	subscription := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || topic == "" || subscription == "" {
		return
	}

	tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		// TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	client, err := aznamespaces.NewReceiverClient(endpoint, topic, subscription, tokenCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client // ignore

}
Output:

func NewReceiverClientWithSharedKeyCredential added in v1.0.0

func NewReceiverClientWithSharedKeyCredential(endpoint string, topic string, subscription string, keyCred *azcore.KeyCredential, options *ReceiverClientOptions) (*ReceiverClient, error)

NewReceiverClientWithSharedKeyCredential creates a ReceiverClient using a shared key.

  • topicName - Topic Name.
  • subscriptionName - Event Subscription Name.
Example
package main

import (
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	sharedKey := os.Getenv("EVENTGRID_KEY")
	topic := os.Getenv("EVENTGRID_TOPIC")
	subscription := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || sharedKey == "" || topic == "" || subscription == "" {
		return
	}

	client, err := aznamespaces.NewReceiverClientWithSharedKeyCredential(endpoint, topic, subscription, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client // ignore

}
Output:

func (*ReceiverClient) AcknowledgeEvents added in v1.0.0

func (client *ReceiverClient) AcknowledgeEvents(ctx context.Context, lockTokens []string, options *AcknowledgeEventsOptions) (AcknowledgeEventsResponse, error)

AcknowledgeEvents acknowledges a batch of Cloud Events. The server responds with an HTTP 200 status code if the request is successfully accepted. The response body will include the set of successfully acknowledged lockTokens, along with other failed lockTokens with their corresponding error information. Successfully acknowledged events will no longer be available to any consumer. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • lockTokens - slice of lock tokens.
  • options - AcknowledgeEventsOptions contains the optional parameters for the ReceiverClient.AcknowledgeEvents method.

func (*ReceiverClient) ReceiveEvents added in v1.0.0

func (client *ReceiverClient) ReceiveEvents(ctx context.Context, options *ReceiveEventsOptions) (ReceiveEventsResponse, error)

ReceiveEvents receives a batch of Cloud Events from a subscription. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • options - ReceiveEventsOptions contains the optional parameters for the ReceiverClient.ReceiveEvents method.
Example
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	sender, receiver := newEventGridClients()

	if sender == nil || receiver == nil {
		return
	}

	resp, err := receiver.ReceiveEvents(context.TODO(), &aznamespaces.ReceiveEventsOptions{
		MaxEvents:   to.Ptr[int32](1),
		MaxWaitTime: to.Ptr[int32](10), // in seconds
	})

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	for _, rd := range resp.Details {
		lockToken := rd.BrokerProperties.LockToken

		// NOTE: See the documentation for CloudEvent.Data on how your data
		// is deserialized.
		data := rd.Event.Data

		fmt.Fprintf(os.Stderr, "Event ID:%s, data: %#v, lockToken: %s\n", rd.Event.ID, data, *lockToken)

		// This will complete the message, deleting it from the subscription.
		resp, err := receiver.AcknowledgeEvents(context.TODO(), []string{*lockToken}, nil)

		if err != nil {
			//  TODO: Update the following line with your application specific error handling logic
			log.Fatalf("ERROR: %s", err)
		}

		if len(resp.FailedLockTokens) > 0 {
			log.Fatalf("ERROR: %d events were not acknowledged", len(resp.FailedLockTokens))
		}
	}

}

func newEventGridClients() (*aznamespaces.SenderClient, *aznamespaces.ReceiverClient) {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	sharedKey := os.Getenv("EVENTGRID_KEY")
	topic := os.Getenv("EVENTGRID_TOPIC")
	subscription := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || sharedKey == "" || topic == "" || subscription == "" {
		return nil, nil
	}

	sender, err := aznamespaces.NewSenderClientWithSharedKeyCredential(endpoint, topic, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	receiver, err := aznamespaces.NewReceiverClientWithSharedKeyCredential(endpoint, topic, subscription, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	return sender, receiver
}
Output:

func (*ReceiverClient) RejectEvents added in v1.0.0

func (client *ReceiverClient) RejectEvents(ctx context.Context, lockTokens []string, options *RejectEventsOptions) (RejectEventsResponse, error)

RejectEvents rejects a batch of Cloud Events. The server responds with an HTTP 200 status code if the request is successfully accepted. The response body will include the set of successfully rejected lockTokens, along with other failed lockTokens with their corresponding error information. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • lockTokens - slice of lock tokens.
  • options - RejectEventsOptions contains the optional parameters for the ReceiverClient.RejectEvents method.

func (*ReceiverClient) ReleaseEvents added in v1.0.0

func (client *ReceiverClient) ReleaseEvents(ctx context.Context, lockTokens []string, options *ReleaseEventsOptions) (ReleaseEventsResponse, error)

ReleaseEvents releases a batch of Cloud Events. The server responds with an HTTP 200 status code if the request is successfully accepted. The response body will include the set of successfully released lockTokens, along with other failed lockTokens with their corresponding error information. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • lockTokens - slice of lock tokens.
  • options - ReleaseEventsOptions contains the optional parameters for the ReceiverClient.ReleaseEvents method.

func (*ReceiverClient) RenewEventLocks added in v1.0.0

func (client *ReceiverClient) RenewEventLocks(ctx context.Context, lockTokens []string, options *RenewEventLocksOptions) (RenewEventLocksResponse, error)

RenewEventLocks renews locks for batch of Cloud Events. The server responds with an HTTP 200 status code if the request is successfully accepted. The response body will include the set of successfully renewed lockTokens, along with other failed lockTokens with their corresponding error information. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • lockTokens - slice of lock tokens.
  • options - RenewLocksOptions contains the optional parameters for the ReceiverClient.RenewLocks method.

type ReceiverClientOptions added in v1.0.0

type ReceiverClientOptions struct {
	azcore.ClientOptions
}

ReceiverClientOptions contains the optional parameters when creating a ReceiverClient.

type RejectEventsOptions added in v1.0.0

type RejectEventsOptions struct {
}

RejectEventsOptions contains the optional parameters for the ReceiverClient.RejectEvents method.

type RejectEventsResponse added in v1.0.0

type RejectEventsResponse struct {
	// The result of the Reject operation.
	RejectEventsResult
}

RejectEventsResponse contains the response from method ReceiverClient.RejectEvents.

type RejectEventsResult added in v1.0.0

type RejectEventsResult struct {
	// REQUIRED; Array of FailedLockToken for failed cloud events. Each FailedLockToken includes the lock token along with the
	// related error information (namely, the error code and description).
	FailedLockTokens []FailedLockToken

	// REQUIRED; Array of lock tokens for the successfully rejected cloud events.
	SucceededLockTokens []string
}

RejectEventsResult - The result of the Reject operation.

func (RejectEventsResult) MarshalJSON added in v1.0.0

func (r RejectEventsResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type RejectResult.

func (*RejectEventsResult) UnmarshalJSON added in v1.0.0

func (r *RejectEventsResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type RejectResult.

type ReleaseDelay

type ReleaseDelay string

ReleaseDelay - Supported delays for release operation.

const (
	// ReleaseDelayNoDelay - Release the event after 0 seconds.
	ReleaseDelayNoDelay ReleaseDelay = "0"
	// ReleaseDelayOneHour - Release the event after 3600 seconds.
	ReleaseDelayOneHour ReleaseDelay = "3600"
	// ReleaseDelayOneMinute - Release the event after 60 seconds.
	ReleaseDelayOneMinute ReleaseDelay = "60"
	// ReleaseDelayTenMinutes - Release the event after 600 seconds.
	ReleaseDelayTenMinutes ReleaseDelay = "600"
	// ReleaseDelayTenSeconds - Release the event after 10 seconds.
	ReleaseDelayTenSeconds ReleaseDelay = "10"
)

func PossibleReleaseDelayValues

func PossibleReleaseDelayValues() []ReleaseDelay

PossibleReleaseDelayValues returns the possible values for the ReleaseDelay const type.

type ReleaseEventsOptions added in v1.0.0

type ReleaseEventsOptions struct {
	// Release cloud events with the specified delay in seconds.
	ReleaseDelayInSeconds *ReleaseDelay
}

ReleaseEventsOptions contains the optional parameters for the ReceiverClient.ReleaseEvents method.

type ReleaseEventsResponse added in v1.0.0

type ReleaseEventsResponse struct {
	// The result of the Release operation.
	ReleaseEventsResult
}

ReleaseEventsResponse contains the response from method ReceiverClient.ReleaseEvents.

type ReleaseEventsResult added in v1.0.0

type ReleaseEventsResult struct {
	// REQUIRED; Array of FailedLockToken for failed cloud events. Each FailedLockToken includes the lock token along with the
	// related error information (namely, the error code and description).
	FailedLockTokens []FailedLockToken

	// REQUIRED; Array of lock tokens for the successfully released cloud events.
	SucceededLockTokens []string
}

ReleaseEventsResult - The result of the Release operation.

func (ReleaseEventsResult) MarshalJSON added in v1.0.0

func (r ReleaseEventsResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ReleaseResult.

func (*ReleaseEventsResult) UnmarshalJSON added in v1.0.0

func (r *ReleaseEventsResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ReleaseResult.

type RenewEventLocksOptions added in v1.0.0

type RenewEventLocksOptions struct {
}

RenewEventLocksOptions contains the optional parameters for the ReceiverClient.RenewEventLocks method.

type RenewEventLocksResponse added in v1.0.0

type RenewEventLocksResponse struct {
	// The result of the RenewLock operation.
	RenewEventLocksResult
}

RenewEventLocksResponse contains the response from method ReceiverClient.RenewEventLocks.

type RenewEventLocksResult added in v1.0.0

type RenewEventLocksResult struct {
	// REQUIRED; Array of FailedLockToken for failed cloud events. Each FailedLockToken includes the lock token along with the
	// related error information (namely, the error code and description).
	FailedLockTokens []FailedLockToken

	// REQUIRED; Array of lock tokens for the successfully renewed locks.
	SucceededLockTokens []string
}

RenewEventLocksResult - The result of the RenewLock operation.

func (RenewEventLocksResult) MarshalJSON added in v1.0.0

func (r RenewEventLocksResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type RenewLocksResult.

func (*RenewEventLocksResult) UnmarshalJSON added in v1.0.0

func (r *RenewEventLocksResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type RenewLocksResult.

type SendEventOptions added in v1.0.0

type SendEventOptions struct {
}

SendEventOptions contains the optional parameters for the SenderClient.SendEvent method.

type SendEventResponse added in v1.0.0

type SendEventResponse struct {
}

SendEventResponse contains the response from method SenderClient.SendEvent.

type SendEventsOptions added in v1.0.0

type SendEventsOptions struct {
}

SendEventsOptions contains the optional parameters for the SenderClient.SendEvents method.

type SendEventsResponse added in v1.0.0

type SendEventsResponse struct {
}

SendEventsResponse contains the response from method SenderClient.SendEvents.

type SenderClient added in v1.0.0

type SenderClient struct {
	// contains filtered or unexported fields
}

SenderClient contains the methods for the Microsoft.EventGrid namespace. Don't use this type directly, use a constructor function instead.

func NewSenderClient added in v1.0.0

func NewSenderClient(endpoint string, topic string, cred azcore.TokenCredential, options *SenderClientOptions) (*SenderClient, error)

NewSenderClient creates a SenderClient which uses an azcore.TokenCredential for authentication.

Example
package main

import (
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	topic := os.Getenv("EVENTGRID_TOPIC")

	if endpoint == "" || topic == "" {
		return
	}

	tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		// TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	client, err := aznamespaces.NewSenderClient(endpoint, topic, tokenCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client // ignore

}
Output:

func NewSenderClientWithSharedKeyCredential added in v1.0.0

func NewSenderClientWithSharedKeyCredential(endpoint string, topic string, keyCred *azcore.KeyCredential, options *SenderClientOptions) (*SenderClient, error)

NewSenderClientWithSharedKeyCredential creates a SenderClient using a shared key.

Example
package main

import (
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	sharedKey := os.Getenv("EVENTGRID_KEY")
	topic := os.Getenv("EVENTGRID_TOPIC")

	if endpoint == "" || sharedKey == "" || topic == "" {
		return
	}

	client, err := aznamespaces.NewSenderClientWithSharedKeyCredential(endpoint, topic, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client // ignore

}
Output:

func (*SenderClient) SendEvent added in v1.0.0

func (client *SenderClient) SendEvent(ctx context.Context, event *messaging.CloudEvent, options *SendEventOptions) (SendEventResponse, error)

SendEvent publishes a single Cloud Event to a namespace topic. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • event - Cloud Event to publish.
  • options - SendOptions contains the optional parameters for the SenderClient.SendEvent method.
Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	sender, receiver := newEventGridClients()

	if sender == nil || receiver == nil {
		return
	}

	// CloudEvent is in github.com/Azure/azure-sdk-for-go/azcore/messaging and can be
	// used to transport

	// you can send a variety of different payloads, all of which can be encoded by messaging.CloudEvent
	payload := []byte{1, 2, 3}
	eventToSend, err := messaging.NewCloudEvent("source", "eventType", payload, &messaging.CloudEventOptions{
		DataContentType: to.Ptr("application/octet-stream"),
	})

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_, err = sender.SendEvent(context.TODO(), &eventToSend, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

}

func newEventGridClients() (*aznamespaces.SenderClient, *aznamespaces.ReceiverClient) {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	sharedKey := os.Getenv("EVENTGRID_KEY")
	topic := os.Getenv("EVENTGRID_TOPIC")
	subscription := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || sharedKey == "" || topic == "" || subscription == "" {
		return nil, nil
	}

	sender, err := aznamespaces.NewSenderClientWithSharedKeyCredential(endpoint, topic, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	receiver, err := aznamespaces.NewReceiverClientWithSharedKeyCredential(endpoint, topic, subscription, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	return sender, receiver
}
Output:

func (*SenderClient) SendEvents added in v1.0.0

func (client *SenderClient) SendEvents(ctx context.Context, events []*messaging.CloudEvent, options *SendEventsOptions) (SendEventsResponse, error)

SendEvents publishes a batch of Cloud Events to a namespace topic. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2024-06-01

  • events - slice of Cloud Events to publish.
  • options - SendEventsOptions contains the optional parameters for the SenderClient.SendEvents method.
Example
package main

import (
	"context"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/messaging"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/messaging/eventgrid/aznamespaces"
)

func main() {
	sender, receiver := newEventGridClients()

	if sender == nil || receiver == nil {
		return
	}

	// CloudEvent is in github.com/Azure/azure-sdk-for-go/azcore/messaging and can be
	// used to transport

	// you can send a variety of different payloads, all of which can be encoded by messaging.CloudEvent
	var payloads = []any{
		[]byte{1, 2, 3},
		"hello world",
		struct{ Value string }{Value: "hello world"},
	}

	var eventsToSend []*messaging.CloudEvent

	for _, payload := range payloads {
		event, err := messaging.NewCloudEvent("source", "eventType", payload, &messaging.CloudEventOptions{
			DataContentType: to.Ptr("application/octet-stream"),
		})

		if err != nil {
			//  TODO: Update the following line with your application specific error handling logic
			log.Fatalf("ERROR: %s", err)
		}

		eventsToSend = append(eventsToSend, &event)
	}

	_, err := sender.SendEvents(context.TODO(), eventsToSend, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

}

func newEventGridClients() (*aznamespaces.SenderClient, *aznamespaces.ReceiverClient) {
	endpoint := os.Getenv("EVENTGRID_ENDPOINT")
	sharedKey := os.Getenv("EVENTGRID_KEY")
	topic := os.Getenv("EVENTGRID_TOPIC")
	subscription := os.Getenv("EVENTGRID_SUBSCRIPTION")

	if endpoint == "" || sharedKey == "" || topic == "" || subscription == "" {
		return nil, nil
	}

	sender, err := aznamespaces.NewSenderClientWithSharedKeyCredential(endpoint, topic, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	receiver, err := aznamespaces.NewReceiverClientWithSharedKeyCredential(endpoint, topic, subscription, azcore.NewKeyCredential(sharedKey), nil)

	if err != nil {

		log.Fatalf("ERROR: %s", err)
	}

	return sender, receiver
}
Output:

type SenderClientOptions added in v1.0.0

type SenderClientOptions struct {
	azcore.ClientOptions
}

SenderClientOptions contains the optional parameters when creating a SenderClient.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL