khhttp

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildBaseURL

func BuildBaseURL(host string) string

BuildBaseURL normalises a host string into a base URL. If host already starts with http:// or https://, it is returned as-is (with any trailing slash stripped). Otherwise https:// is prepended.

func SemverLessThan

func SemverLessThan(current, minimum string) bool

SemverLessThan is the exported form used in tests.

Types

type APIError

type APIError struct {
	StatusCode int
	Body       []byte
	Message    string
}

APIError wraps an HTTP error response with status code and body.

func NewAPIError

func NewAPIError(resp *http.Response) *APIError

NewAPIError reads the response body and constructs an APIError. It attempts to extract a JSON "error" or "message" field for the message.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

type Client

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

Client is a retryable HTTP client that injects version and auth headers on every outgoing request.

func NewClient

func NewClient(opts ClientOptions) *Client

NewClient creates a Client wrapping hashicorp/go-retryablehttp with 3 retries, exponential backoff between 1s and 30s, and suppressed logging.

func (*Client) Do

func (c *Client) Do(req *retryablehttp.Request) (*http.Response, error)

Do executes a retryablehttp.Request, injecting version and auth headers before the first attempt and checking the version compatibility header after each successful response.

func (*Client) NewRequest

func (c *Client) NewRequest(method, url string, body interface{}) (*retryablehttp.Request, error)

NewRequest is a convenience wrapper around retryablehttp.NewRequest.

func (*Client) StandardClient

func (c *Client) StandardClient() *http.Client

StandardClient returns the underlying *http.Client for compatibility with libraries that require a standard net/http client.

type ClientOptions

type ClientOptions struct {
	// Host is the base URL for this client (used for display / logging only).
	Host string

	// Token is the Bearer token sent in the Authorization header.
	// If empty, no Authorization header is added.
	Token string

	// Headers are additional per-host headers injected on every request
	// (e.g. Cloudflare Access headers loaded from hosts.yml).
	Headers map[string]string

	// IOStreams provides the ErrOut writer for version warnings.
	IOStreams *iostreams.IOStreams

	// AppVersion is the CLI version string sent in the KH-CLI-Version header.
	AppVersion string
}

ClientOptions configures a new Client.

Jump to

Keyboard shortcuts

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