Help us shape the future of Go! Take the 2026 Go Developer Survey

http

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MethodGet    = gohttp.MethodGet
	MethodPost   = gohttp.MethodPost
	MethodPut    = gohttp.MethodPut
	MethodDelete = gohttp.MethodDelete
)

The request methods this repository sends, re-exported so that a caller of DoRequest needs no second import for them. Add one here when a caller starts sending it.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authorization

type Authorization interface {
	GetBearerToken(ctx context.Context) (BearerToken, error)

	// RefreshBearerToken returns the token that replaces the rejected one. Returning the
	// rejected token unchanged tells the caller that there is nothing new to try.
	RefreshBearerToken(ctx context.Context, rejected BearerToken) (BearerToken, error)
}

Authorization produces the bearer token for each request. An implementation renews the token it holds once that token is close to expiry, so that AuthorizedClient.DoRequest only has to handle the 401 a token lost earlier than expected.

type AuthorizedClient

type AuthorizedClient struct {
	Client

	Authorization Authorization
}

func (AuthorizedClient) DoRequest

func (c AuthorizedClient) DoRequest[R any](ctx context.Context, method string, url *url.URL, options ...RequestOption) (R, error)

type BearerToken

type BearerToken string

BearerToken is both the token Authorization produces and an Authorization of its own, for a caller that holds one static token.

func (BearerToken) GetBearerToken

func (token BearerToken) GetBearerToken(_ context.Context) (BearerToken, error)

func (BearerToken) RefreshBearerToken

func (token BearerToken) RefreshBearerToken(_ context.Context, _ BearerToken) (BearerToken, error)

type Client

type Client struct {
	*gohttp.Client

	UserAgent string
}

func NewClient

func NewClient(userAgent string) Client

func (Client) DoRequest

func (c Client) DoRequest[R any](ctx context.Context, method string, url *url.URL, options ...RequestOption) (result R, err error)

DoRequest sends one request and parses the answer as JSON, or returns it as it came, empty included, for an R of []byte. A non-2xx status is an Error that carries the response body, because an OIDC endpoint answers a refusal with an error document and that document is the only thing saying which refusal it was.

func (Client) Download added in v0.3.0

func (c Client) Download(ctx context.Context, url *url.URL, path string, options ...DownloadOption) (wait func(), err error)

Download waits at most waitForNewerDownload for a newer document where path holds one already. After that, the caller reads the stored document, and wait lets the download finish for the next command.

func (Client) WithAuthorization

func (c Client) WithAuthorization(auth Authorization) AuthorizedClient

type DownloadOption added in v0.3.0

type DownloadOption func(opts *downloadOptions)

func WithConditionalGet added in v0.3.0

func WithConditionalGet(conditionalGet bool) DownloadOption

WithConditionalGet asks for a newer document on every download. Without it, Download asks once per downloadCheckInterval, to save the round trip.

type Error

type Error struct {
	StatusCode   int
	ResponseBody []byte
}

func (Error) Error

func (e Error) Error() string

func (Error) IsClientError added in v0.3.0

func (e Error) IsClientError() bool

func (Error) IsConflict

func (e Error) IsConflict() bool

func (Error) IsForbidden

func (e Error) IsForbidden() bool

func (Error) IsNotFound

func (e Error) IsNotFound() bool

func (Error) IsUnauthorized

func (e Error) IsUnauthorized() bool

type ExponentialBackoff

type ExponentialBackoff struct {
	MinWait, MaxWait time.Duration
}

func (ExponentialBackoff) Calculate

func (b ExponentialBackoff) Calculate(attempt int) time.Duration

type RequestOption

type RequestOption func(opts *requestOptions)

func Retryable

func Retryable() RequestOption

Retryable declares that replaying this request cannot do harm, which is the only way a method other than GET is ever retried. meshStack's /api/login is the case it exists for: it mints a token and invalidates nothing, so a replay after a gateway 503 costs one token.

Never put it on an OIDC refresh grant: that grant rotates the refresh token, and keycloak ends the whole session when a rotated token is used twice.

func WithAccept

func WithAccept(accept string) RequestOption

func WithBody added in v0.3.0

func WithBody(body []byte) RequestOption

WithBody sends the bytes as they are, so the caller names their type in a Content-Type header.

func WithFormPayload

func WithFormPayload(payload any) RequestOption

WithFormPayload sends the values as an url-encoded form body — converted from a struct or a map as WithUrlQuery describes — and asks for JSON in return, which is what an OIDC grant expects.

func WithHeaders added in v0.3.0

func WithHeaders(header gohttp.Header) RequestOption

func WithJsonPayload

func WithJsonPayload(payload any, contentType string) RequestOption

WithJsonPayload sends a value as a JSON body, and both sends and asks for the given content type. The type is a parameter because meshStack names a meshObject's kind and version in it.

func WithUrlQuery

func WithUrlQuery(query any) RequestOption

WithUrlQuery sends each field of query as a query parameter named by its `json` tag. A struct passed by value leaves out every field that has the zero value, so an unset filter needs neither a pointer nor `omitempty`. A map is sent as given, so a deliberate zero such as page=0 stays. Each value is sent as the JSON literal it marshals to, with a string unquoted, and an array as the parameter repeated. Nested objects are not supported.

type RetryBackoff

type RetryBackoff interface {
	Calculate(attempt int) time.Duration
}

type RetryOptions

type RetryOptions struct {
	MaxRetries int
	Backoff    RetryBackoff
}

func (RetryOptions) ApplyTo

func (options RetryOptions) ApplyTo(c *gohttp.Client)

ApplyTo makes the given client retry a GET on its own, and any other method only where the caller marked the request with Retryable.

Jump to

Keyboard shortcuts

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