request

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeout      = 30 * time.Second
	DefaultMaxBodyBytes = 10 * 1024 * 1024
)

Variables

View Source
var (
	ErrNilContext        = errors.New("request: nil context")
	ErrNilHTTPClient     = errors.New("request: nil http client")
	ErrNilTransport      = errors.New("request: nil transport")
	ErrNilBody           = errors.New("request: nil body")
	ErrBodyAlreadySet    = errors.New("request: body already set")
	ErrBodyNotReplayable = errors.New("request: body is not replayable")
	ErrBodyTooLarge      = errors.New("request: response body too large")
	ErrNilMultipartPart  = errors.New("request: multipart part has nil reader")
)

Functions

This section is empty.

Types

type Client

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

func DefaultClient

func DefaultClient() *Client

func NewClient

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

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, target string, opts ...RequestOption) (*Response, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Head

func (c *Client) Head(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Options

func (c *Client) Options(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Patch

func (c *Client) Patch(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Put

func (c *Client) Put(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, method, target string, opts ...RequestOption) (*Stream, error)

type ClientOption

type ClientOption func(*clientConfig) error

func WithAfterHook

func WithAfterHook(fn func(context.Context, *Response, error) error) ClientOption

func WithBaseURL

func WithBaseURL(rawURL string) ClientOption

func WithBeforeHook

func WithBeforeHook(fn func(context.Context, *http.Request) error) ClientOption

func WithDefaultHeader

func WithDefaultHeader(name, value string) ClientOption

func WithDefaultHeaders

func WithDefaultHeaders(headers http.Header) ClientOption

func WithDefaultQuery

func WithDefaultQuery(name, value string) ClientOption

func WithHTTPClient

func WithHTTPClient(client *http.Client) ClientOption

func WithHook

func WithHook(hook Hook) ClientOption

func WithMaxBodyBytes

func WithMaxBodyBytes(n int64) ClientOption

func WithProxy

func WithProxy(proxyURL string) ClientOption

func WithRetry

func WithRetry(cfg RetryConfig) ClientOption

func WithStatusValidator

func WithStatusValidator(fn StatusValidator) ClientOption

func WithTLSConfig

func WithTLSConfig(tlsConfig *tls.Config) ClientOption

func WithTimeout

func WithTimeout(timeout time.Duration) ClientOption

func WithTransport

func WithTransport(transport http.RoundTripper) ClientOption

type DecodeError

type DecodeError struct {
	Err  error
	Body []byte
}

func (*DecodeError) Error

func (e *DecodeError) Error() string

func (*DecodeError) Unwrap

func (e *DecodeError) Unwrap() error

type Hook

type Hook interface {
	BeforeRequest(ctx context.Context, req *http.Request) error
	AfterResponse(ctx context.Context, resp *Response, err error) error
}

type HookFunc

type HookFunc struct {
	Before func(ctx context.Context, req *http.Request) error
	After  func(ctx context.Context, resp *Response, err error) error
}

func (HookFunc) AfterResponse

func (h HookFunc) AfterResponse(ctx context.Context, resp *Response, err error) error

func (HookFunc) BeforeRequest

func (h HookFunc) BeforeRequest(ctx context.Context, req *http.Request) error

type MultipartPart

type MultipartPart struct {
	FieldName   string
	FileName    string
	ContentType string
	Reader      io.Reader
	Headers     textproto.MIMEHeader
}

func FieldPart

func FieldPart(name, value string) MultipartPart

func FilePart

func FilePart(fieldName, fileName, contentType string, r io.Reader) MultipartPart

type RequestError

type RequestError struct {
	Method string
	URL    string
	Err    error
}

func (*RequestError) Error

func (e *RequestError) Error() string

func (*RequestError) Unwrap

func (e *RequestError) Unwrap() error

type RequestOption

type RequestOption func(*requestConfig) error

func WithBasicAuth

func WithBasicAuth(username, password string) RequestOption

func WithBearerToken

func WithBearerToken(token string) RequestOption

func WithBody

func WithBody(contentType string, body io.Reader) RequestOption

func WithBytes

func WithBytes(contentType string, body []byte) RequestOption

func WithDecodeJSON

func WithDecodeJSON(v any) RequestOption

func WithExpectedStatus

func WithExpectedStatus(codes ...int) RequestOption

func WithForm

func WithForm(values url.Values) RequestOption

func WithFormMap

func WithFormMap(values map[string]string) RequestOption

func WithHeader

func WithHeader(name, value string) RequestOption

func WithHeaders

func WithHeaders(headers http.Header) RequestOption

func WithJSON

func WithJSON(v any) RequestOption

func WithMultipart

func WithMultipart(parts ...MultipartPart) RequestOption

func WithQuery

func WithQuery(name, value string) RequestOption

func WithQueryValues

func WithQueryValues(values url.Values) RequestOption

func WithRequestHook

func WithRequestHook(hook Hook) RequestOption

func WithRequestMaxBodyBytes

func WithRequestMaxBodyBytes(n int64) RequestOption

func WithRequestRetry

func WithRequestRetry(cfg RetryConfig) RequestOption

func WithRequestStatusValidator

func WithRequestStatusValidator(fn StatusValidator) RequestOption

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) RequestOption

func WithString

func WithString(contentType, body string) RequestOption

type Response

type Response struct {
	Request    *http.Request
	Raw        *http.Response
	StatusCode int
	Status     string
	Header     http.Header
	Body       []byte
}

func Delete

func Delete(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func Do

func Do(ctx context.Context, method, target string, opts ...RequestOption) (*Response, error)

func Get

func Get(ctx context.Context, target string, opts ...RequestOption) (*Response, error)
func Head(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func Options

func Options(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func Patch

func Patch(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func Post

func Post(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func Put

func Put(ctx context.Context, target string, opts ...RequestOption) (*Response, error)

func (*Response) Bytes

func (r *Response) Bytes() []byte

func (*Response) DecodeJSON

func (r *Response) DecodeJSON(v any) error

func (*Response) String

func (r *Response) String() string

type RetryConfig

type RetryConfig struct {
	MaxAttempts int
	WaitMin     time.Duration
	WaitMax     time.Duration
	Methods     []string
	StatusCodes []int
	RetryErrors bool
	ShouldRetry func(ctx context.Context, attempt int, resp *Response, err error) bool
}

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

type StatusError

type StatusError struct {
	Response *Response
}

func (*StatusError) Body

func (e *StatusError) Body() []byte

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) Header

func (e *StatusError) Header() http.Header

func (*StatusError) StatusCode

func (e *StatusError) StatusCode() int

type StatusValidator

type StatusValidator func(statusCode int) bool

type Stream

type Stream struct {
	Request  *http.Request
	Response *http.Response
}

func StreamRequest

func StreamRequest(ctx context.Context, method, target string, opts ...RequestOption) (*Stream, error)

func (*Stream) Close

func (s *Stream) Close() error

Jump to

Keyboard shortcuts

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