Giter VIP home page Giter VIP logo

sdk-go-1's Introduction

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/[email protected]

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.

sdk-go-1's People

Contributors

n3wscott avatar slinkydeveloper avatar alanconway avatar ian-mi avatar nachocano avatar matzew avatar markusthoemmes avatar lionelvillard avatar inigohu avatar yolocs avatar dan-j avatar kars7e avatar duglin avatar markpeek avatar antoineco avatar oleg-kulyk avatar kconwayinvision avatar agrimmer avatar matejvasek avatar pastequo avatar thinkerou avatar grayside avatar joaopgrassi avatar johejo avatar tenczar avatar odacremolbap avatar harwayne avatar grantr avatar deissnerk avatar ti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.