Documentation
¶
Index ¶
- Constants
- func DecodeResponse[T any](resp *http.Response) (T, error)
- func DefaultRetryPolicy(resp *http.Response, err error) bool
- func DeleteAs[T any](c *Client, endpoint string, options ...ClientOption) (T, error)
- func GetAs[T any](c *Client, endpoint string, options ...ClientOption) (T, error)
- func NewHTTPClient(config *ClientConfig, options ...ClientOption) *http.Client
- func PatchAs[T any](c *Client, endpoint string, body interface{}, options ...ClientOption) (T, error)
- func PostAs[T any](c *Client, endpoint string, body interface{}, options ...ClientOption) (T, error)
- func PutAs[T any](c *Client, endpoint string, body interface{}, options ...ClientOption) (T, error)
- func RequestAs[T any](c *Client, method, endpoint string, body interface{}, options ...ClientOption) (T, error)
- type Client
- func (c *Client) Delete(endpoint string, options ...ClientOption) (map[string]interface{}, error)
- func (c *Client) Get(endpoint string, options ...ClientOption) (map[string]interface{}, error)
- func (c *Client) GetBytes(endpoint string, options ...ClientOption) ([]byte, error)
- func (c *Client) GetString(endpoint string, options ...ClientOption) (string, error)
- func (c *Client) Patch(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
- func (c *Client) Post(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
- func (c *Client) Put(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
- func (c *Client) Request(method, endpoint string, body interface{}, options ...ClientOption) (*http.Response, error)
- func (c *Client) SetBasicAuth(username, password string) *Client
- func (c *Client) SetBearerToken(token string) *Client
- func (c *Client) SetContentType(contentType string) *Client
- func (c *Client) SetHeader(key string, values ...string) *Client
- func (c *Client) SetQueryParam(key, value string) *Client
- type ClientConfig
- type ClientOption
- func NoRetry() ClientOption
- func WithBaseURL(url string) ClientOption
- func WithBasicAuth(username, password string) ClientOption
- func WithBearerToken(token string) ClientOption
- func WithContentType(contentType string) ClientOption
- func WithCustomToken(tokenType, token string) ClientOption
- func WithDialTimeout(timeout time.Duration) ClientOption
- func WithErrorResult(result interface{}) ClientOption
- func WithExpectContinueTimeout(timeout time.Duration) ClientOption
- func WithFormContentType() ClientOption
- func WithHeader(key string, values ...string) ClientOption
- func WithHeaders(headers map[string][]string) ClientOption
- func WithIdleConnectionTimeout(timeout time.Duration) ClientOption
- func WithJSONContentType() ClientOption
- func WithKeepAlive(keepAlive time.Duration) ClientOption
- func WithMaxConnectionsPerHost(max int) ClientOption
- func WithMaxIdleConnections(max int) ClientOption
- func WithMaxIdleConnectionsPerHost(max int) ClientOption
- func WithMaxRetries(max int) ClientOption
- func WithMultipartContentType() ClientOption
- func WithQueryParam(key, value string) ClientOption
- func WithQueryParams(params map[string]string) ClientOption
- func WithResponseHeaderTimeout(timeout time.Duration) ClientOption
- func WithRetryDelay(delay time.Duration) ClientOption
- func WithRetryPolicy(policy RetryPolicy) ClientOption
- func WithTLSHandshakeTimeout(timeout time.Duration) ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- type RetryPolicy
- type StatusCode
Constants ¶
const ( // Default HTTP timeouts DEFAULT_TIMEOUT = 60 * time.Second DEFAULT_DIAL_TIMEOUT = 30 * time.Second DEFAULT_KEEP_ALIVE = 30 * time.Second DEFAULT_TLS_HANDSHAKE_TIMEOUT = 10 * time.Second DEFAULT_RESPONSE_HEADER_TIMEOUT = 60 * time.Second DEFAULT_EXPECT_CONTINUE_TIMEOUT = 5 * time.Second DEFAULT_MAX_IDLE_CONNECTIONS = 100 DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST = 100 DEFAULT_MAX_CONNECTIONS_PER_HOST = 1000 DEFAULT_IDLE_CONNECTION_TIMEOUT = 90 * time.Second DEFAULT_MAX_RETRIES = 3 DEFAULT_RETRY_DELAY = 1 * time.Second // Content type constants CONTENT_TYPE_JSON = "application/json" CONTENT_TYPE_FORM = "application/x-www-form-urlencoded" CONTENT_TYPE_MULTIPART = "multipart/form-data" CONTENT_TYPE_TEXT = "text/plain" CONTENT_TYPE_XML = "application/xml" CONTENT_TYPE_OCTET_STREAM = "application/octet-stream" )
HTTP client configuration constants
Variables ¶
This section is empty.
Functions ¶
func DecodeResponse ¶
DecodeResponse is a generic function to decode an HTTP response into the specified type
func DefaultRetryPolicy ¶
DefaultRetryPolicy provides a reasonable default retry policy
func DeleteAs ¶
func DeleteAs[T any](c *Client, endpoint string, options ...ClientOption) (T, error)
DeleteAs performs an HTTP DELETE request and decodes the response to the specified type
func GetAs ¶
func GetAs[T any](c *Client, endpoint string, options ...ClientOption) (T, error)
GetAs performs an HTTP GET request and decodes the response to the specified type
func NewHTTPClient ¶
func NewHTTPClient(config *ClientConfig, options ...ClientOption) *http.Client
NewHTTPClient creates and configures a new HTTP client
func PatchAs ¶
func PatchAs[T any](c *Client, endpoint string, body interface{}, options ...ClientOption) (T, error)
PatchAs performs an HTTP PATCH request and decodes the response to the specified type
func PostAs ¶
func PostAs[T any](c *Client, endpoint string, body interface{}, options ...ClientOption) (T, error)
PostAs performs an HTTP POST request and decodes the response to the specified type
Types ¶
type Client ¶
type Client struct {
Config ClientConfig
HTTPClient *http.Client
}
Client is the main HTTP client interface
func NewClient ¶
func NewClient(options ...ClientOption) *Client
NewClient creates a new HTTP client with the provided configuration Actually this should be better to have it's own simpleClientOptions instead of ClientOption which belongs to http.Client options. Then one of them is just WithConfig(NewDefaultConfig(ClientOption))
func (*Client) Delete ¶
func (c *Client) Delete(endpoint string, options ...ClientOption) (map[string]interface{}, error)
Delete performs an HTTP DELETE request and returns the result as a JSON map
func (*Client) Get ¶
func (c *Client) Get(endpoint string, options ...ClientOption) (map[string]interface{}, error)
Get performs an HTTP GET request and returns the result as a JSON map
func (*Client) GetBytes ¶
func (c *Client) GetBytes(endpoint string, options ...ClientOption) ([]byte, error)
GetBytes gets the raw bytes from an HTTP response
func (*Client) GetString ¶
func (c *Client) GetString(endpoint string, options ...ClientOption) (string, error)
GetString gets the response as a string
func (*Client) Patch ¶
func (c *Client) Patch(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
Patch performs an HTTP PATCH request and returns the result as a JSON map
func (*Client) Post ¶
func (c *Client) Post(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
Post performs an HTTP POST request and returns the result as a JSON map
func (*Client) Put ¶
func (c *Client) Put(endpoint string, body interface{}, options ...ClientOption) (map[string]interface{}, error)
Put performs an HTTP PUT request and returns the result as a JSON map
func (*Client) Request ¶
func (c *Client) Request(method, endpoint string, body interface{}, options ...ClientOption) (*http.Response, error)
Request performs an HTTP request and returns the raw response
func (*Client) SetBasicAuth ¶
SetBasicAuth sets basic authentication credentials
func (*Client) SetBearerToken ¶
SetBearerToken sets a bearer token for authentication
func (*Client) SetContentType ¶
SetContentType sets the content type for requests
func (*Client) SetQueryParam ¶
SetQueryParam sets a query parameter to the client's default parameters
type ClientConfig ¶
type ClientConfig struct {
// Basic settings
BaseURL string
Headers map[string][]string
QueryParams map[string]string
ContentType string
// Authentication
Username string
Password string
Token string
TokenType string
// Error handling
ErrorResult interface{}
// Timeout settings
Timeout time.Duration
DialTimeout time.Duration
KeepAlive time.Duration
TLSHandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
ExpectContinueTimeout time.Duration
IdleConnectionTimeout time.Duration
// Connection settings
MaxIdleConnections int
MaxIdleConnsPerHost int
MaxConnsPerHost int
// Retry settings
MaxRetries int
RetryDelay time.Duration
RetryPolicy RetryPolicy
}
ClientConfig holds configuration options for the HTTP client
func NewDefaultConfig ¶
func NewDefaultConfig(options ...ClientOption) *ClientConfig
NewDefaultConfig creates a new configuration with default values
type ClientOption ¶
type ClientOption func(*ClientConfig)
ClientOption defines a function that modifies client configuration
func WithBaseURL ¶
func WithBaseURL(url string) ClientOption
WithBaseURL sets the base URL for the client
func WithBasicAuth ¶
func WithBasicAuth(username, password string) ClientOption
WithBasicAuth sets basic authentication for requests
func WithBearerToken ¶
func WithBearerToken(token string) ClientOption
WithBearerToken sets bearer token authentication for requests
func WithContentType ¶
func WithContentType(contentType string) ClientOption
WithContentType sets the content type for requests
func WithCustomToken ¶
func WithCustomToken(tokenType, token string) ClientOption
WithCustomToken sets a custom token authentication for requests
func WithDialTimeout ¶
func WithDialTimeout(timeout time.Duration) ClientOption
WithDialTimeout sets the connection dial timeout
func WithErrorResult ¶
func WithErrorResult(result interface{}) ClientOption
WithErrorResult sets a result object for error responses
func WithExpectContinueTimeout ¶
func WithExpectContinueTimeout(timeout time.Duration) ClientOption
WithExpectContinueTimeout sets the expect continue timeout
func WithFormContentType ¶
func WithFormContentType() ClientOption
WithFormContentType sets the content type to application/x-www-form-urlencoded
func WithHeader ¶
func WithHeader(key string, values ...string) ClientOption
WithHeader adds a single header
func WithHeaders ¶
func WithHeaders(headers map[string][]string) ClientOption
WithHeaders sets headers for requests
func WithIdleConnectionTimeout ¶
func WithIdleConnectionTimeout(timeout time.Duration) ClientOption
WithIdleConnectionTimeout sets the idle connection timeout
func WithJSONContentType ¶
func WithJSONContentType() ClientOption
WithJSONContentType sets the content type to application/json
func WithKeepAlive ¶
func WithKeepAlive(keepAlive time.Duration) ClientOption
WithKeepAlive sets the keep-alive duration
func WithMaxConnectionsPerHost ¶
func WithMaxConnectionsPerHost(max int) ClientOption
WithMaxConnectionsPerHost sets the maximum number of connections per host
func WithMaxIdleConnections ¶
func WithMaxIdleConnections(max int) ClientOption
WithMaxIdleConnections sets the maximum number of idle connections
func WithMaxIdleConnectionsPerHost ¶
func WithMaxIdleConnectionsPerHost(max int) ClientOption
WithMaxIdleConnectionsPerHost sets the maximum number of idle connections per host
func WithMaxRetries ¶
func WithMaxRetries(max int) ClientOption
WithMaxRetries sets the maximum number of retry attempts
func WithMultipartContentType ¶
func WithMultipartContentType() ClientOption
WithMultipartContentType sets the content type to multipart/form-data
func WithQueryParam ¶
func WithQueryParam(key, value string) ClientOption
WithQueryParam adds a single query parameter
func WithQueryParams ¶
func WithQueryParams(params map[string]string) ClientOption
WithQueryParams sets query parameters for requests
func WithResponseHeaderTimeout ¶
func WithResponseHeaderTimeout(timeout time.Duration) ClientOption
WithResponseHeaderTimeout sets the response header timeout
func WithRetryDelay ¶
func WithRetryDelay(delay time.Duration) ClientOption
WithRetryDelay sets the delay between retry attempts
func WithRetryPolicy ¶
func WithRetryPolicy(policy RetryPolicy) ClientOption
WithRetryPolicy sets a custom retry policy
func WithTLSHandshakeTimeout ¶
func WithTLSHandshakeTimeout(timeout time.Duration) ClientOption
WithTLSHandshakeTimeout sets the TLS handshake timeout
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets the overall request timeout
type RetryPolicy ¶
RetryPolicy determines if a request should be retried