gorestly

package module
v0.0.0-...-ebda180 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 32 Imported by: 0

README

gorestly

gorestly is a wrapper of https://github.com/go-resty/resty HTTP client with reusable configurations.

Documentation

Overview

Package gorestly is a wrapper of https://github.com/go-resty/resty HTTP client with reusable configurations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClientFromConfig

func NewClientFromConfig(config RestyConfig, options ...Option) (*resty.Client, error)

NewClientFromConfig creates a resty client with configuration.

func ParseHostNameAndPortFromURL

func ParseHostNameAndPortFromURL(endpoint *url.URL) (string, int, error)

ParseHostNameAndPortFromURL parses the host and port from a URL.

Types

type CustomAttributesFunc

type CustomAttributesFunc func(*resty.Response) []attribute.KeyValue

CustomAttributesFunc abstracts a function to add custom attributes to spans and metrics.

type HTTPTransportConfig

type HTTPTransportConfig struct {
	// Options the http.Dialer to connect to an address
	Dialer *RestyDialerConfig `json:"dialer,omitempty" yaml:"dialer"`
	// Idle connection timeout. The maximum amount of time an idle (keep-alive) connection will remain idle before closing itself. Zero means no limit.
	IdleConnTimeout *model.Duration `json:"idleConnTimeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"idleConnTimeout"`
	// Response header timeout, if non-zero, specifies the amount of time to wait for a server's response headers after fully writing the request (including its body, if any).
	// This time does not include the time to read the response body.
	// This timeout is used to cover cases where the tcp connection works but the server never answers.
	ResponseHeaderTimeout *model.Duration `json:"responseHeaderTimeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"responseHeaderTimeout"`
	// TLS handshake timeout is the maximum amount of time to wait for a TLS handshake. Zero means no timeout.
	TLSHandshakeTimeout *model.Duration `json:"tlsHandshakeTimeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"tlsHandshakeTimeout"`
	// Expect continue timeout, if non-zero, specifies the amount of time to wait for a server's first response headers after fully writing the request headers if the request has an "Expect: 100-continue" header.
	ExpectContinueTimeout *model.Duration `json:"expectContinueTimeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"expectContinueTimeout"`
	// MaxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts. Zero means no limit.
	MaxIdleConns *int `json:"maxIdleConns,omitempty" jsonschema:"nullable,min=0" yaml:"maxIdleConns"`
	// MaxIdleConnsPerHost, if non-zero, controls the maximum idle (keep-alive) connections to keep per-host.
	MaxIdleConnsPerHost *int `json:"maxIdleConnsPerHost,omitempty" jsonschema:"nullable,min=0" yaml:"maxIdleConnsPerHost"`
	// MaxConnsPerHost optionally limits the total number of connections per host, including connections in the dialing, active, and idle states.
	// On limit violation, dials will block. Zero means no limit.
	MaxConnsPerHost *int `json:"maxConnsPerHost,omitempty" jsonschema:"nullable,min=0" yaml:"maxConnsPerHost"`
	// MaxResponseHeaderBytes specifies a limit on how many response bytes are allowed in the server's response header.
	// Zero means to use a default limit.
	MaxResponseHeaderBytes *int64 `json:"maxResponseHeaderBytes,omitempty" jsonschema:"nullable,min=0" yaml:"maxResponseHeaderBytes"`
	// ReadBufferSize specifies the size of the read buffer used when reading from the transport.
	// If zero, a default (currently 4KB) is used.
	ReadBufferSize *int `json:"readBufferSize,omitempty" jsonschema:"nullable,min=0" yaml:"readBufferSize"`
	// WriteBufferSize specifies the size of the write buffer used when writing to the transport.
	// If zero, a default (currently 4KB) is used.
	WriteBufferSize *int `json:"writeBufferSize,omitempty" jsonschema:"nullable,min=0" yaml:"writeBufferSize"`
	// DisableKeepAlives, if true, disables HTTP keep-alives and will only use the connection to the server for a single HTTP request.
	// This is unrelated to the similarly named TCP keep-alives.
	DisableKeepAlives bool `json:"disableKeepAlives,omitempty" yaml:"disableKeepAlives"`
}

HTTPTransportConfig stores the http.Transport configuration for the http client.

func (HTTPTransportConfig) ToTransport

func (ttc HTTPTransportConfig) ToTransport() *http.Transport

ToTransport creates an http transport from the configuration.

type Option

type Option func(*clientOptions)

Option abstracts a function to modify client options.

func WithCustomAttributesFunc

func WithCustomAttributesFunc(fn CustomAttributesFunc) Option

WithCustomAttributesFunc sets the function to add custom attributes to spans and metrics.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger create an option to set the logger.

