Skip to content

cloudevents/sdk-go

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

* http: Batch Events from HTTP Request and Response

Simple implementations of NewEventsFromHTTPRequest and
NewEventsFromHTTPResponse as a step towards providing some support for
`application/cloudevents-batch+json`.

This doesn't help with batch sending of Events, but this provides some
basics of being able to process batch requests and/or responses
from standard Go HTTP handling.

This could be improved over time (See previous work on #301) as well,
but optimised for having something that works to start, rather than
implementing big design changes.

Signed-off-by: Mark Mandel <markmandel@google.com>

* Implement NewHTTPRequestFromEvents

Now you can send batch events with your very own http.Client.

Signed-off-by: Mark Mandel <markmandel@google.com>

* Review updates

* Expanded test for NewEventsFromHTTPRequest
* Implemented NewHTTPRequestFromEvent as well

Signed-off-by: Mark Mandel <markmandel@google.com>

* Add IsHTTPBatch

IsHTTPBatch is a convenience function such that an end user could
determine if a request or response is batch or not, and process it as
such.

Signed-off-by: Mark Mandel <markmandel@google.com>

* Review updates

* Reorder in alias.go
* More coverage of TestNewEventsFromHTTPRequest
* tests for `to_events(...)`
* Better comment documentation.

Signed-off-by: Mark Mandel <markmandel@google.com>

Signed-off-by: Mark Mandel <markmandel@google.com>
885b9ca

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v2
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Go SDK for CloudEvents

go-doc Go Report Card Releases LICENSE

Official CloudEvents SDK to integrate your application with CloudEvents.

This library will help you to:

Note: Supported CloudEvents specification: 0.3, 1.0

Note: Supported go version: 1.17+

Get started

Add the module as dependency using go mod:

go get github.com/cloudevents/sdk-go/v2@v2.12.0

And import the module in your code

import cloudevents "github.com/cloudevents/sdk-go/v2"

Send your first CloudEvent

To send a CloudEvent using HTTP:

func main() {
	c, err := cloudevents.NewClientHTTP()
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}

	// Create an Event.
	event :=  cloudevents.NewEvent()
	event.SetSource("example/uri")
	event.SetType("example.type")
	event.SetData(cloudevents.ApplicationJSON, map[string]string{"hello": "world"})

	// Set a target.
	ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")

	// Send that Event.
	if result := c.Send(ctx, event); cloudevents.IsUndelivered(result) {
		log.Fatalf("failed to send, %v", result)
	}
}

Receive your first CloudEvent

To start receiving CloudEvents using HTTP:

func receive(event cloudevents.Event) {
	// do something with event.
    fmt.Printf("%s", event)
}

func main() {
	// The default client is HTTP.
	c, err := cloudevents.NewClientHTTP()
	if err != nil {
		log.Fatalf("failed to create client, %v", err)
	}
	log.Fatal(c.StartReceiver(context.Background(), receive));
}

Create a CloudEvent from an HTTP Request

func handler(w http.ResponseWriter, r *http.Request) {
	event, err := cloudevents.NewCloudEventFromHTTPRequest(r)
	if err != nil {
		log.Print("failed to parse CloudEvent from request: %v", err)
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
	}
	w.Write([]byte(*event.String()))
}

Serialize/Deserialize a CloudEvent

To marshal a CloudEvent into JSON:

event := cloudevents.NewEvent()
event.SetID("example-uuid-32943bac6fea")
event.SetSource("example/uri")
event.SetType("example.type")
event.SetData(cloudevents.ApplicationJSON, map[string]string{"hello": "world"})

bytes, err := json.Marshal(event)

To unmarshal JSON back into a CloudEvent:

event :=  cloudevents.NewEvent()

err := json.Unmarshal(bytes, &event)

Go further

Community

Each SDK may have its own unique processes, tooling and guidelines, common governance related material can be found in the CloudEvents community directory. In particular, in there you will find information concerning how SDK projects are managed, guidelines for how PR reviews and approval, and our Code of Conduct information.