khhttp

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyHostHeaders added in v0.10.0

func ApplyHostHeaders(req *http.Request, host string)

ApplyHostHeaders sets per-host headers (hosts.yml + CF env) on req.

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 HeadersForHost added in v0.10.0

func HeadersForHost(host string) map[string]string

HeadersForHost returns the set of headers to apply to a request targeting host: per-host entries from hosts.yml with Cloudflare Access env vars merged on top. Used by code paths that build their own *http.Client and bypass the shared khhttp.Client (auth token validation, doctor checks).

func MergeCloudflareAccessEnv added in v0.10.0

func MergeCloudflareAccessEnv(base map[string]string) map[string]string

MergeCloudflareAccessEnv returns base with Cloudflare Access headers added from the environment. Env values take precedence over base entries with the same key (matching the KH_API_KEY > hosts.yml precedence used elsewhere).

Service-token headers are only added when both ID and secret are set, to avoid sending half a credential pair.

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

	// OrgOverride is the organization ID from the --org flag.
	// When non-empty, X-Organization-Id is sent on every request.
	OrgOverride 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