Documentation
¶
Overview ¶
Package http provides HTTP helper functions and error types.
Index ¶
- Constants
- func Delete[T resources.Resource](ctx context.Context, client Client, url url.URL) error
- func Get[T resources.Resource](ctx context.Context, client Client, u url.URL) (entity T, err error)
- func Patch[T resources.Resource](ctx context.Context, client Client, payload interface{}, url url.URL) (entity T, err error)
- func Post[T resources.Resource](ctx context.Context, client Client, payload any, url url.URL) (entity T, err error)
- func Put[T resources.Resource](ctx context.Context, client Client, payload interface{}, url url.URL) (entity T, err error)
- func Request[T resources.Resource](ctx context.Context, c Client, method string, url url.URL, payload any) (res T, err error)
- type Client
- type Credential
Constants ¶
const ErrConflict errors.Kind = "conflict (HTTP Status 409)"
ErrConflict indicates the request contains conflicting data (eg.: duplicated resource)
const ErrNotFound errors.Kind = "resource not found (HTTP Status 404)"
ErrNotFound indicates the requested resource does not exist in the server.
const ErrUnexpectedResponseBody errors.Kind = "unexpected API response body"
ErrUnexpectedResponseBody indicates the server responded with an unexpected body.
const ErrUnexpectedStatus errors.Kind = "unexpected status code"
ErrUnexpectedStatus indicates the server responded with an unexpected status code.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
Get requests the endpoint components list making a GET request and decode the response into the entity T if validates successfully.
func Patch ¶
func Patch[T resources.Resource](ctx context.Context, client Client, payload interface{}, url url.URL) (entity T, err error)
Patch requests the endpoint components list making a PATCH request and decode the response into the entity T if validates successfully.
func Post ¶
func Post[T resources.Resource](ctx context.Context, client Client, payload any, url url.URL) (entity T, err error)
Post requests the endpoint components list making a POST request and decode the response into the entity T if validates successfully.
func Put ¶
func Put[T resources.Resource](ctx context.Context, client Client, payload interface{}, url url.URL) (entity T, err error)
Put requests the endpoint components list making a PUT request and decode the response into the entity T if validated successfully.
func Request ¶
func Request[T resources.Resource](ctx context.Context, c Client, method string, url url.URL, payload any) (res T, err error)
Request makes a request to the Terramate Cloud using client. The instantiated type gets decoded and return as the entity T, The payload is encoded accordingly to the rules below: - If payload is nil, no body is sent and no Content-Type is set. - If payload is a []byte or string, it is sent as is and the Content-Type is set to text/plain. - If payload is any other type, it is marshaled to JSON and the Content-Type is set to application/json.
Types ¶
type Client ¶
type Client interface {
HTTPClient() *stdhttp.Client
Credential() Credential
}
Client is the interface for the HTTP client.
type Credential ¶
type Credential interface {
// ApplyCredentials applies the credential to the given request.
ApplyCredentials(req *stdhttp.Request) error
// RedactCredentials redacts the credential from the given request.
// This is used for dumping the request without exposing sensitive information.
RedactCredentials(req *stdhttp.Request)
}
Credential is the interface for the credential providers. Because each provider has its own way to authenticate, the dependency inversion principle is used to abstract the authentication method.