middleware

package
v0.0.0-...-723ec97 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModeDefault = iota
	ModeCacheOnly
)

Variables

View Source
var Non2XXStatusCodes = StatusCodeRange{Low: 300, High: 599}

Non2XXStatusCodes is the status code range representing non 2XX status codes

Functions

func BearerToken

func BearerToken(token string) api.Middleware

func Cache

func Cache(mode Mode, ttl time.Duration, store GetSetter) api.Middleware

Header will set the header name and value

func ErrorOnStatusCodes

func ErrorOnStatusCodes(statusCodes ...StatusCodeRange) api.Middleware

ErrorOnStatusCodes will return an error on certain status codes

func Header(name, value string) api.Middleware

Header will set the header name and value

func InRanges

func InRanges(code int, ranges []StatusCodeRange) bool

InRanges checks to see if the status code is in the ranges

func JSONHeaders

func JSONHeaders(next api.Dofn) api.Dofn

JSONPayload is a Do func middleware that sets the JSON payload headers

func MockResponse

func MockResponse(mock func(req *http.Request) (statusCode int, respBody string)) api.Dofn

MockResponse mocks a response object and can be used with NewMock.

func NewMock

func NewMock(mock api.Dofn) api.Middleware

NewMock mocks the Do func and doesn't call in to the next Dofn in line.

func NewMockResponse

func NewMockResponse(mock func(req *http.Request) (statusCode int, respBody string)) api.Middleware

NewMockResponse is a convenience func that combines NewMock and MockResponse

func RetryOnStatusCodes

func RetryOnStatusCodes(retry uint, statusCodes ...StatusCodeRange) api.Middleware

RetryOnStatusCodes is a Do func middleware that will retry based on status codes

func RetryWithDelay

func RetryWithDelay(retry uint, delayMin, delayMax time.Duration, delayRamp float32, ranges ...StatusCodeRange) api.Middleware

RetryWithDelay is a Do func middleware that will retry based on status codes or on err. It also can take a delay min, max, and backoff multiplier. If no range is passed, it defaults to Non2XXStatusCodes range

Types

type ErrMaxRetries

type ErrMaxRetries struct {
	Err error // the wrapped error
}

ErrMaxRetries is the error that represents when the max number of retries has been reached

func (ErrMaxRetries) Error

func (e ErrMaxRetries) Error() string

Error implements the error interface

func (ErrMaxRetries) Unwrap

func (e ErrMaxRetries) Unwrap() error

type ErrStatusCode

type ErrStatusCode struct {
	Status string
	Code   int
}

ErrStatusCode This is used when you want to handle a certain status code as an error.

func (ErrStatusCode) Error

func (esc ErrStatusCode) Error() string

type GetSetter

type GetSetter interface {
	Get(key string) (io.ReadCloser, error)
	Set(key string, ttl time.Duration, body io.ReadCloser) error
}

type Mode

type Mode int

type StatusCodeRange

type StatusCodeRange struct {
	Low  int
	High int
}

StatusCodeRange is a range of status codes

func (StatusCodeRange) InRange

func (scr StatusCodeRange) InRange(code int) bool

InRange checks a status code to see if it is in the range

Jump to

Keyboard shortcuts

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