Documentation ¶
Overview ¶
Package cloudevents implements utilities for handling CloudEvents. For information on the spec, see https://github.com/cloudevents/spec/blob/v0.1/http-transport-binding.md and https://github.com/cloudevents/spec/blob/v0.1/spec.md
Index ¶
- Constants
- func Handler(fn interface{}) http.Handler
- func NewRequest(urlString string, data interface{}, context SendContext) (*http.Request, error)
- type BinaryLoader
- type BinarySender
- type ContextType
- type EventContext
- type HTTPMarshaller
- type LoadContext
- type Mux
- type SendContext
- type StructuredLoader
- type StructuredSender
- type V01EventContext
- func (ec V01EventContext) AsHeaders() http.Header
- func (ec V01EventContext) AsJSON() (map[string]json.RawMessage, error)
- func (ec V01EventContext) AsV01() V01EventContext
- func (ec V01EventContext) DataContentType() string
- func (ec *V01EventContext) FromHeaders(in http.Header) error
- func (ec *V01EventContext) FromJSON(in map[string]json.RawMessage) error
Constants ¶
const ( // HeaderCloudEventsVersion is the header for the version of Cloud Events // used. HeaderCloudEventsVersion = "CE-CloudEventsVersion" // HeaderEventID is the header for the unique ID of this event. HeaderEventID = "CE-EventID" // HeaderEventTime is the OPTIONAL header for the time at which an event // occurred. HeaderEventTime = "CE-EventTime" // HeaderEventType is the header for type of event represented. Value SHOULD // be in reverse-dns form. HeaderEventType = "CE-EventType" // HeaderEventTypeVersion is the OPTIONAL header for the version of the // scheme for the event type. HeaderEventTypeVersion = "CE-EventTypeVersion" // HeaderSchemaURL is the OPTIONAL header for the schema of the event data. HeaderSchemaURL = "CE-SchemaURL" // HeaderSource is the header for the source which emitted this event. HeaderSource = "CE-Source" // HeaderExtensionsPrefix is the OPTIONAL header prefix for CloudEvents extensions HeaderExtensionsPrefix = "CE-X-" // Binary implements Binary encoding/decoding Binary binary = 0 )
const ( // 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. ContentTypeStructuredJSON = "application/cloudevents+json" // ContentTypeBinaryJSON is the content-type for "Binary" encoding where // the event context is in HTTP headers and the body is a JSON event data. ContentTypeBinaryJSON = "application/json" // HeaderContentType is the standard HTTP header "Content-Type" HeaderContentType = "Content-Type" // CloudEventsVersion is a legacy alias of V01CloudEventsVersion, for compatibility. CloudEventsVersion = V01CloudEventsVersion )
const (
// Structured implements the JSON structured encoding/decoding
Structured structured = 0
)
const ( // V01CloudEventsVersion is the version of the CloudEvents spec targeted // by this library. V01CloudEventsVersion = "0.1" )
Variables ¶
This section is empty.
Functions ¶
func Handler ¶
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. Valid types of fn are:
* func() * func() error * func() (anything, error) * func() (anything, EventContext, error) * func(context.Context) * func(context.Context) error * func(context.Context) (anything, error) * func(context.Context) (anything, EventContext, error) * func(context.Context, anything) * func(context.Context, anything) error * func(context.Context, anything) (anything, error) * func(context.Context, anything) (anything, EventContext, error)
CloudEvent contexts are available from the context.Context parameter CloudEvent data will be deserialized into the "anything" parameter. The library supports native decoding with both XML and JSON encoding. To accept another advanced type, pass an io.Reader as the input parameter.
HTTP responses are generated based on the return value of fn:
- any error return value will cause a StatusInternalServerError response
- a function with no return type or a function returning nil will cause a StatusNoContent response
- a function that returns a value will cause a StatusOK and render the response as JSON, with headers from an EventContext, if appropriate
func NewRequest ¶
func NewRequest(urlString string, data interface{}, context SendContext) (*http.Request, error)
NewRequest craetes an HTTP request for Structured content encoding.
Types ¶
type BinaryLoader ¶
type BinaryLoader interface { // FromHeaders copies data from the supplied HTTP headers into the object. // Values will be defaulted if necessary. FromHeaders(in http.Header) error }
BinaryLoader implements an interface for translating a binary encoding HTTP request or response to a an EventContext (possibly one of several versions).
type BinarySender ¶
type BinarySender interface { // AsHeaders converts this EventContext to a set of HTTP headers. AsHeaders() http.Header }
BinarySender implements an interface for sending an EventContext as (possibly one of several versions) as a binary encoding HTTP request.
type ContextType ¶
type ContextType interface { SendContext LoadContext }
ContextType is a unified interface for both sending and loading the CloudEvent data across versions.
type EventContext ¶
type EventContext = V01EventContext
EventContext is a legacy un-versioned alias, from when we thought that field names would stay the same.
func FromRequest ¶
func FromRequest(data interface{}, r *http.Request) (*EventContext, error)
FromRequest parses a CloudEvent from any known encoding.
type HTTPMarshaller ¶
type HTTPMarshaller interface { FromRequest(data interface{}, r *http.Request) (*EventContext, error) NewRequest(urlString string, data interface{}, context SendContext) (*http.Request, error) }
HTTPMarshaller implements a scheme for decoding CloudEvents over HTTP. Implementations are Binary, Structured, and Any
type LoadContext ¶
type LoadContext interface { StructuredLoader BinaryLoader // AsV01 provides a translation from whatever the "native" encoding of the // CloudEvent was to the equivalent in v0.1 field names, moving fields to or // from extensions as necessary. AsV01() V01EventContext }
LoadContext provides an interface for extracting information from an EventContext (the set of non-data event attributes of a CloudEvent).
LoadContext also provides a set of translation methods between the versions of the CloudEvents spec, which allows programs to interoperate with different versions of the CloudEvents spec at the same time.
type Mux ¶
type Mux map[string]*handler
Mux allows developers to handle logically related groups of functionality multiplexed based on the event type. TODO: Consider dropping Mux or figure out how to handle non-JSON encoding.
func (Mux) Handle ¶
Handle adds a new handler for a specific event type If the fn parameter is not a valid type, the endpoint will respond to all HTTP requests with that error. Valid types of fn are:
* func() * func() error * func() (anything, error) * func(context.Context) * func(context.Context) error * func(context.Context) (anything, error) * func(context.Context, anything) * func(context.Context, anything) error * func(context.Context, anything) (anything, error)
CloudEvent contexts are available from the context.Context parameter CloudEvent data will be deserialized into the "anything" parameter. The library supports native decoding with both XML and JSON encoding. To accept another advanced type, pass an io.Reader as the input parameter.
HTTP responses are generated based on the return value of fn: * any error return value will cause a StatusInternalServerError response * a function with no return type or a function returning nil will cause a StatusNoContent response * a function that returns a value will cause a StatusOK and render the response as JSON
type SendContext ¶
type SendContext interface { StructuredSender BinarySender }
SendContext provides an interface for extracting information from an EventContext (the set of non-data event attributes of a CloudEvent).
type StructuredLoader ¶
type StructuredLoader interface { // FromJSON assumes that the object has already been decoded into a raw map // from string to json.RawMessage, because this is needed to extract the // CloudEvents version. FromJSON(map[string]json.RawMessage) error }
StructuredLoader implements an interface for translating a structured encoding HTTP request or response to a an EventContext (possibly one of several versions).
type StructuredSender ¶
type StructuredSender interface { // AsJSON encodes the object into a map from string to JSON data, which // allows additional keys to be encoded later. AsJSON() (map[string]json.RawMessage, error) // DataContentType returns the MIME content type for encoding data. DataContentType() string }
StructuredSender implements an interface for translating an EventContext (possibly one of severals versions) to a structured encoding HTTP request.
type V01EventContext ¶
type V01EventContext struct { // The version of the CloudEvents specification used by the event. CloudEventsVersion string `json:"cloudEventsVersion,omitempty"` // ID of the event; must be non-empty and unique within the scope of the producer. EventID string `json:"eventID"` // Timestamp when the event happened. EventTime time.Time `json:"eventTime,omitempty"` // Type of occurrence which has happened. EventType string `json:"eventType"` // The version of the `eventType`; this is producer-specific. EventTypeVersion string `json:"eventTypeVersion,omitempty"` // A link to the schema that the `data` attribute adheres to. SchemaURL string `json:"schemaURL,omitempty"` // A MIME (RFC 2046) string describing the media type of `data`. // TODO: Should an empty string assume `application/json`, or auto-detect the content? ContentType string `json:"contentType,omitempty"` // A URI describing the event producer. Source string `json:"source"` // Additional metadata without a well-defined structure. Extensions map[string]interface{} `json:"extensions,omitempty"` }
V01EventContext holds standard metadata about an event. See https://github.com/cloudevents/spec/blob/v0.1/spec.md#context-attributes for details on these fields.
func FromContext ¶
func FromContext(ctx context.Context) *V01EventContext
FromContext loads an V01EventContext from a normal context.Context
func (V01EventContext) AsHeaders ¶
func (ec V01EventContext) AsHeaders() http.Header
AsHeaders implements the BinarySender interface.
func (V01EventContext) AsJSON ¶
func (ec V01EventContext) AsJSON() (map[string]json.RawMessage, error)
AsJSON implements the StructuredSender interface
func (V01EventContext) AsV01 ¶
func (ec V01EventContext) AsV01() V01EventContext
AsV01 implements the LoadContext interface.
func (V01EventContext) DataContentType ¶
func (ec V01EventContext) DataContentType() string
DataContentType implements the StructuredSender interface
func (*V01EventContext) FromHeaders ¶
func (ec *V01EventContext) FromHeaders(in http.Header) error
FromHeaders implements the BinaryContext interface
func (*V01EventContext) FromJSON ¶
func (ec *V01EventContext) FromJSON(in map[string]json.RawMessage) error
FromJSON implements the StructuredContext interface