plugins

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package plugins provides various utilities and helpers for interacting with plugins, including sending HTTP requests to plugin endpoints, decoding request bodies, and validating plugin types against JSON schemas.

The package also includes mechanisms for managing plugin error handling, waiting for plugin readiness, and making plugin calls with various options.

Key components:

  • **Call**: Makes HTTP requests to plugin endpoints with optional payloads, headers, and query parameters.
  • **Error**: A custom error type for handling plugin-related errors in HTTP responses.
  • **ValidatePlugin**: Validates an incoming raw type against a given JSON schema.
  • **WaitForPlugin**: Waits for a plugin to become ready by making periodic health checks. Once the plugin is ready it sets up a client which can then be used to interact with said plugin.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(ctx context.Context, client *http.Client, locationType types.ConnectionType, location, endpoint, method string, opts ...CallOptionFn) (err error)

Call will use the plugin's constructed connection client to make a call to the specified endpoint. The result will be marshalled into the provided response if not nil.

func DecodeJSONRequestBody

func DecodeJSONRequestBody[T any](writer http.ResponseWriter, request *http.Request) (*T, error)

DecodeJSONRequestBody takes a request and any type and decodes the request's body into that type and returns the result.

func GenerateJSONSchemaForType added in v0.0.12

func GenerateJSONSchemaForType(obj runtime.Typed) ([]byte, error)

GenerateJSONSchemaForType takes a Type and uses reflection to generate a JSON Schema representation for it. It will also use the correct type representation as we don't marshal the type in object format.

func StartLogStreamer

func StartLogStreamer(ctx context.Context, plugin *types.Plugin)

StartLogStreamer is intended to be launched when the plugin is started. It will continuously stream logs from the plugin to the context debug logger. Logs in the plugin has to be written to stderr. The expected logger using stderr should be set up like this:

logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
		Level: slog.LevelDebug, // debug level here is respected when sending this message.
}))

func ValidatePlugin

func ValidatePlugin(typ runtime.Typed, jsonSchema []byte) (bool, error)

ValidatePlugin will take a runtime Type and validate it against the given JSON Schema.

func WaitForPlugin

func WaitForPlugin(ctx context.Context, plugin *types.Plugin) (*http.Client, string, error)

WaitForPlugin sets up the HTTP client for the plugin and waits for it to become available. It returns the configured HTTP client, the plugin location, and any error encountered.

Types

type CallOptionFn

type CallOptionFn func(opt *CallOptions)

CallOptionFn defines a function that sets parameters for the Call method.

func WithHeader

func WithHeader(header KV) CallOptionFn

WithHeader sets a specific header for the call.

func WithHeaders

func WithHeaders(headers []KV) CallOptionFn

WithHeaders sets headers for the call.

func WithPayload

func WithPayload(payload any) CallOptionFn

WithPayload sets up payload to send to the callee

func WithQueryParams

func WithQueryParams(queryParams []KV) CallOptionFn

WithQueryParams sets url parameters for the call.

func WithResult

func WithResult(result any) CallOptionFn

WithResult sets up a result that the call will marshal into.

type CallOptions

type CallOptions struct {
	Payload     any
	Result      any
	Headers     []KV
	QueryParams []KV
}

CallOptions contains options for calling a plugin endpoint.

type Error

type Error struct {
	Err    error `json:"error"`
	Status int   `json:"status"`
}

func NewError

func NewError(err error, status int) *Error

NewError creates a new Error instance with the provided error and HTTP status code.

func (*Error) Write

func (e *Error) Write(w http.ResponseWriter)

type KV

type KV struct {
	Key   string
	Value string
}

Jump to

Keyboard shortcuts

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