http

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTestHTTPClient

func NewTestHTTPClient(fn RoundTripFunc) *http.Client

NewTestHTTPClient returns a new http.Client that uses the given function as its RoundTripper. This allows for easy mocking of an HTTP client for test purposes. Usually, this function is used in conjunction with the WithHTTPClient function to configure a Client with a mock HTTP client.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}
var DefaultClient *Client

func NewClient

func NewClient(options ...ClientOption) (*Client, error)

func (*Client) Query

func (c *Client) Query(url string) (*http.Response, error)

Query executes a GET request against the given url and returns the response and any errors associated with it.

The HTTP Headers defined under the [clientOptions] struct are added to the request. If the User-Agent header is not set, it is set to the value of the UserAgent field in the [clientOptions] struct.

The response body is limited to the value of the ReaderLimit field in the [clientOptions] struct and an error is returned if the limit is exceeded. Additionally, the response body will be read by this function to facilitate logging, yet returned to the caller as a ReadCloser to be handled like any other response body.

type ClientOption

type ClientOption interface {
	// contains filtered or unexported methods
}

func WithHTTPClient

func WithHTTPClient(c *http.Client) ClientOption

func WithHeaders

func WithHeaders(headers http.Header) ClientOption

func WithLogger

func WithLogger(logger *slog.Logger) ClientOption

func WithReaderLimit

func WithReaderLimit(limit int64) ClientOption

func WithUserAgent

func WithUserAgent(ua string) ClientOption

type RoundTripFunc

type RoundTripFunc func(req *http.Request) (*http.Response, error)

func (RoundTripFunc) RoundTrip

func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

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