httpclient

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package httpclient provides a generic HTTP client for protosource aggregates with protobuf-first content negotiation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int    `json:"-"`
	Code       string `json:"code"`
	Message    string `json:"error"`
	Detail     string `json:"detail,omitempty"`
}

APIError represents an error response from the server. The server sends {"code": "...", "error": "...", "detail": "..."}.

func (*APIError) Error

func (e *APIError) Error() string

type AuthProvider

type AuthProvider interface {
	Authenticate(req *http.Request) error
	Actor() string
}

AuthProvider decorates outgoing HTTP requests with authentication. Implementations can add bearer tokens, API keys, signed headers, etc. Also provides the actor identity for command attribution.

type BearerTokenAuth

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

BearerTokenAuth adds a Bearer token to each request.

func NewBearerTokenAuth

func NewBearerTokenAuth(token, actor string) *BearerTokenAuth

NewBearerTokenAuth creates an AuthProvider that sets Authorization: Bearer.

func (*BearerTokenAuth) Actor

func (a *BearerTokenAuth) Actor() string

func (*BearerTokenAuth) Authenticate

func (a *BearerTokenAuth) Authenticate(req *http.Request) error

type Client

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

Client is a generic protosource HTTP client with content negotiation.

func New

func New(baseURL string, auth AuthProvider, opts ...Option) *Client

New creates a new Client. Panics if auth is nil.

func (*Client) Apply

func (c *Client) Apply(ctx context.Context, routePath string, cmd proto.Message) (*responsev1.CommandResponse, error)

Apply sends a command to the server and returns the result. The actor field is set from the AuthProvider before serialization.

func (*Client) Get

func (c *Client) Get(ctx context.Context, routePath string, id string, target proto.Message) error

Get retrieves a materialized aggregate by ID from the aggregate store.

func (*Client) History

func (c *Client) History(ctx context.Context, routePath string, id string) (*historyv1.History, error)

History retrieves the event history for an aggregate.

func (*Client) Load

func (c *Client) Load(ctx context.Context, routePath string, id string, target proto.Message) error

Load retrieves an aggregate by ID via event replay, unmarshaling into the provided message.

func (*Client) Query

func (c *Client) Query(ctx context.Context, routePath string, queryPath string, params map[string]string, target proto.Message) error

Query sends a GET request to a query endpoint and unmarshals the response into the target proto.Message (typically an {Aggregate}List). The queryPath is appended to {baseURL}/{routePath}/query/{queryPath}. Params are sent as URL query parameters.

type Doer

type Doer interface {
	Apply(ctx context.Context, routePath string, cmd proto.Message) (*responsev1.CommandResponse, error)
	Load(ctx context.Context, routePath string, id string, target proto.Message) error
	Get(ctx context.Context, routePath string, id string, target proto.Message) error
	History(ctx context.Context, routePath string, id string) (*historyv1.History, error)
	Query(ctx context.Context, routePath string, queryPath string, params map[string]string, target proto.Message) error
}

Doer is the interface that generated per-aggregate clients depend on. *Client satisfies it. Consumers can mock this for testing.

type NoAuth

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

NoAuth provides actor identity without authentication headers. Suitable for local development and testing.

func NewNoAuth

func NewNoAuth(actor string) *NoAuth

NewNoAuth creates an AuthProvider with no authentication.

func (*NoAuth) Actor

func (a *NoAuth) Actor() string

func (*NoAuth) Authenticate

func (a *NoAuth) Authenticate(_ *http.Request) error

type Option

type Option func(*Client)

Option configures a Client.

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient sets a custom http.Client.

func WithJSON

func WithJSON() Option

WithJSON uses JSON serialization instead of protobuf.

Jump to

Keyboard shortcuts

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