Skip to content

Commit

Permalink
add publishes custom event
Browse files Browse the repository at this point in the history
  • Loading branch information
cinience committed Feb 25, 2021
1 parent e9a069a commit ffd53a4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type Client interface {
// InvokeMethodWithCustomContent invokes app with custom content (struct + content type).
InvokeMethodWithCustomContent(ctx context.Context, appID, methodName, verb string, contentType string, content interface{}) (out []byte, err error)

// PublishEvent publishes custom event.
Publish(ctx context.Context, in *PublishEventRequest) error

// PublishEvent publishes data onto topic in specific pubsub component.
PublishEvent(ctx context.Context, pubsubName, topicName string, data []byte) error

Expand Down
42 changes: 41 additions & 1 deletion client/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,51 @@ package client
import (
"context"
"encoding/json"

pb "github.com/dapr/go-sdk/dapr/proto/runtime/v1"
"github.com/pkg/errors"
)

type PublishEventRequest struct {
// The name of the pubsub component
PubsubName string
// The pubsub topic
Topic string
// The data which will be published to topic.
Data []byte
// The content type for the data (optional).
DataContentType string
// The metadata passing to pub components
//
// metadata property:
// - key : the key of the message.
Metadata map[string]string
}

// Publish publishes custom event.
func (c *GRPCClient) Publish(ctx context.Context, in *PublishEventRequest) error {
if in.PubsubName == "" {
return errors.New("PublishEventRequest.PubsubName required")
}
if in.Topic == "" {
return errors.New("PublishEventRequest.Topic required")
}

envelop := &pb.PublishEventRequest{
PubsubName: in.PubsubName,
Topic: in.Topic,
Data: in.Data,
DataContentType: in.DataContentType,
Metadata: in.Metadata,
}

_, err := c.protoClient.PublishEvent(c.withAuthToken(ctx), envelop)
if err != nil {
return errors.Wrapf(err, "error publishing event unto %s topic", in.Topic)
}

return nil
}

// PublishEvent publishes data onto specific pubsub topic.
func (c *GRPCClient) PublishEvent(ctx context.Context, pubsubName, topicName string, data []byte) error {
if pubsubName == "" {
Expand Down

0 comments on commit ffd53a4

Please sign in to comment.