request

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRetryConfig = RetryConfig{
		InitialDelay:   1 * time.Second,
		Multiplier:     1,
		MaxDelay:       10 * time.Second,
		Jitter:         0,
		MaxRetries:     1,
		MaxElapsedTime: 5 * time.Second,
	}

	DefaultRetryer = retryer{}
)
View Source
var (
	// NoBody is an http.NoBody reader instructing the Go HTTP client to not include
	// a body in the HTTP request.
	NoBody = http.NoBody
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Endpoint is hostname or fully qualified URI of the service being called
	Endpoint string

	// Name of the external service being called
	ServiceName string

	// Set this to `true` to disable SSL when sending requests. Defaults
	// to `false`
	DisableSSL bool

	// The HTTP client to use when sending requests
	HTTPClient *http.Client

	DisableFollowRedirects bool

	LogLevel LogLevel
	// The logger writer interface to write logging messages to. Defaults to
	// standard out.
	Logger Logger

	// Unique ID to trace a request attempt
	RequestID string
}

type Hook

type Hook struct {
	Name string
	Fn   func(*Request)
}

type HookList

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

func (*HookList) Clear

func (l *HookList) Clear()

Clear clears the hook list

func (*HookList) Debug

func (l *HookList) Debug() string

func (*HookList) Len

func (l *HookList) Len() int

Len returns the number of hooks in the list

func (*HookList) PushBack

func (l *HookList) PushBack(f func(*Request))

PushBack pushes hook f to the back of the hook list.

func (*HookList) PushBackHook

func (l *HookList) PushBackHook(h Hook)

PushBackHook pushes hook h to the back of the hook list.

func (*HookList) PushFront

func (l *HookList) PushFront(f func(*Request))

func (*HookList) PushFrontHook

func (l *HookList) PushFrontHook(h Hook)

PushFrontHook pushes hook h to the front of the hook list

func (*HookList) Remove

func (l *HookList) Remove(name string)

Remove removes a Hook by name

func (*HookList) RemoveHook

func (l *HookList) RemoveHook(h Hook)

RemoveHook removes Hook h

func (*HookList) Run

func (l *HookList) Run(r *Request)

Run executes all handlers in the list with a given request object

func (*HookList) Swap

func (l *HookList) Swap(name string, replace Hook)

type Hooks

type Hooks struct {
	Validate  HookList
	Build     HookList
	Send      HookList
	Unmarshal HookList
	Retry     HookList
	Complete  HookList
}

func (*Hooks) Copy

func (h *Hooks) Copy() Hooks

Copy returns a copy of these hooks' lists

func (*Hooks) Debug

func (h *Hooks) Debug() map[string]string

func (*Hooks) IsEmpty

func (h *Hooks) IsEmpty() bool

IsEmpty returns if there are no hooks in any of the hook lists.

type LogLevel

type LogLevel uint
const (
	// LogSilent state used to disable all logging. This is the default state
	LogSilent LogLevel = iota * 0x1000

	// LogError state used to log when service requests fail
	// to build, send, validate, or unmarshal.
	LogError

	// LogDebug state can be used for debug output to inspect requests
	// made and responses received.
	LogDebug
)
const (
	// LogDebugWithHTTPBody state used to log HTTP request and response HTTP bodies
	// in addition to the headers and path. This should be used to see the body
	// content of requests and responses made. Will also enable LogDebug.
	LogDebugWithHTTPBody LogLevel = LogDebug | (1 << iota)

	// LogDebugWithRequestRetries state used to log when service requests will
	// be retried. This should be used to log when you want to log when service
	// requests are being retried. Will also enable LogDebug.
	LogDebugWithRequestRetries
)

Debug Logging Sub Levels

func (LogLevel) AtLeast

func (l LogLevel) AtLeast(v LogLevel) bool

AtLeast returns true if this LogLevel is at least high enough to satisfy v.