func WithMeter

func WithMeter(meter metric.Meter) Option

WithMeter creates an option to set the meter for metrics.

func WithMetricHighCardinalityPath

func WithMetricHighCardinalityPath(enabled bool) Option

WithMetricHighCardinalityPath enables high cardinality path on metrics.

func WithTraceHighCardinalityPath

func WithTraceHighCardinalityPath(enabled bool) Option

WithTraceHighCardinalityPath enables high cardinality path on traces.

func WithTracer

func WithTracer(tracer trace.Tracer) Option

WithTracer create an option to set the tracer.

type RestyConfig

type RestyConfig struct {
	// Default maximum timeout duration that is applied for all requests.
	Timeout *model.Duration `json:"timeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"timeout,omitempty"`
	// Transport stores the http.Transport configuration for the http client.
	Transport *HTTPTransportConfig `json:"transport,omitempty" yaml:"transport,omitempty"`
	// The transport layer security (LTS) configuration for the mutualTLS authentication.
	TLS *TLSConfig `json:"tls,omitempty" yaml:"tls,omitempty"`
	// Retry policy of client requests.
	Retry *RestyRetryConfig `json:"retry,omitempty" yaml:"retry,omitempty"`
	// Authentication configuration.
	Authentication *authc.RestlyAuthConfig `json:"authentication,omitempty" yaml:"authentication,omitempty"`
}

RestyConfig contains configurations to create client.

func (*RestyConfig) ToTransport

func (c *RestyConfig) ToTransport() (*http.Transport, error)

ToTransport creates an http transport from configurations.

type RestyDialerConfig

type RestyDialerConfig struct {
	// The maximum amount of time a dial will wait for a connect to complete.
	// If Deadline is also set, it may fail earlier.
	Timeout *model.Duration `json:"timeout,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"timeout"`
	// Keep-alive probes are enabled by default.
	KeepAliveEnabled *bool `json:"keepAliveEnabled,omitempty" yaml:"keepAliveEnabled"`
	// KeepAliveInterval is the time between keep-alive probes. If zero, a default value of 15 seconds is used.
	KeepAliveInterval *model.Duration `json:"keepAliveInterval,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"keepAliveInterval"`
	// KeepAliveCount is the maximum number of keep-alive probes that can go unanswered before dropping a connection.
	// If zero, a default value of 9 is used.
	KeepAliveCount *int `json:"keepAliveCount,omitempty" jsonschema:"nullable,min=0" yaml:"keepAliveCount"`
	// KeepAliveIdle is the time that the connection must be idle before the first keep-alive probe is sent.
	// If zero, a default value of 15 seconds is used.
	KeepAliveIdle *model.Duration `json:"keepAliveIdle,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"keepAliveIdle"`
	// FallbackDelay specifies the length of time to wait before spawning a RFC 6555 Fast Fallback connection.
	// That is, this is the amount of time to wait for IPv6 to succeed before assuming that IPv6 is misconfigured and falling back to IPv4.
	// If zero, a default delay of 300ms is used. A negative value disables Fast Fallback support.
	FallbackDelay *model.Duration `json:"fallbackDelay,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"fallbackDelay"`
}

RestyDialerConfig contains options the http.Dialer to connect to an address.

type RestyRetryConfig

type RestyRetryConfig struct {
	// Number of retry count. Defaults to 0 (no retry).
	Count int `json:"count,omitempty" yaml:"count,omitempty"`
	// The default wait time for sleep before retrying. Default is 100 milliseconds.
	WaitTime *model.Duration `json:"waitTime,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"waitTime,omitempty"`
	// The max wait time for sleep before retrying. Default is 2000 milliseconds.
	MaxWaitTime *model.Duration `json:"maxWaitTime,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"maxWaitTime,omitempty"`
}

RestyRetryConfig represents the retry policy of request.

type TLSClientCertificate

type TLSClientCertificate struct {
	// CertFile is the path to the TLS cert to use for TLS required connections.
	CertFile *goenvconf.EnvString `json:"certFile,omitempty" yaml:"certFile,omitempty"`
	// CertPem is alternative to certFile. Provide the certificate contents as a base64-encoded string instead of a filepath.
	CertPem *goenvconf.EnvString `json:"certPem,omitempty" yaml:"certPem,omitempty"`
	// KeyFile is the path to the TLS key to use for TLS required connections.
	KeyFile *goenvconf.EnvString `json:"keyFile,omitempty" yaml:"keyFile,omitempty"`
	// KeyPem is the alternative to keyFile. Provide the key contents as a base64-encoded string instead of a filepath.
	KeyPem *goenvconf.EnvString `json:"keyPem,omitempty" yaml:"keyPem,omitempty"`
}

