extsdk

package
v0.16.399 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: AGPL-3.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package extsdk is the skeleton of an adapter of the hub's extension protocol (docs/hub/extensions.md): a separate process that the hub calls over HTTP. It answers what every adapter must answer the same way, and leaves the calls of its own kind to the author:

  • GET /v1/describe: who the adapter is, the protocol it speaks (harnessly.<kind>/v<major>) and its capabilities;
  • the bearer token, checked on every call (constant time), when one is set;
  • the Idempotency-Key of a call that changes something: a repeat with the same key gets the first answer again and the work is not done twice (the hub resends a call after a timeout or a restart);
  • the error body {"error": "<code>", "message": "<text>"} (WriteError); 501 or `not_supported` means «this call is not available».

It imports nothing of the hub; an adapter is built and released on its own.

s := extsdk.New(extsdk.Description{Protocol: "harnessly.machine-provider/v1", Name: "acme", Version: "1.0.0",
	Capabilities: []string{"state"}}, extsdk.Options{Token: os.Getenv("ADAPTER_TOKEN")})
s.Handle("GET /v1/machines/{id}", func(w http.ResponseWriter, r *http.Request) { ... })
s.Handle("POST /v1/machines", s.Idempotent(create))
log.Fatal(http.ListenAndServe(":8080", s))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NotSupported

func NotSupported(w http.ResponseWriter, r *http.Request)

NotSupported answers a call the adapter does not do.

func WriteError

func WriteError(w http.ResponseWriter, status int, code, message string)

WriteError answers with the error body of the protocol.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON answers with a JSON body.

Types

type Description

type Description struct {
	Protocol     string         `json:"protocol"`
	Name         string         `json:"name"`
	Version      string         `json:"version"`
	Capabilities []string       `json:"capabilities"`
	ConfigSchema map[string]any `json:"config_schema,omitempty"`
}

Description is the answer of GET /v1/describe.

type Options

type Options struct {
	// Token is the bearer token the hub calls with; empty means no check (only for a trusted network).
	Token string
	// MaxReplays is how many answers of mutating calls are kept to be replayed; zero means 1024. The oldest go first.
	MaxReplays int
}

Options are the adapter's settings.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the adapter's HTTP handler.

func New

func New(d Description, o Options) *Server

New makes a server that already answers describe.

func (*Server) Handle

func (s *Server) Handle(pattern string, h http.HandlerFunc)

Handle registers a call of the adapter's kind, with a pattern as http.ServeMux takes it («POST /v1/machines»).

func (*Server) Idempotent

func (s *Server) Idempotent(h http.HandlerFunc) http.HandlerFunc

Idempotent wraps the handler of a call that changes something. A request with an Idempotency-Key that was answered before gets that answer again and the handler does not run; a request without a key is refused, because the hub always sends one and an adapter that works without it would do the work twice after a retry. The answer is kept only when it is not a 5xx: a failure may be tried again. (Two requests with the same key at the same moment are not serialized here: an adapter with a real side effect keeps the key with the work, in its own store.)

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP checks the token and routes the call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL