package
99.0.0+incompatible
Repository: https://github.com/poy/knative-pkg.git
Documentation: pkg.go.dev

# README

Knative CloudEvents SDK

This library produces CloudEvents in version 0.1 compatible form. To learn more about CloudEvents, see the Specification.

There are two roles the SDK fulfills: the producer and the consumer. The producer creates a cloud event in either Binary or Structured request format. The producer assembles and sends the event through an HTTP endpoint. The consumer will inspect the incoming HTTP request and select the correct decode format.

This SDK should be wire-compatible with any other producer or consumer of the supported versions of CloudEvents.

Getting Started

CloudEvents acts as the envelope in which to send a custom object. Define a CloudEvent type for the events you will be producing.

Example CloudEvent Type: dev.knative.cloudevent.example

Select a source to identify the originator of this CloudEvent. It should be a valid URI which represents the subject which created the CloudEvent (cloud bucket, git repo, etc).

Example CloudEvent Source: https://github.com/knative/pkg#cloudevents-example

And finally, create a struct that will be the data inside the CloudEvent, example:


type Example struct {
    Sequence int    `json:"id"`
    Message  string `json:"message"`
}

Producer

The producer creates a new cloudevent.Client, and then sends 10 Example events to "http://localhost:8080".


package main

import (
    "github.com/knative/pkg/cloudevents"
    "log"
)

type Example struct {
    Sequence int    `json:"id"`
    Message  string `json:"message"`
}

func main() {
    c := cloudevents.NewClient(
        "http://localhost:8080",
        cloudevents.Builder{
            Source:    "https://github.com/knative/pkg#cloudevents-example",
            EventType: "dev.knative.cloudevent.example",
        },
    )
    for i := 0; i < 10; i++ {
        data := Example{
            Message:  "hello, world!",
            Sequence: i,
        }
        if err := c.Send(data); err != nil {
            log.Printf("error sending: %v", err)
        }
    }
}

Consumer

The consumer will listen for a post and then inspect the headers to understand how to decode the request.


package main

import (
    "context"
    "log"
    "net/http"
    "time"

    "github.com/knative/pkg/cloudevents"
)

type Example struct {
    Sequence int    `json:"id"`
    Message  string `json:"message"`
}

func handler(ctx context.Context, data *Example) {
    metadata := cloudevents.FromContext(ctx)
    log.Printf("[%s] %s %s: %d, %q", metadata.EventTime.Format(time.RFC3339), metadata.ContentType, metadata.Source, data.Sequence, data.Message)
}

func main() {
    log.Print("listening on port 8080")
    log.Fatal(http.ListenAndServe(":8080", cloudevents.Handler(handler)))
}

Request Formats

CloudEvents Version 0.1

Binary

This is default, but to leverage binary request format:


    c := cloudevents.NewClient(
        "http://localhost:8080",
        cloudevents.Builder{
            Source:    "https://github.com/knative/pkg#cloudevents-example",
            EventType: "dev.knative.cloudevent.example",
            Encoding: cloudevents.BinaryV01,
        },
    )

Structured

To leverage structured request format:


    c := cloudevents.NewClient(
        "http://localhost:8080",
        cloudevents.Builder{
            Source:    "https://github.com/knative/pkg#cloudevents-example",
            EventType: "dev.knative.cloudevent.example",
            Encoding: cloudevents.StructuredV01,
        },
    )

# Functions

FromContext loads an V01EventContext from a normal context.Context.
FromRequest parses a CloudEvent from any known encoding.
Handler creates an EventHandler that implements http.Handler If the fn parameter is not a valid type, will produce an http.Handler that also conforms to error and will respond to all HTTP requests with that error.
NewClient returns a CloudEvent Client used to send CloudEvents.
NewMux creates a new Mux.
NewRequest craetes an HTTP request for Structured content encoding.

# Constants

Binary implements Binary encoding/decoding.
Binary v0.1.
CloudEventsVersion is a legacy alias of V01CloudEventsVersion, for compatibility.
ContentTypeBinaryJSON is the content-type for "Binary" encoding where the event context is in HTTP headers and the body is a JSON event data.
ContentTypeStructuredJSON is the content-type for "Structured" encoding where an event envelope is written in JSON and the body is arbitrary data which might be an alternate encoding.
HeaderCloudEventsVersion is the header for the version of Cloud Events used.
HeaderContentType is the standard HTTP header "Content-Type".
HeaderEventID is the header for the unique ID of this event.
HeaderEventTime is the OPTIONAL header for the time at which an event occurred.
HeaderEventType is the header for type of event represented.
HeaderEventTypeVersion is the OPTIONAL header for the version of the scheme for the event type.
HeaderExtensionsPrefix is the OPTIONAL header prefix for CloudEvents extensions.
HeaderSchemaURL is the OPTIONAL header for the schema of the event data.
HeaderSource is the header for the source which emitted this event.
Structured implements the JSON structured encoding/decoding.
Structured v0.1.
V01CloudEventsVersion is the version of the CloudEvents spec targeted by this library.
V02CloudEventsVersion is the version of the CloudEvents spec targeted by this library.

# Structs

Builder holds settings that do not change over CloudEvents.
Client wraps Builder, and is intended to be configured for a single event type and target.
V01EventContext holds standard metadata about an event.
V02EventContext represents the non-data attributes of a CloudEvents v0.2 event.

# Interfaces

BinaryLoader implements an interface for translating a binary encoding HTTP request or response to a an EventContext (possibly one of several versions).
BinarySender implements an interface for sending an EventContext as (possibly one of several versions) as a binary encoding HTTP request.
ContextTranslator provides a set of translation methods between the different versions of the CloudEvents spec, which allows programs to interoperate with different versions of the CloudEvents spec by converting EventContexts to their preferred version.
ContextType is a unified interface for both sending and loading the CloudEvent data across versions.
HTTPMarshaller implements a scheme for decoding CloudEvents over HTTP.
LoadContext provides an interface for extracting information from an EventContext (the set of non-data event attributes of a CloudEvent).
SendContext provides an interface for extracting information from an EventContext (the set of non-data event attributes of a CloudEvent).
StructuredLoader implements an interface for translating a structured encoding HTTP request or response to a an EventContext (possibly one of several versions).
StructuredSender implements an interface for translating an EventContext (possibly one of severals versions) to a structured encoding HTTP request.

# Type aliases

CloudEventEncoding is used to tell the builder which encoding to select.
EventContext is a legacy un-versioned alias, from when we thought that field names would stay the same.
Mux allows developers to handle logically related groups of functionality multiplexed based on the event type.