Add version/correlation headers to Nexus and Praxis ecosystem clients
mavend's hand-rolled Nexus and Praxis HTTP clients sent bare requests with no version or correlation headers, unlike the Hexis client. Added a shared context-based correlation ID mechanism and version headers (X-Nexus-Version, X-Praxis-Version) across all Nexus/Praxis call sites, and threaded the correlation ID already generated in executeCapability through to the Nexus/Praxis calls in the same request chain. Synced vendor/ copy of hexis/pkg/client after its WithCorrelationID addition. Part of Vikunja #273. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018ghELqYhZNLub2TXGMazqA
This commit is contained in:
+38
@@ -10,6 +10,35 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// APIVersion is sent as X-Hexis-Version on every request so the server can
|
||||
// negotiate/reject unsupported client versions.
|
||||
const APIVersion = "v1"
|
||||
|
||||
type correlationKey struct{}
|
||||
type causationKey struct{}
|
||||
|
||||
// WithCorrelationID returns a context that carries a correlation ID to be
|
||||
// sent as X-Correlation-ID on every Hexis request made with it.
|
||||
func WithCorrelationID(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, correlationKey{}, id)
|
||||
}
|
||||
|
||||
// WithCausationID returns a context that carries a causation ID to be sent
|
||||
// as X-Causation-ID on every Hexis request made with it.
|
||||
func WithCausationID(ctx context.Context, id string) context.Context {
|
||||
return context.WithValue(ctx, causationKey{}, id)
|
||||
}
|
||||
|
||||
func correlationIDFrom(ctx context.Context) string {
|
||||
id, _ := ctx.Value(correlationKey{}).(string)
|
||||
return id
|
||||
}
|
||||
|
||||
func causationIDFrom(ctx context.Context) string {
|
||||
id, _ := ctx.Value(causationKey{}).(string)
|
||||
return id
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
httpClient *http.Client
|
||||
@@ -37,6 +66,13 @@ func (c *Client) do(ctx context.Context, method, path string, body, result any)
|
||||
return fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Hexis-Version", APIVersion)
|
||||
if id := correlationIDFrom(ctx); id != "" {
|
||||
req.Header.Set("X-Correlation-ID", id)
|
||||
}
|
||||
if id := causationIDFrom(ctx); id != "" {
|
||||
req.Header.Set("X-Causation-ID", id)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -83,6 +119,7 @@ type Execution struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
RequestedBy map[string]string `json:"requested_by,omitempty"`
|
||||
CorrelationID string `json:"correlation_id,omitempty"`
|
||||
CausationID string `json:"causation_id,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
}
|
||||
|
||||
@@ -94,6 +131,7 @@ type ExecuteRequest struct {
|
||||
Origin map[string]string `json:"origin,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
CorrelationID string `json:"correlation_id,omitempty"`
|
||||
CausationID string `json:"causation_id,omitempty"`
|
||||
}
|
||||
|
||||
type CreateCapabilityRequest struct {
|
||||
|
||||
Reference in New Issue
Block a user