requests2

package
v3.1.12+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertStatusCode

func AssertStatusCode(code int, wantStart, wantEnd int) error

Types

type Request

type Request struct {
	Method string
	URL    string
	// Query defines additional query parameters
	// If url already has some and Query is defined, they will be added together
	Query  url.Values
	Header http.Header

	// Body is sent to the server.
	Body []byte

	// BodyReader is sent to the server.
	// Use for large files.
	// Need to be careful for concurrent use.
	// When Request is called seeks to beginning.
	BodyReader io.ReadSeeker

	BasicAuthUser     string
	BasicAuthPassword string
}

func NewRequest

func NewRequest() Request

func (Request) Request

func (s Request) Request() (*http.Request, error)

type Requests

type Requests struct {
	Logger    hclog.Logger
	Client    *http.Client
	Retryable RetryableRequest
}

func New

func New(logger hclog.Logger, client *http.Client) Requests

func NewRetryableDefault

func NewRetryableDefault(logger hclog.Logger, client *http.Client) Requests

func (Requests) Do

func (opts Requests) Do(ctx context.Context, req Request) (res Result, rerr error)

Do makes an http request. It preserves both request and response body for logging purposes. Returns logError function that logs the passed error together with request and response body for easier debugging.

func (Requests) JSON

func (opts Requests) JSON(
	req Request,
	res interface{}) (resp Result, rerr error)

JSON makes http request and unmarshals resulting json. Returns errors on StatusCode != 200. Logs request and response body on errors.

type Result

type Result struct {
	Resp         *http.Response
	ErrorContext func(error) error
}

type RetryableRequest

type RetryableRequest struct {
	MaxAttempts float64
	MaxDuration time.Duration
	RetryDelay  time.Duration
}

type StatusCodeError

type StatusCodeError struct {
	WantStart int
	WantEnd   int
	Got       int
}

func (StatusCodeError) Error

func (s StatusCodeError) Error() string

Jump to

Keyboard shortcuts

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