http

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: Apache-2.0 Imports: 16 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a type that represents an HTTP client

func NewClient

func NewClient(config *Config) *Client

NewClient configures a new HTTP client using given configuration

func (*Client) Delete

func (c *Client) Delete(url string) (*Response, error)

Delete sends an HTTP DELETE request

func (*Client) Get

func (c *Client) Get(url string) (*Response, error)

Get sends an HTTP GET request

func (*Client) Patch

func (c *Client) Patch(url string, body io.ReadCloser) (*Response, error)

Patch sends an HTTP PATCH request

func (*Client) Post

func (c *Client) Post(url string, contentType string, body io.ReadCloser) (*Response, error)

Post sends an HTTP POST request

func (*Client) Put

func (c *Client) Put(url string, body io.ReadCloser) (*Response, error)

Put sends an HTTP PUT request

type Config

type Config struct {
	// TLS settings
	InsecureSkipVerify bool

	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

	ProxyHost string
	ProxyPort uint16

	// Headers are automatically added to all requests
	Headers map[string]string

	// BaseURL is added as a prefix to all URLs
	BaseURL string

	// Specify if request/response bodies should be logged
	TraceBody     bool
	TraceResponse bool
	Trace         bool
	// GET's are on TRACE, PUT/PATCH/POST are on Debug, and DELETE are on Info
	Logger logger.Logger

	// RETRY HANDLING
	// Cancel the request after this timeout
	Timeout time.Duration
	// Number of retries to attempt
	Retries uint
	// Time to wait between retries
	RetryWait time.Duration
	// Amount to increase RetryWait with each failure, 2.0 is a good option for exponential backoff
	Factor float64
}

Config holds all configuration for the HTTP client

type ContentType

type ContentType struct {
	// contains filtered or unexported fields
}

type DeleteRequest

type DeleteRequest struct {
	*Request
}

func NewDeleteRequest

func NewDeleteRequest(config *Config, url string) *DeleteRequest

NewDeleteRequest creates a new HTTP DELETE request

func (*DeleteRequest) Send

func (r *DeleteRequest) Send(client *http.Client, logger logger.Logger) (*Response, error)

Send the HTTP DELETE request

type GetRequest

type GetRequest struct {
	*Request
}

func NewGetRequest

func NewGetRequest(config *Config, url string) *GetRequest

NewGetRequest creates a new HTTP GET request

func (*GetRequest) Send

func (r *GetRequest) Send(client *http.Client, logger logger.Logger) (*Response, error)

Send the HTTP GET request

type PatchRequest

type PatchRequest struct {
	*Request
}

func NewPatchRequest

func NewPatchRequest(config *Config, url string, body io.ReadCloser) *PatchRequest

NewPatchRequest creates a new HTTP PATCH request

func (*PatchRequest) Send

func (r *PatchRequest) Send(client *http.Client, logger logger.Logger) (*Response, error)

Send the HTTP PATCH request

type PostRequest

type PostRequest struct {
	*Request
	*ContentType
}

func NewPostRequest

func NewPostRequest(config *Config, url string, contentType string, body io.ReadCloser) *PostRequest

NewPostRequest creates a new HTTP POST request

func (*PostRequest) Send

func (r *PostRequest) Send(client *http.Client, logger logger.Logger) (*Response, error)

Send the HTTP POST request

type PutRequest

type PutRequest struct {
	*Request
}

func NewPutRequest

func NewPutRequest(config *Config, url string, body io.ReadCloser) *PutRequest

NewPutRequest creates a new HTTP PUT request

func (*PutRequest) Send

func (r *PutRequest) Send(client *http.Client, logger logger.Logger) (*Response, error)

Send the HTTP PUT request

type Request

type Request struct {
	// contains filtered or unexported fields
}

func (*Request) GetLoggableStrings

func (r *Request) GetLoggableStrings() (string, error)

GetLoggableStrings returns the Headers and Body of the response as strings that can be logged while maintaining the request body's readability

type RequestLoggableStrings

type RequestLoggableStrings struct {
	Headers string
	Body    string
}

type Requester

type Requester interface {
	Send(client *http.Client, logger logger.Logger) (*Response, error)
}

type Response

type Response struct {
	*http.Response
}

Response embeds the stdlib http.Response type and extends its functionality

func (*Response) AsBytes

func (resp *Response) AsBytes() ([]byte, error)

AsBytes returns the body of the response as a byte slice, or returns an error if this is not possible

func (*Response) AsError

func (resp *Response) AsError() error

AsError returns an error with details of the response

func (*Response) AsReader

func (resp *Response) AsReader() (io.Reader, error)

AsReader returns the response body as an io.Reader, or returns an error if this is not possible

func (*Response) AsString

func (resp *Response) AsString() (string, error)

AsString returns the body of the response as a string, or returns an error if this is not possible

func (*Response) IsOK

func (resp *Response) IsOK() bool

IsOK is a convenience method to determine if the response returned a 200 OK

func (*Response) TraceMessage

func (resp *Response) TraceMessage() (string, error)

TraceMessage returns the Headers, StatusCode and Body of the response as strings that can be logged while maintaining the response body's readability

Jump to

Keyboard shortcuts

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