Documentation
¶
Index ¶
- Variables
- func CreateTraceParent() string
- func ExtractSpanID(traceParent string) string
- func ExtractTraceID(traceParent string) string
- func FormatTraceContext(traceParent string) string
- func HandleResourceListResponse[T any](resp *http.Response, respBody []byte, config ResponseHandlerConfig) ([]T, error)
- func ListResource[T any](ctx context.Context, c *APIClient, path string, config ResponseHandlerConfig) ([]T, error)
- type APIClient
- type Config
- type Logger
- type ResponseHandlerConfig
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
ExtractSpanID extracts the span ID from a traceparent header value.
func ExtractTraceID ¶
ExtractTraceID extracts the trace ID from a traceparent header value.
func FormatTraceContext ¶
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 ¶
NewAPIClient creates a new API client with the given configuration.
func NewAPIClientWithConfig ¶
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 ¶
SetAuthToken sets the authentication token for the client.
func (*APIClient) SetLogRequests ¶
SetLogRequests enables or disables request logging.
func (*APIClient) SetLogResponses ¶
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.