Documentation
¶
Index ¶
- func ApplyHostHeaders(req *http.Request, host string)
- func BuildBaseURL(host string) string
- func HeadersForHost(host string) map[string]string
- func MergeCloudflareAccessEnv(base map[string]string) map[string]string
- func SemverLessThan(current, minimum string) bool
- type APIError
- type Client
- type ClientOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyHostHeaders ¶ added in v0.10.0
ApplyHostHeaders sets per-host headers (hosts.yml + CF env) on req.
func BuildBaseURL ¶
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
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
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 ¶
SemverLessThan is the exported form used in tests.
Types ¶
type APIError ¶
APIError wraps an HTTP error response with status code and body.
func NewAPIError ¶
NewAPIError reads the response body and constructs an APIError. It attempts to extract a JSON "error" or "message" field for the message.
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 ¶
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 ¶
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.