middleware

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package middleware contains the custom middleware used by the go-tfe SDK, as well as options for configuring the default middlewares.

Package middleware contains the custom middleware used by the go-tfe SDK.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetForKiota

func GetForKiota(tfeSDKVersion string, options ...MiddlewareOption) ([]khttp.Middleware, error)

GetForKiota uses the provided options to configure the default middlewares used by kiota as well as the custom middleware supplied by the SDK.

This function replaces Kiota's built-in RetryHandler with a custom RetryMiddleware. Kiota's RetryHandler has a hardcoded isRetriableErrorCode gate that only covers 429, 503, 504 and short-circuits before calling the ShouldRetry callback. Our custom middleware calls ShouldRetry unconditionally, allowing retries on 429, 425, and all 5xx (when RetryServerErrors is enabled).

func NewErrorMiddleware

func NewErrorMiddleware(errFactory APIErrorFactory) khttp.Middleware

NewErrorMiddleware creates a new kiota middleware that uses the provided factory to convert HTTP responses into custom error types when the response status code indicates an error (400 or above).

func NewHeadersMiddleware

func NewHeadersMiddleware(headers http.Header) khttp.Middleware

NewHeadersMiddleware creates a new instance of HeadersMiddleware with the provided headers.

func NewRateLimitMiddleware

func NewRateLimitMiddleware() khttp.Middleware

NewRateLimitMiddleware creates a middleware that translates X-RateLimit-Reset headers into standard Retry-After headers for use by the retry middleware.

func NewRetryMiddleware

func NewRetryMiddleware(opts RetryMiddlewareOptions) khttp.Middleware

NewRetryMiddleware creates a custom retry middleware with the given options.

Types

type APIErrorFactory

type APIErrorFactory func(resp *http.Response, pipelineErr error) error

APIErrorFactory is a function type that takes an HTTP response and a middleware error, and returns a new error. It is used to return custom error types based on the response from the API.

type ErrorMiddleware

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

ErrorMiddleware is a custom middleware that uses an APIErrorFactory to convert HTTP responses into custom error types when the response status code indicates an error (400 or above).

func (*ErrorMiddleware) Intercept

func (m *ErrorMiddleware) Intercept(pipeline khttp.Pipeline, middlewareindex int, req *http.Request) (*http.Response, error)

Intercept implements the khttp.Middleware interface.

type HeadersMiddleware

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

HeadersMiddleware is a custom middleware that adds the configured headers to each request.

func (*HeadersMiddleware) Intercept

func (m *HeadersMiddleware) Intercept(pipeline khttp.Pipeline, middlewareindex int, req *http.Request) (*http.Response, error)

Intercept implements the khttp.Middleware interface.

type MiddlewareOption

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

MiddlewareOption is a functional option for configuring the middleware pipeline.

func WithErrorInterceptorOption

func WithErrorInterceptorOption(errorFactory APIErrorFactory) MiddlewareOption

WithErrorInterceptorOption creates a middleware option that configures error interception.

func WithHeaders

func WithHeaders(headers nethttp.Header) MiddlewareOption

WithHeaders creates a middleware option that adds the provided headers to each request.

func WithRetryOptions

func WithRetryOptions(enabled, enabledForServerErrors bool, maxRetries int, hook RetryHookCallback) MiddlewareOption

WithRetryOptions creates a middleware option that configures retry behavior.

type RateLimitMiddleware

type RateLimitMiddleware struct{}

RateLimitMiddleware translates custom rate limit headers to Retry-After, which is used by the retry middleware for backoff timing.

func (*RateLimitMiddleware) Intercept

func (m *RateLimitMiddleware) Intercept(pipeline khttp.Pipeline, index int, req *http.Request) (*http.Response, error)

Intercept implements the khttp.Middleware interface.

type RetryHookCallback

type RetryHookCallback func(retryCount int, response *nethttp.Response)

RetryHookCallback is called before each retry attempt with the attempt number and response.

type RetryMiddleware

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

RetryMiddleware is a custom retry middleware that replaces Kiota's built-in RetryHandler. Unlike Kiota's implementation, it does NOT gate retries behind a hardcoded isRetriableErrorCode check (which only covers 429, 503, 504). Instead, it delegates the retry decision entirely to the provided ShouldRetry callback, allowing retries on any status code (e.g. 425, 500, 502).

func (*RetryMiddleware) Intercept

func (m *RetryMiddleware) Intercept(pipeline khttp.Pipeline, middlewareIndex int, req *nethttp.Request) (*nethttp.Response, error)

Intercept implements the khttp.Middleware interface.

type RetryMiddlewareOptions

type RetryMiddlewareOptions struct {
	// MaxRetries is the maximum number of retry attempts. Capped at 10.
	MaxRetries int
	// DelaySeconds is the base delay between retries (used for exponential backoff).
	DelaySeconds int
	// ShouldRetry determines whether a given response warrants a retry.
	// This is called unconditionally for every non-nil response — there is no
	// status code pre-filter.
	ShouldRetry ShouldRetryFunc
}

RetryMiddlewareOptions configures the custom retry middleware.

type RetryOptions

type RetryOptions struct {
	Enabled           bool
	MaxRetries        int
	RetryServerErrors bool
	Hook              RetryHookCallback
}

RetryOptions configures the retry behavior for the middleware pipeline.

type ShouldRetryFunc

type ShouldRetryFunc func(executionCount int, request *nethttp.Request, response *nethttp.Response) bool

ShouldRetryFunc determines whether a request should be retried based on the response.

Jump to

Keyboard shortcuts

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