client

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrResourceNotFound indicates a resource was not found.
	ErrResourceNotFound = errors.New("resource not found")
	// ErrAccessDenied indicates access to a resource was denied.
	ErrAccessDenied = errors.New("access denied")
	// ErrServerError indicates a server error occurred.
	ErrServerError = errors.New("server error")
	// ErrUnexpectedStatus indicates an unexpected status code was received.
	ErrUnexpectedStatus = errors.New("unexpected status code")
)
View Source
var (
	// ErrUnexpectedStatusCode indicates an unexpected HTTP status code was received.
	ErrUnexpectedStatusCode = errors.New("unexpected status code")
)

Functions

func CreateTraceParent

func CreateTraceParent() string

CreateTraceParent creates a W3C traceparent header value.

func ExtractSpanID

func ExtractSpanID(traceParent string) string

ExtractSpanID extracts the span ID from a traceparent header value.

func ExtractTraceID

func ExtractTraceID(traceParent string) string

ExtractTraceID extracts the trace ID from a traceparent header value.

func FormatTraceContext

func FormatTraceContext(traceParent string) string

FormatTraceContext formats trace ID and span ID for log output.

func HandleResourceListResponse

func HandleResourceListResponse[T any](resp *http.Response, respBody []byte, config ResponseHandlerConfig) ([]T, error)

HandleResourceListResponse handles common response patterns for resource listing endpoints using type-safe generics. Returns an empty slice with an error for error cases where AllowForbidden or AllowNotFound is true. Type parameter T should be the element type (e.g., openapi.Cluster, openapi.Region).

func ListResource

func ListResource[T any](ctx context.Context, c *APIClient, path string, config ResponseHandlerConfig) ([]T, error)

ListResource is a type-safe generic helper for list operations. Type parameter T should be the element type (e.g., openapi.Cluster). Example usage: ListResource[openapi.Cluster](ctx, client, path, config).

Types

type APIClient

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

APIClient provides a generic HTTP client for API testing with trace context support.

func NewAPIClient

func NewAPIClient(baseURL, authToken string, timeout time.Duration, logger Logger) *APIClient

NewAPIClient creates a new API client with the given configuration.

func NewAPIClientWithConfig

func NewAPIClientWithConfig(config Config, authToken string, logger Logger) *APIClient

NewAPIClientWithConfig creates a new API client with the given configuration struct.

func (*APIClient) DoRequest

func (c *APIClient) DoRequest(ctx context.Context, method, path string, body io.Reader, expectedStatus int) (*http.Response, []byte, error)

DoRequest performs an HTTP request with W3C trace context and returns the response. If expectedStatus is > 0, the function will return an error if the response status doesn't match.

func (*APIClient) SetAuthToken

func (c *APIClient) SetAuthToken(token string)

SetAuthToken sets the authentication token for the client.

func (*APIClient) SetLogRequests

func (c *APIClient) SetLogRequests(enabled bool)

SetLogRequests enables or disables request logging.

func (*APIClient) SetLogResponses

func (c *APIClient) SetLogResponses(enabled bool)

SetLogResponses enables or disables response logging.

type Config

type Config struct {
	BaseURL        string
	RequestTimeout time.Duration
	LogRequests    bool
	LogResponses   bool
}

Config holds the base configuration for the API client.

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger defines the interface for logging in the API client. This allows services to plug in their own logging implementation (e.g., ginkgo.GinkgoWriter).

type ResponseHandlerConfig

type ResponseHandlerConfig struct {
	ResourceType   string
	ResourceID     string
	ResourceIDType string
	AllowForbidden bool
	AllowNotFound  bool
}

ResponseHandlerConfig configures how different status codes should be handled.

Jump to

Keyboard shortcuts

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