client

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: MIT Imports: 13 Imported by: 0

README

Delivery Station Public API Client

The client package provides the high-level public API for interacting with the Delivery Station system.

Overview

The Client provides a unified interface for plugins to:

  • Pull and push OCI artifacts from registries (for plugin use)
  • Access shared cache and configuration
  • Manage shared state between plugins
  • Publish and subscribe to events
  • Access the underlying DS subsystems

Note: This is a library for plugin developers. End users interact with DS through plugins, not directly with these APIs.

Usage

Creating a Client
import "github.com/delivery-station/ds/pkg/client"

// Create client with default configuration
client, err := client.NewClient()
if err != nil {
    log.Fatal(err)
}
defer client.Close()

// Or with custom configuration
cfg := &types.Config{
    Cache: types.CacheConfig{
        Dir:     "/path/to/cache",
        MaxSize: 1024 * 1024 * 100, // 100MB
        TTL:     time.Hour,
    },
    Plugins: types.PluginsConfig{
        Dir: "/path/to/plugins",
    },
    Registry: types.RegistryConfig{
        Default: "ghcr.io/delivery-station",
    },
}

client, err := client.NewClient(client.WithConfig(cfg))
Artifact Operations
ctx := context.Background()

// Pull an artifact
err := client.Pull(ctx, "ghcr.io/owner/repo:tag", outputWriter)

// Push an artifact
err := client.Push(ctx, "ghcr.io/owner/repo:tag", inputReader, "application/octet-stream")

// List tags
tags, err := client.List(ctx, "ghcr.io/owner/repo")
Plugin Management
// Install a plugin
err := client.InstallPlugin(ctx, "plugin-name", "1.0.0")

// List installed plugins
plugins, err := client.ListPlugins()

// Execute a plugin
exitCode, err := client.ExecutePlugin("plugin-name", "command", []string{"arg1", "arg2"})
State Management
ctx := context.Background()

// Set state
state := map[string]interface{}{
    "key": "value",
}
err := client.SetState(ctx, "state-key", "plugin-id", state, nil)

// Get state
value, err := client.GetState(ctx, "state-key")
Event Bus
// Subscribe to events
handler := func(ctx context.Context, event *communication.Event) error {
    fmt.Printf("Received event: %s\n", event.Type)
    return nil
}
client.Subscribe(communication.EventCustom, handler)

// Publish events
data := map[string]interface{}{"message": "hello"}
err := client.Publish(ctx, communication.EventCustom, "plugin-id", data)
Accessing Subsystems
// Access underlying components
config := client.Config()
cache := client.Cache()
registry := client.Registry()
pluginMgr := client.PluginManager()
eventBus := client.EventBus()
stateStore := client.StateStore()
pluginRegistry := client.PluginRegistry()

Features

  • Simple API: High-level, easy-to-use interface
  • Flexible Configuration: Support for custom configurations and dependency injection
  • Integrated Subsystems: Unified access to cache, registry, plugins, and communication
  • Extensible: Access to underlying components for advanced use cases
  • Well-Tested: Comprehensive test coverage

Architecture

The Client orchestrates several internal subsystems:

  1. Config: Configuration management and loading
  2. Cache: Artifact caching layer
  3. Registry: OCI registry client
  4. Plugin Manager: Plugin lifecycle management
  5. Event Bus: Event-driven communication
  6. State Store: Shared state management
  7. Plugin Registry: Plugin discovery and registration

Best Practices

  1. Always use defer client.Close() to ensure proper cleanup
  2. Pass context for cancellation and timeouts
  3. Use the functional options pattern for custom configuration
  4. Check errors from all operations
  5. Use the high-level Client API rather than directly accessing subsystems

Testing

Run the client tests:

go test ./pkg/client/... -v

Examples

See the test file client_test.go for comprehensive usage examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is the main public API for interacting with the delivery station

func NewClient

func NewClient(opts ...Option) (*Client, error)

NewClient creates a new delivery station client

func (*Client) Cache

func (c *Client) Cache() *cache.Cache

Cache returns the cache instance

func (*Client) Close

func (c *Client) Close() error

Close cleans up resources

func (*Client) Config

func (c *Client) Config() *types.Config

Config returns the current configuration

func (*Client) DiscoverPlugin

func (c *Client) DiscoverPlugin(ctx context.Context, name string) error

DiscoverPlugin discovers a plugin by name Note: This returns communication.PluginInfo internally

func (*Client) EventBus

func (c *Client) EventBus() *communication.EventBus

EventBus returns the event bus

func (*Client) ExecutePlugin

func (c *Client) ExecutePlugin(name, operation string, args []string) (int, error)

ExecutePlugin runs a plugin command

func (*Client) GetState

func (c *Client) GetState(ctx context.Context, key string) (map[string]interface{}, error)

GetState retrieves state by key

func (*Client) InstallPlugin

func (c *Client) InstallPlugin(ctx context.Context, ref, version string) error

InstallPlugin installs a plugin from a reference

func (*Client) List

func (c *Client) List(ctx context.Context, repository string) ([]string, error)

List returns available tags for a repository

func (*Client) ListPlugins

func (c *Client) ListPlugins() ([]*types.PluginInfo, error)

ListPlugins returns all installed plugins

func (*Client) PluginManager

func (c *Client) PluginManager() *plugin.Manager

PluginManager returns the plugin manager

func (*Client) PluginRegistry

func (c *Client) PluginRegistry() *communication.PluginRegistry

PluginRegistry returns the plugin registry

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, eventType communication.EventType, pluginID string, data map[string]interface{}) error

Publish publishes an event to the event bus

func (*Client) Pull

func (c *Client) Pull(ctx context.Context, ref string, writer io.Writer) error

Pull downloads an artifact from a registry

func (*Client) Push

func (c *Client) Push(ctx context.Context, ref string, reader io.Reader, mediaType string) error

Push uploads an artifact to a registry

func (*Client) RegisterPlugin

func (c *Client) RegisterPlugin(ctx context.Context, name, version, path string) error

RegisterPlugin registers a plugin in the plugin registry Note: This uses communication.PluginInfo internally

func (*Client) Registry

func (c *Client) Registry() *registry.Client

Registry returns the registry client

func (*Client) SetState

func (c *Client) SetState(ctx context.Context, key string, pluginID string, value map[string]interface{}, ttl *time.Duration) error

SetState sets state by key

func (*Client) StateStore

func (c *Client) StateStore() *communication.StateStore

StateStore returns the state store

func (*Client) Subscribe

func (c *Client) Subscribe(topic communication.EventType, handler communication.EventHandler)

Subscribe subscribes to events from the event bus

type Option

type Option func(*Client) error

Option is a functional option for configuring the Client

func WithCache

func WithCache(cache *cache.Cache) Option

WithCache sets the cache

func WithConfig

func WithConfig(cfg *types.Config) Option

WithConfig sets the configuration

func WithPluginManager

func WithPluginManager(mgr *plugin.Manager) Option

WithPluginManager sets the plugin manager

func WithRegistry

func WithRegistry(reg *registry.Client) Option

WithRegistry sets the registry client

Jump to

Keyboard shortcuts

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