rest

package
v0.45.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RESTErrorStringLimit = 100

Functions

This section is empty.

Types

type Body

type Body interface {
	Bytes() []byte
	fmt.Stringer
	io.Reader
}

type ClientOpt added in v0.40.0

type ClientOpt func(RestClient)

ClientOpt functions configure the client.

func AddHeader added in v0.40.0

func AddHeader(key, value string) ClientOpt

AddHeader sets a request header that will be applied to all subsequent requests.

func SetAuthentication added in v0.40.0

func SetAuthentication(authenticator authpkg.Authenticator) ClientOpt

SetAuthentication sets the authentication credentials and method. Leave the authenticator method blank to allow HTTP challenges to select an appropriate method. Otherwise it should be "basic".

func SetHttpClient added in v0.40.0

func SetHttpClient(httpClient httpclient.HttpClient) ClientOpt

SetHttpClient changes the http.Client. This allows control over the http.Transport, timeouts etc.

type ReqOpt added in v0.40.0

type ReqOpt func(*http.Request)

ReqOpt optionally amends or enhances a request before it is sent.

func Headers

func Headers(kv ...string) ReqOpt

Headers adds header values to the request. kv is a list of key & value pairs.

func IfMatch added in v0.42.0

func IfMatch(etag ...header.ETag) ReqOpt

IfMatch makes a request conditional upon ETags and is typically used for PUT, POST and DELETE requests. There is also an If-Unmodified-Since header, but If-Match takes precedence (see RFC-9110).

func IfModifiedSince added in v0.42.0

func IfModifiedSince(t time.Time) ReqOpt

IfModifiedSince makes a request conditional upon change history and is typically used for GET requests.

func IfNoneMatch added in v0.42.0

func IfNoneMatch(etag ...header.ETag) ReqOpt

IfNoneMatch makes a request conditional upon ETags and is typically used for GET requests.

func Query added in v0.40.0

func Query(kv ...string) ReqOpt

Query adds query values to the request. kv is a list of key & value pairs.

type Response added in v0.40.0

type Response struct {
	// StatusCode the HTTP status code
	StatusCode int
	// Header the response headers
	Header http.Header
	// Type the content type of the response entity
	Type header.ContentType
	// Body the buffered response entity
	Body *bodypkg.Body
	// Request the original request
	Request *http.Request
}

Response holds an HTTP response with the entity in a buffer.

type RestClient added in v0.40.0

type RestClient interface {
	Request(ctx context.Context, method, path string, reqBody any, opts ...ReqOpt) (*http.Response, error)
	Head(ctx context.Context, path string, opts ...ReqOpt) (*Response, error)
	Get(ctx context.Context, path string, opts ...ReqOpt) (*Response, error)
	Put(ctx context.Context, path string, reqBody any, opts ...ReqOpt) (*Response, error)
	Post(ctx context.Context, path string, reqBody any, opts ...ReqOpt) (*Response, error)
	Delete(ctx context.Context, path string, reqBody any, opts ...ReqOpt) (*Response, error)
	ClearCookies()
}

RestClient implements HTTP client requests as typically used for REST APIs etc.

The Request method returns a http.Response that may contain a body as an io.ReadCloser, which can be handled appropriately by the caller. The caller must close this body (if the response is not nil).

The Head, Get, Put, Post, and Delete methods return a Response containing a buffered body that is simpler to use but potentially less performant for large bodies.

func NewClient added in v0.40.0

func NewClient(uri string, opts ...ClientOpt) RestClient

NewClient creates a new Client. By default, this uses the default HTTP client.

type RestError added in v0.40.0

type RestError struct {
	Response
	Cause error
}

func (*RestError) Error added in v0.40.0

func (re *RestError) Error() string

Error makes it compatible with `error` interface.

func (*RestError) IsPermanent added in v0.45.0

func (re *RestError) IsPermanent() bool

IsPermanent returns the opposite of RestError.IsTransient; the request should not be retried.

func (*RestError) IsTransient added in v0.45.0

func (re *RestError) IsTransient() bool

IsTransient returns true for server/network errors that can be retried. (Note that 401 authentication challenges should be responded to normally).

func (*RestError) Unwrap added in v0.40.0

func (re *RestError) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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