rest

package
v2.0.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsClientError

func IsClientError(response *resty.Response) bool

func IsNotFound

func IsNotFound(response *resty.Response) bool

func IsServerError

func IsServerError(response *resty.Response) bool

func IsUnauthorized

func IsUnauthorized(response *resty.Response) bool

Types

type Client

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

func NewClient

func NewClient(options ...ClientOption) *Client

func (*Client) AddMiddleware

func (c *Client) AddMiddleware(middleware Middleware)

func (*Client) GetMiddlewares

func (c *Client) GetMiddlewares() []Middleware

func (*Client) GetRestClient

func (c *Client) GetRestClient() *resty.Client

func (*Client) GetRestConfig

func (c *Client) GetRestConfig() *Config

func (*Client) HandleResponse

func (c *Client) HandleResponse(response *resty.Response) error

func (*Client) MakeRequest

func (c *Client) MakeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) (*resty.Response, error)

func (*Client) MakeRequestWithTrace

func (c *Client) MakeRequestWithTrace(ctx context.Context, method string, url string, body string, headers map[string]string) (*resty.Response, error)

func (*Client) SetMiddlewares

func (c *Client) SetMiddlewares(middlewares ...Middleware)

type ClientOption

type ClientOption func(*Client)

func WithMiddleware

func WithMiddleware(middleware Middleware) ClientOption

func WithMiddlewares

func WithMiddlewares(middlewares ...Middleware) ClientOption

func WithOTelConfig

func WithOTelConfig(cfg *otel.Config) ClientOption

WithOTelConfig sets the OpenTelemetry configuration for the REST client When set, adds OTel tracing, metrics, and logging middleware automatically

func WithRestConfig

func WithRestConfig(restConfig Config) ClientOption

type Config

type Config struct {
	RetryCount       int           `yaml:"retryCount" mapstructure:"retryCount"`
	RetryWaitTime    time.Duration `yaml:"retryWaitTime" mapstructure:"retryWaitTime"`
	RetryMaxWaitTime time.Duration `yaml:"retryMaxWaitTime" mapstructure:"retryMaxWaitTime"`
	Timeout          time.Duration `yaml:"timeout" mapstructure:"timeout"`

	// OpenTelemetry Configuration (optional - nil disables telemetry)
	OTelConfig *otel.Config `yaml:"-" mapstructure:"-"` // Not serializable from config files
}

Config RestConfig contains configuration for REST client

func DefaultRestConfig

func DefaultRestConfig() *Config

DefaultRestConfig returns a default REST configuration

type DatabaseLoggingMiddleware

type DatabaseLoggingMiddleware struct{}

DatabaseLoggingMiddleware logs HTTP requests and responses to a database (placeholder implementation)

func NewDatabaseLoggingMiddleware

func NewDatabaseLoggingMiddleware() *DatabaseLoggingMiddleware

NewDatabaseLoggingMiddleware creates a new DatabaseLoggingMiddleware instance

func (*DatabaseLoggingMiddleware) AfterRequest

