goop

package module
v0.0.0-...-4e98568 Latest Latest
Warning

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

Go to latest
Published: May 10, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Report Card

GOOP - GOOgle Pub/sub

A Golang package for streamlining the usage of Google Cloud Pub/Sub.

The package that Google provide for Pub/Sub (cloud.google.com/go/pubsub) is awesome, but it can be a little verbose in places. The purpose of this package is to make common Pub/Sub tasks (creating topics, publishing and pulling messages) just a bit quicker and simpler.

Installation

Install the package as normal:

$ go get -u github.com/LUSHDigital/goop

Documentation

See GoDoc

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goop

type Goop struct {
	Context context.Context
	Project string
	Opts    []option.ClientOption
	Client  *pubsub.Client
}

Goop - Wrapper for Google Cloud Pub/Sub.

func (*Goop) CreateClient

func (g *Goop) CreateClient() error

CreateClient - Create a new Pub/Sub client.

Return:

error - An error if it occurred.
Example

ExampleGoop_CreateClient - Example usage of the CreateClient function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Create the client.
if err := g.CreateClient(); err != nil {
	log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}
Output:

func (*Goop) CreateSubscription

func (g *Goop) CreateSubscription(topic *pubsub.Topic, subName string) (*pubsub.Subscription, error)

CreateSubscription - Create a new Pub/Sub subscription if it does not already exist.

Params:

topic *pubsub.Topic - The topic to get messages from.
subName string - The name of the subscription to use.

Return:

*pubsub.Topic - Pointer to a Pub/Sub subsription.
error - An error if it occurred.
Example

ExampleGoop_CreateSubscription - Example usage of the CreateSubscription function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Create the client.
if err := g.CreateClient(); err != nil {
	log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}

// Create the topic.
topic, err := g.CreateTopic("my-awesome-topic")
if err != nil {
	log.Fatalf("\nFailed to create Pub/Sub topic. Reason - %+v\n", err)
}

// Create the subscription.
_, subErr := g.CreateSubscription(topic, "my-awesome-subscription")
if subErr != nil {
	log.Fatalf("\nFailed to create Pub/Sub subscription. Reason - %+v\n", err)
}
Output:

func (*Goop) CreateTopic

func (g *Goop) CreateTopic(topicName string) (*pubsub.Topic, error)

CreateTopic - Create a new Pub/Sub topic if it does not already exist.

Params:

topicName string - The name of the topic to create.

Return:

*pubsub.Topic - Pointer to a Pub/Sub topic.
error - An error if it occurred.
Example

ExampleGoop_CreateTopic - Example usage of the CreateTopic function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Create the client.
if err := g.CreateClient(); err != nil {
	log.Fatalf("\nFailed to create Pub/Sub client. Reason - %+v\n", err)
}

// Create the topic.
topic, err := g.CreateTopic("my-awesome-topic")
if err != nil {
	log.Fatalf("\nFailed to create Pub/Sub topic. Reason - %+v\n", err)
}

_ = topic // TODO: use the topic.
Output:

func (*Goop) Publish

func (g *Goop) Publish(topicName, msg string) error

Publish - Publish a message to a Pub/Sub topic.

Params:

topicName string - The name of the topic to publish to.
msg string - The message body to publish.

Return:

error - An error if it occurred.
Example

ExampleGoop_Publish - Example usage of the Publish function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Publish the message.
err := g.Publish("my-other-awesome-topic", "Hello world!")
if err != nil {
	log.Fatalf("\nFailed to publish Pub/Sub messages. Reason - %+v\n", err)
}
Output:

func (*Goop) PublishWithAttributes

func (g *Goop) PublishWithAttributes(topicName, msg string, attributes map[string]string) error

PublishWithAttributes - Publish a message with attributes to a Pub/Sub topic.

Params:

topicName string - The name of the topic to publish to.
msg string - The message body to publish.
attributes map[string]string - Map of attributes (key/value) to publish
along with the message body.

Return:

error - An error if it occurred.
Example

ExampleGoop_PublishWithAttributes - Example usage of the PublishWithAttributes function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Make some attributes.
messageAttributes := map[string]string{
	"answer_to_life_universe_everything": "42",
}

// Publish the message with attributes.
err := g.PublishWithAttributes("my-other-awesome-topic", "Hello world!", messageAttributes)
if err != nil {
	log.Fatalf("\nFailed to publish Pub/Sub messages. Reason - %+v\n", err)
}
Output:

func (*Goop) PullMessages

func (g *Goop) PullMessages(subName string, messageCallback func(msg *pubsub.Message, g *Goop) error) error

PullMessages - Pull messages from a Pub/Sub subscription.

Params:

subName string - The name of the subscription to use.
messageCallback func(msg *pubsub.Message, g *Goop) error - Callback
function to fire for each message pulled from the topic.

Return:

error - An error if it occurred.
Example

ExampleGoop_PullMessages - Example usage of the PullMessages function.

ctx := context.Background()

// Create Pub/Sub wrapper.
g := &Goop{
	Context: ctx,
	Project: "project-id",
}

// Callback function.
myAwesomeCallback := func(msg *pubsub.Message, pubSub *Goop) error {
	fmt.Printf("Got messages %+v", msg)

	return nil
}

// Pull the messages.
if err := g.PullMessages("my-awesome-subscription", myAwesomeCallback); err != nil {
	log.Fatalf("\nFailed to pull Pub/Sub messages. Reason - %+v\n", err)
}
Output:

Jump to

Keyboard shortcuts

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