func (LogLevel) Equals

func (l LogLevel) Equals(v LogLevel) bool

type Logger

type Logger interface {
	Log(...any)
}

func NewDefaultLogger

func NewDefaultLogger() Logger

NewDefaultLogger returns a Logger which will write log messages to stdout, and use same formatting runes as the stdlib log.Logger

type LoggerFunc

type LoggerFunc func(...any)

A LoggerFunc is a convenience type to convert a function taking a variadic list of arguments and wrap it so the Logger interface can be used.

func (LoggerFunc) Log

func (f LoggerFunc) Log(args ...any)

type Operation

type Operation struct {
	Name   string
	Method string
	Path   string
}

type Option

type Option func(*Request)

An Option is a functional option that can augment or modify a request when using a WithContext API operation method.

func WithLogLevel

func WithLogLevel(l LogLevel) Option

WithLogLevel sets log level

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets the logger func used with the request

func WithRequestHeader

func WithRequestHeader(key, val string) Option

WithRequestHeader builds a request Option which will add an http header to the request.

func WithRequestID

func WithRequestID(id string) Option

WithRequestID sets the request id in config

func WithServiceName

func WithServiceName(name string) Option

WithServiceName sets the service name in config

type Request

type Request struct {
	Config Config

	// request payload
	Params any

	// response body
	Body any
	Data any

	Hooks Hooks

	Error error

	Retryer
	RetryConfig RetryConfig

	Operation *Operation
	Request   *http.Request
	Response  *http.Response

	AttemptTime time.Time
	// contains filtered or unexported fields
}

func New

func New(cfg Config, hooks Hooks, retryer Retryer, operation *Operation, params, data any) *Request

New returns a new Request pointer for the api operation and parameters.

Params is any value for the request payload.

Data is for the response payload

func (*Request) ApplyOptions

func (r *Request) ApplyOptions(opts ...Option)

ApplyOptions will apply each option to the request calling them in the order they were provided.

func (*Request) Build

func (r *Request) Build() error

Build will build the request object to be sent. Build will also validate all the request's parameters.

If any Validate or Build errors occur, the build will stop and the error which occurred will be returned

func (*Request) Context

func (r *Request) Context() context.Context

Context will always return a non-nil context. If the Request does not have a context, context.Background will be returned.

func (*Request) Send

func (r *Request) Send() error

func (*Request) WithContext

func (r *Request) WithContext(ctx context.Context)

func (*Request) WithRetryConfig

func (r *Request) WithRetryConfig(cfg RetryConfig)

type RetryConfig

type RetryConfig struct {
	// InitialDelay before the first retry.
	InitialDelay time.Duration
	// Multiplier to apply to the delay interval between retries.
	// Values >1 increase the delay interval exponentially, values <1 decrease it.
	Multiplier float64
	// MaximumDelay n at which to cap the delay interval between retries.
	// As the delay interval increases, the highest interval will be capped
	// at this value.
	MaxDelay time.Duration
	// Jitter is the amount of random jitter to apply to the delay interval
	// between retries to prevent exact timing between retries.
	// Valid values are between 0 and 1. A value of 0 disables Jitter.
	Jitter float64
	// CurrentDelay tracks the current delay interval between retries.
	CurrentDelay time.Duration

	// Number of retries attempted. Value should be incremented with each retry
	RetryCount int
	// Number of maximum allowed retries
	MaxRetries int
	// Maximum allowed time for retries
	MaxElapsedTime time.Duration

	// Additional API error codes that should be retried. IsErrorRetryable
	// will consider these codes in addition to its built-in cases.
	RetryErrorCodes []string
}

type Retryer

type Retryer interface {
	// Delay returns the duration to wait before making another attempt for the
	// failed request.
	Delay(*Request) time.Duration

	// Retryable returns true if the request should be retried
	Retryable(*Request) bool
}

Jump to

Keyboard shortcuts

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