func (m *DatabaseLoggingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest logs the request to database (placeholder - just logs to console for now)

func (*DatabaseLoggingMiddleware) BeforeRequest

func (m *DatabaseLoggingMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest stores the start time in context for later use

type ExecutionError

type ExecutionError struct {
	Msg string
	Err error
}

ExecutionError represents an error during execution

func NewExecutionError

func NewExecutionError(msg string, err error) *ExecutionError

func (*ExecutionError) Error

func (e *ExecutionError) Error() string

func (*ExecutionError) Unwrap

func (e *ExecutionError) Unwrap() error

type LoggingMiddleware

type LoggingMiddleware struct{}

LoggingMiddleware logs HTTP requests and responses

func NewLoggingMiddleware

func NewLoggingMiddleware() *LoggingMiddleware

NewLoggingMiddleware creates a new LoggingMiddleware instance

func (*LoggingMiddleware) AfterRequest

func (m *LoggingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest logs the completion of the request with timing information

func (*LoggingMiddleware) BeforeRequest

func (m *LoggingMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest logs the start of the request and stores the start time in context

type Middleware

type Middleware interface {
	BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context
	AfterRequest(ctx context.Context, info RequestInfo)
}

type NoOpMiddleware

type NoOpMiddleware struct{}

NoOpMiddleware is a middleware that does nothing - useful for testing and as a placeholder

func NewNoOpMiddleware

func NewNoOpMiddleware() *NoOpMiddleware

NewNoOpMiddleware creates a new NoOpMiddleware instance

func (*NoOpMiddleware) AfterRequest

func (m *NoOpMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest does nothing

func (*NoOpMiddleware) BeforeRequest

func (m *NoOpMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest does nothing and returns the context unchanged

type OTelLoggingMiddleware

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

OTelLoggingMiddleware implements structured logging with trace correlation for HTTP client

func NewOTelLoggingMiddleware

func NewOTelLoggingMiddleware(cfg *pkgotel.Config) *OTelLoggingMiddleware

NewOTelLoggingMiddleware creates a new OpenTelemetry logging middleware

func (*OTelLoggingMiddleware) AfterRequest

func (m *OTelLoggingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest logs the completion of the request with trace correlation

func (*OTelLoggingMiddleware) BeforeRequest

func (m *OTelLoggingMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest logs the start of the request

type OTelMetricsMiddleware

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

OTelMetricsMiddleware implements metrics collection for HTTP client requests

func NewOTelMetricsMiddleware

func NewOTelMetricsMiddleware(cfg *pkgotel.Config) *OTelMetricsMiddleware

NewOTelMetricsMiddleware creates a new OpenTelemetry metrics middleware

func (*OTelMetricsMiddleware) AfterRequest

func (m *OTelMetricsMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest records response metrics

func (*OTelMetricsMiddleware) BeforeRequest

func (m *OTelMetricsMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest records request size metrics

func (*OTelMetricsMiddleware) RecordRetry

func (m *OTelMetricsMiddleware) RecordRetry(ctx context.Context, method string, attempt int)

RecordRetry records a retry attempt (to be called by retry logic)

type OTelTracingMiddleware

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

OTelTracingMiddleware implements distributed tracing for HTTP client requests

func NewOTelTracingMiddleware

func NewOTelTracingMiddleware(cfg *pkgotel.Config) *OTelTracingMiddleware

NewOTelTracingMiddleware creates a new OpenTelemetry tracing middleware

func (*OTelTracingMiddleware) AfterRequest

func (m *OTelTracingMiddleware) AfterRequest(ctx context.Context, info RequestInfo)

AfterRequest ends the span and records the response status

func (*OTelTracingMiddleware) BeforeRequest

func (m *OTelTracingMiddleware) BeforeRequest(ctx context.Context, method string, url string, body string, headers map[string]string) context.Context

BeforeRequest starts a new span for the HTTP request and injects trace context into headers

type RequestInfo

type RequestInfo struct {
	Method     string
	URL        string
	Headers    map[string]string
	Body       string
	StartTime  time.Time
	EndTime    time.Time
	Duration   time.Duration
	StatusCode int
	Response   string
	Error      error
	TraceInfo  resty.TraceInfo
}

type ResourceNotFoundError

type ResourceNotFoundError struct {
	StatusCode int
	Msg        string
	RespBody   string
}

func NewResourceNotFoundError

func NewResourceNotFoundError(statusCode int, msg string, respBody string) *ResourceNotFoundError

func (*ResourceNotFoundError) Error

func (e *ResourceNotFoundError) Error() string

type ResponseError

type ResponseError struct {
	StatusCode int
	Msg        string
	RespBody   string
}

func NewResponseError

func NewResponseError(statusCode int, msg string, respBody string) *ResponseError

func (*ResponseError) Error

func (e *ResponseError) Error() string

type ServerError

type ServerError struct {
	StatusCode int
	Msg        string
	RespBody   string
}

func NewServerError

func NewServerError(statusCode int, msg string, respBody string) *ServerError

NewServerError creates a new ServerError

func (*ServerError) Error

func (e *ServerError) Error() string

type UnauthorizedError

type UnauthorizedError struct {
	StatusCode int
	Msg        string
	RespBody   string
}

UnauthorizedError represents an unauthorized error (HTTP 401)

func NewUnauthorizedError

func NewUnauthorizedError(statusCode int, msg string, respBody string) *UnauthorizedError

NewUnauthorizedError creates a new UnauthorizedError

func (*UnauthorizedError) Error

func (e *UnauthorizedError) Error() string

Jump to

Keyboard shortcuts

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