httpclient

package
v1.1.115 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRequestError is returned when a request could not be made
	ErrRequestError = erx.New("HTTP Client", "failed to make request")

	// ErrCodingError is returned when encoding or decoding request or response bodies has failed
	ErrCodingError = erx.New("HTTP Client", "failed to marshal values")
)

Functions

This section is empty.

Types

type Client

type Client interface {
	Perform(options ...Option) (*Result, error)
}

func NewClient

func NewClient(base *url.URL) Client

func NewClientWithHttpClient

func NewClientWithHttpClient(base *url.URL, httpClient *http.Client) Client

type Codeable

type Codeable interface {
	Encodable
	Decodeable
}

func JSON

func JSON(v interface{}, constraints ...Constraint) Codeable

func Raw

func Raw(buffer *bytes.Buffer, constraints ...Constraint) Codeable

type Constraint

type Constraint func(response *http.Response) bool
var ConstraintFailed Constraint = func(response *http.Response) bool {
	return response.StatusCode >= http.StatusBadRequest
}

ConstraintFailed is a constraint to filter for failed http responses.

var ConstraintNone Constraint = func(response *http.Response) bool {
	return true
}

ConstraintNone is a helper function that filters for nothing and allows everything.

var ConstraintSuccess Constraint = func(response *http.Response) bool {
	return response.StatusCode >= http.StatusOK && response.StatusCode < http.StatusMultipleChoices
}

ConstraintSuccess is a helper function to filter for successful http responses.

type Decodeable

type Decodeable interface {
	ShouldDecode(response *http.Response) bool
	Decode(reader io.Reader) error
}

type Encodable

type Encodable interface {
	Encode(writer io.Writer) error
}

type Option

type Option func(config *requestConfig) error
func Header(key string, value string) Option

func Method

func Method(method string) Option

func Parameter

func Parameter(key string, value string) Option

func Path

func Path(path string) Option

func Request

func Request(body Encodable) Option

func Response

func Response(body Decodeable) Option

func Timeout

func Timeout(timeout time.Duration) Option

type Result

type Result struct {
	StatusCode int
}

Jump to

Keyboard shortcuts

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