TLSClientCertificate represents a cert and key pair certificate.

func (TLSClientCertificate) IsZero

func (tc TLSClientCertificate) IsZero() bool

IsZero checks if the client certificate is empty.

func (TLSClientCertificate) LoadKeyPair

func (tc TLSClientCertificate) LoadKeyPair() (*tls.Certificate, error)

LoadKeyPair loads the X509 key pair from configurations.

type TLSConfig

type TLSConfig struct {
	// Interval to reload certificates. Only takes effect for file-path certificates.
	// Default value is 24 hours.
	ReloadInterval *model.Duration `json:"reloadInterval,omitempty" jsonschema:"oneof_ref=#/$defs/Duration,oneof_type=null" yaml:"reloadInterval"`
	// RootCAFile represents paths to root certificates. For a client this verifies the server certificate. For a server this verifies client certificates.
	// If empty uses system root CA.
	RootCAFile []goenvconf.EnvString `json:"rootCAFile,omitempty" yaml:"rootCAFile,omitempty"`
	// RootCAPem is the alternative to rootCAFile. Provide the CA cert contents as a base64-encoded string instead of a filepath.
	RootCAPem []goenvconf.EnvString `json:"rootCAPem,omitempty" yaml:"rootCAPem,omitempty"`
	// CAFile is the path to the CA cert. For a client this verifies the server certificate. For a server this verifies client certificates.
	// If empty uses system root CA.
	CAFile []goenvconf.EnvString `json:"caFile,omitempty" yaml:"caFile,omitempty"`
	// CAPem is alternative to caFile. Provide the CA cert contents as a base64-encoded string instead of a filepath.
	CAPem []goenvconf.EnvString `json:"caPem,omitempty" yaml:"caPem,omitempty"`
	// Certificates contains the list of client certificates.
	Certificates []TLSClientCertificate `json:"certificates,omitempty" yaml:"certificates,omitempty"`
	// InsecureSkipVerify you can configure TLS to be enabled but skip verifying the server's certificate chain.
	InsecureSkipVerify *goenvconf.EnvBool `json:"insecureSkipVerify,omitempty" yaml:"insecureSkipVerify,omitempty"`
	// IncludeSystemCACertsPool whether to load the system certificate authorities pool alongside the certificate authority.
	IncludeSystemCACertsPool *goenvconf.EnvBool `json:"includeSystemCACertsPool,omitempty" yaml:"includeSystemCACertsPool,omitempty"`
	// Minimum acceptable TLS version.
	MinVersion string `json:"minVersion,omitempty" yaml:"minVersion,omitempty"`
	// Maximum acceptable TLS version.
	MaxVersion string `json:"maxVersion,omitempty" yaml:"maxVersion,omitempty"`
	// Explicit cipher suites can be set. If left blank, a safe default list is used.
	// See https://go.dev/src/crypto/tls/cipher_suites.go for a list of supported cipher suites.
	CipherSuites []string `json:"cipherSuites,omitempty" yaml:"cipherSuites,omitempty"`
	// ServerName requested by client for virtual hosting.
	// This sets the ServerName in the TLSConfig. Please refer to
	// https://godoc.org/crypto/tls#Config for more information. (optional)
	ServerName *goenvconf.EnvString `json:"serverName,omitempty" yaml:"serverName,omitempty"`
}

TLSConfig represents the transport layer security (LTS) configuration for the mutualTLS authentication.

func (TLSConfig) GetMaxVersion

func (tc TLSConfig) GetMaxVersion() (uint16, error)

GetMaxVersion parses the max TLS version from string.

func (TLSConfig) GetMinVersion

func (tc TLSConfig) GetMinVersion() (uint16, error)

GetMinVersion parses the minx TLS version from string.

func (TLSConfig) Validate

func (tc TLSConfig) Validate() error

Validate if the current instance is valid.

Directories

Path Synopsis
authscheme
Package authscheme defines types and interfaces for security schemes.
Package authscheme defines types and interfaces for security schemes.
basicauth
Package basicauth implements authentication interfaces for the basic security scheme.
Package basicauth implements authentication interfaces for the basic security scheme.
digestauth
Package digestauth implements authentication interfaces for the digest security scheme.
Package digestauth implements authentication interfaces for the digest security scheme.
httpauth
Package httpauth implements authentication interfaces for the http security scheme.
Package httpauth implements authentication interfaces for the http security scheme.
oauth2scheme
Package oauth2scheme implements authentication interfaces for OAuth2 security scheme.
Package oauth2scheme implements authentication interfaces for OAuth2 security scheme.

Jump to

Keyboard shortcuts

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