Documentation
¶
Overview ¶
Package httpclient provides a configurable HTTP client module for the modular framework.
Package httpclient provides a configurable HTTP client module for the modular framework.
Package httpclient provides a configurable HTTP client module for the modular framework.
Index ¶
- Constants
- func NewHTTPClientModule() modular.Module
- type ClientService
- type Config
- type FileLogger
- type HTTPClientModule
- func (m *HTTPClientModule) Client() *http.Client
- func (m *HTTPClientModule) Dependencies() []string
- func (m *HTTPClientModule) Init(app modular.Application) error
- func (m *HTTPClientModule) Name() string
- func (m *HTTPClientModule) ProvidesServices() []modular.ServiceProvider
- func (m *HTTPClientModule) RegisterConfig(app modular.Application) error
- func (m *HTTPClientModule) RequestModifier() RequestModifierFunc
- func (m *HTTPClientModule) RequiresServices() []modular.ServiceDependency
- func (m *HTTPClientModule) SetRequestModifier(modifier RequestModifierFunc)
- func (m *HTTPClientModule) Start(context.Context) error
- func (m *HTTPClientModule) Stop(context.Context) error
- func (m *HTTPClientModule) WithTimeout(timeoutSeconds int) *http.Client
- type RequestModifierFunc
- type VerboseOptions
Constants ¶
const ModuleName = "httpclient"
ModuleName is the name of this module
const ServiceName = "httpclient"
ServiceName is the name of the service provided by this module
Variables ¶
This section is empty.
Functions ¶
func NewHTTPClientModule ¶
NewHTTPClientModule creates a new instance of the HTTP client module.
Types ¶
type ClientService ¶
type ClientService interface {
// Client returns the configured http.Client instance
Client() *http.Client
// RequestModifier returns a modifier function that can modify a request before it's sent
RequestModifier() RequestModifierFunc
// WithTimeout creates a new client with the specified timeout in seconds
WithTimeout(timeoutSeconds int) *http.Client
}
ClientService defines the interface for the HTTP client service. Any module that needs to make HTTP requests can use this service.
type Config ¶
type Config struct {
// MaxIdleConns controls the maximum number of idle (keep-alive) connections across all hosts.
MaxIdleConns int `yaml:"max_idle_conns" json:"max_idle_conns"`
// MaxIdleConnsPerHost controls the maximum idle (keep-alive) connections to keep per-host.
MaxIdleConnsPerHost int `yaml:"max_idle_conns_per_host" json:"max_idle_conns_per_host"`
// IdleConnTimeout is the maximum amount of time an idle connection will remain idle before
// closing itself, in seconds.
IdleConnTimeout int `yaml:"idle_conn_timeout" json:"idle_conn_timeout"`
// RequestTimeout is the maximum time for a request to complete, in seconds.
RequestTimeout int `yaml:"request_timeout" json:"request_timeout"`
// TLSTimeout is the maximum time waiting for TLS handshake, in seconds.
TLSTimeout int `yaml:"tls_timeout" json:"tls_timeout"`
// DisableCompression disables decompressing response bodies.
DisableCompression bool `yaml:"disable_compression" json:"disable_compression"`
// DisableKeepAlives disables HTTP keep-alive and will only use connections for a single request.
DisableKeepAlives bool `yaml:"disable_keep_alives" json:"disable_keep_alives"`
// Verbose enables detailed logging of HTTP requests and responses.
Verbose bool `yaml:"verbose" json:"verbose"`
// VerboseOptions configures the behavior when Verbose is enabled.
VerboseOptions *VerboseOptions `yaml:"verbose_options" json:"verbose_options"`
}
Config defines the configuration for the HTTP client module.
func (*Config) GetTimeout ¶
GetTimeout converts a timeout value from seconds to time.Duration.
type FileLogger ¶
type FileLogger struct {
// contains filtered or unexported fields
}
FileLogger handles logging HTTP request and response data to files.
func NewFileLogger ¶
func NewFileLogger(baseDir string, logger modular.Logger) (*FileLogger, error)
NewFileLogger creates a new file logger that writes HTTP data to files.
func (*FileLogger) Close ¶
func (f *FileLogger) Close() error
Close closes any open files and cleans up resources.
func (*FileLogger) LogRequest ¶
func (f *FileLogger) LogRequest(id string, data []byte) error
LogRequest writes request data to a file.
func (*FileLogger) LogResponse ¶
func (f *FileLogger) LogResponse(id string, data []byte) error
LogResponse writes response data to a file.
func (*FileLogger) LogTransactionToFile ¶
func (f *FileLogger) LogTransactionToFile(id string, reqData, respData []byte, duration time.Duration, url string) error
LogTransactionToFile logs both request and response data to a single file for easier analysis.
type HTTPClientModule ¶
type HTTPClientModule struct {
// contains filtered or unexported fields
}
HTTPClientModule implements a configurable HTTP client module.
func (*HTTPClientModule) Client ¶
func (m *HTTPClientModule) Client() *http.Client
Client returns the configured http.Client instance.
func (*HTTPClientModule) Dependencies ¶
func (m *HTTPClientModule) Dependencies() []string
Dependencies returns the names of modules this module depends on.
func (*HTTPClientModule) Init ¶
func (m *HTTPClientModule) Init(app modular.Application) error
Init initializes the module with the provided application.
func (*HTTPClientModule) Name ¶
func (m *HTTPClientModule) Name() string
Name returns the name of the module.
func (*HTTPClientModule) ProvidesServices ¶
func (m *HTTPClientModule) ProvidesServices() []modular.ServiceProvider
ProvidesServices returns services provided by this module.
func (*HTTPClientModule) RegisterConfig ¶
func (m *HTTPClientModule) RegisterConfig(app modular.Application) error
RegisterConfig registers the module's configuration.
func (*HTTPClientModule) RequestModifier ¶
func (m *HTTPClientModule) RequestModifier() RequestModifierFunc
RequestModifier returns a modifier function that can modify a request before it's sent.
func (*HTTPClientModule) RequiresServices ¶
func (m *HTTPClientModule) RequiresServices() []modular.ServiceDependency
RequiresServices returns services required by this module.
func (*HTTPClientModule) SetRequestModifier ¶
func (m *HTTPClientModule) SetRequestModifier(modifier RequestModifierFunc)
SetRequestModifier sets the request modifier function.
func (*HTTPClientModule) Start ¶
func (m *HTTPClientModule) Start(context.Context) error
Start performs startup logic for the module.
func (*HTTPClientModule) Stop ¶
func (m *HTTPClientModule) Stop(context.Context) error
Stop performs shutdown logic for the module.
func (*HTTPClientModule) WithTimeout ¶
func (m *HTTPClientModule) WithTimeout(timeoutSeconds int) *http.Client
WithTimeout creates a new client with the specified timeout in seconds.
type RequestModifierFunc ¶
RequestModifierFunc is a function type that can be used to modify an HTTP request before it is sent by the client.
type VerboseOptions ¶
type VerboseOptions struct {
// LogHeaders enables logging of request and response headers.
LogHeaders bool `yaml:"log_headers" json:"log_headers"`
// LogBody enables logging of request and response bodies.
LogBody bool `yaml:"log_body" json:"log_body"`
// MaxBodyLogSize limits the size of logged request and response bodies.
MaxBodyLogSize int `yaml:"max_body_log_size" json:"max_body_log_size"`
// LogToFile enables logging to files instead of just the logger.
LogToFile bool `yaml:"log_to_file" json:"log_to_file"`
// LogFilePath is the directory where log files will be written.
LogFilePath string `yaml:"log_file_path" json:"log_file_path"`
}
VerboseOptions configures the behavior of verbose logging.