Giter Club home page Giter Club logo

sdk-go's Introduction

Golang CDEvents SDK

Golang SDK to emit CDEvents.

The SDK can be used to create CDEvents and send them as CloudEvents, as well as parse a received CloudEvent into a CDEvent.

Get started

Add the module as dependency using go mod:

go get github.com/cdevents/sdk-go

And import the module in your code corresponding to the desired version of the specification. For CDEvents v0.3.x, use:

import cdevents "github.com/cdevents/sdk-go/pkg/api/v03"

Create your first CDEvent

To create a CDEvent, for instance a pipelineRun queued one:

func main() {

    // Create the base event
    event, err := cdevents.NewPipelineRunQueuedEvent()
    if err != nil {
      log.Fatalf("could not create a cdevent, %v", err)
    }

    // Set the required context fields
    event.SetSubjectId("myPipelineRun1")
    event.SetSource("my/first/cdevent/program")

    // Set the required subject fields
    event.SetSubjectPipelineName("myPipeline")
    event.SetSubjectUrl("https://example.com/myPipeline")
}

Send your first CDEvent as CloudEvent

Import the modules in your code

import cdevents "github.com/cdevents/sdk-go/pkg/api"
import cloudevents "github.com/cloudevents/sdk-go/v2"

To send a CDEvent as CloudEvent:

func main() {
    // (...) set the event first
    ce, err := cdevents.AsCloudEvent(event)

    // Set send options
    ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
    ctx = cloudevents.WithEncodingBinary(ctx)

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

    // Send the CloudEvent
    // c is a CloudEvent client
    if result := c.Send(ctx, *ce); cloudevents.IsUndelivered(result) {
        log.Fatalf("failed to send, %v", result)
    }
}

See the CloudEvents docs as well.

Contributing

If you would like to contribute, see our development guide.

References

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.