httpclient

package module
v1.17.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrParseURL = func(err error) error {
		return errors.New("error on parsing the request url").
			WithCode("ERR_PARSE_REQUEST_URL").
			WithKind(errors.KindInvalidInput).
			WithCause(err)
	}

	ErrBuildRequestError = func(err error) error {
		return errors.New("could not build http request").
			WithCode("ERR_BUILD_HTTP_REQUEST").
			WithKind(errors.KindInternal).
			WithCause(err)
	}

	ErrRequestError = func(err error) error {
		return errors.New("http request error").
			WithCode("HTTP_REQUEST_ERROR").
			WithKind(errors.KindUnknown).
			WithCause(err).
			Retryable()
	}

	ErrBodyReadError = func(err error) error {
		return errors.New("http request error").
			WithCode("ERR_READ_HTTP_RESPONSE_BODY").
			WithKind(errors.KindUnknown).
			WithCause(err)
	}

	ErrUnexpectedStatusCode = func(code int) error {
		kind := kindByStatusCode(code)
		retry := retryByStatusCode[code]

		err := errors.New("unexpected http response status code").
			WithCode("ERR_UNEXPECTED_STATUS_CODE").
			WithKind(kind)

		if !retry {
			return err
		}

		return err.Retryable()
	}

	ErrBodyCastError = func(err error) error {
		return errors.New("could not cast http response body").
			WithCode("ERR_HTTP_RESPONSE_BODY_CAST").
			WithKind(errors.KindUnknown).
			WithCause(err)
	}
)

All errors that can be emitted by http requests.

Functions

This section is empty.

Types

type HTTPClient added in v1.7.0

type HTTPClient interface {
	// Get execute a http GET method.
	Get(context.Context, *Request, ...RequestOption) (*Response, error)

	// Post execute a http POST method with application/json headers.
	Post(context.Context, *Request, ...RequestOption) (*Response, error)

	// PostForm execute a http POST method with application/x-www-form-urlencoded headers.
	PostForm(context.Context, *Request, ...RequestOption) (*Response, error)

	// Put execute a http PUT method with application/json headers.
	Put(context.Context, *Request, ...RequestOption) (*Response, error)

	// Patch execute a http PATCH method with application/json headers.
	Patch(context.Context, *Request, ...RequestOption) (*Response, error)

	// Delete execute a http DELETE method with application/json headers.
	Delete(context.Context, *Request, ...RequestOption) (*Response, error)
}

HTTPClient is responsible for performing http requests.

func New

func New(opts ...Option) HTTPClient

New creates a new Client instance.

type Headers added in v1.1.3

type Headers map[string]string

Headers is a map containing the relation key=value of the headers used on the http rest request.

type Option

type Option func(*client)

Option is a type to set HTTP Client options.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout instructs the HTTP Client to cancel any requests that exceeds the given timeout.

type PathParams added in v1.1.3

type PathParams map[string]string

PathParams is a map containing the relation key=value of the path params used on the http rest request. It will be used to replace values given in Path parameter.

type QueryParams added in v1.1.3

type QueryParams map[string]string

QueryParams is a map containing the relation key=value of the query params used on the http rest request.

type Request added in v1.1.3

type Request struct {
	Host        string
	Path        string
	Body        []byte
	Headers     Headers
	QueryParams QueryParams
	PathParams  PathParams
	// contains filtered or unexported fields
}

Request are the params used to build a new http rest request.

type RequestOption added in v1.2.0

type RequestOption func(*Request)

RequestOption is a type to set HTTP Request options.

func WithAcceptStatusCode added in v1.2.0

func WithAcceptStatusCode(code int) RequestOption

WithAcceptStatusCode indicates that the current request accepts the given status code. It is possible to use this option multiple times for different status codes in the same request.

type Response added in v1.1.3

type Response struct {
	StatusCode int
	Body       []byte
}

Response encapsulates data returned from the client HTTP request.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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