Files
archipelago/sdk/connector/connector.go
T

73 lines
2.2 KiB
Go

// Package connector is what a Go connector extension builds on: the
// generated connector.v1 contract, the interfaces a connector implements and
// the Serve helper archie-core launches it with.
package connector
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json/jsontext"
"time"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// Contract is the connector contract version this SDK speaks.
const Contract = "v1"
// Config is the operator's settings and the host-resolved secrets.
type Config struct {
Settings map[string]string
Secrets map[string]string
}
// Description declares what a connector hears, does and answers.
type Description struct {
Events []*connectorv1.EventType
Actions []*connectorv1.ActionType
Lookups []*connectorv1.LookupType
}
// Event is one typed event parsed from a verified payload.
type Event struct {
Type string
SourceEventID string
Fields jsontext.Value
LinkKey string
OccurredAt time.Time
}
// Connector is the part every connector implements.
type Connector interface {
Describe() Description
}
// Parser turns a payload the host already verified into typed events.
type Parser interface {
Parse(ctx context.Context, body []byte, headers map[string]string) ([]Event, error)
}
// Actor resolves and runs actions. Invoke must honour the idempotency key for
// any action that isn't read-only, and returns unknown=true when it can't
// confirm what its effect did.
type Actor interface {
Resolve(ctx context.Context, action string, input jsontext.Value) (*connectorv1.ResolveResponse, error)
Invoke(ctx context.Context, action string, input jsontext.Value, idempotencyKey string) (output jsontext.Value, unknown bool, err error)
}
// Looker answers read-only lookups. found=false means nothing was found.
type Looker interface {
Lookup(ctx context.Context, name string, input jsontext.Value) (output jsontext.Value, found bool, err error)
}
// Digest identifies one concrete action, the same way archie-core does: its
// name and its exact input.
func Digest(action string, input []byte) string {
h := sha256.New()
h.Write([]byte(action))
h.Write([]byte{0})
h.Write(input)
return hex.EncodeToString(h.Sum(nil))
}