Import path:
gitlab.soludian.com/soludian/fountain/clients/fhttp_client
fhttp_client
import "gitlab.soludian.com/soludian/fountain/clients/fhttp_client"Index
- Constants
- Variables
- func APIRestyCaller[T any](req *resty.Request, method string, endpoint string) (T, *froto.RpcError)
- func APIRestyCallerResponse[T any](req *resty.Request, method string, endpoint string) (*fhttp.Response[T], *froto.RpcError)
- func APISimpleCaller[T any](client *http.Client, req *http.Request) (T, *froto.RpcError)
- func APISimpleCallerResponse[T any](client *http.Client, req *http.Request) (*fhttp.Response[T], *froto.RpcError)
- func BodyRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)
- func DecryptionResponse[T any](res *resty.Response) (*T, *froto.RpcError)
- func DefaultContext() (context.Context, context.CancelFunc)
- func ExecuteRequest[T any](req *resty.Request, method string, endpoint string) (*T, *froto.RpcError)
- func FormRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)
- func WithAddr(addr string) lib_3rd.Option[Config]
- func WithCircuitBreakerFailureThreshold(threshold uint32) lib_3rd.Option[Config]
- func WithCircuitBreakerInterval(interval time.Duration) lib_3rd.Option[Config]
- func WithCircuitBreakerMaxRequests(max uint32) lib_3rd.Option[Config]
- func WithCircuitBreakerTimeout(timeout time.Duration) lib_3rd.Option[Config]
- func WithClient(client *http.Client) lib_3rd.Option[Config]
- func WithConfig(conf *Config) lib_3rd.Option[Config]
- func WithCounter(counter metrics.Counter) lib_3rd.Option[Config]
- func WithDebug(debug bool) lib_3rd.Option[Config]
- func WithDisableAccessInterceptor(disable ...bool) lib_3rd.Option[Config]
- func WithDisableAccessInterceptorRes(disable ...bool) lib_3rd.Option[Config]
- func WithDisableKeepAlives(disable ...bool) lib_3rd.Option[Config]
- func WithDisableTraceInterceptor(disable ...bool) lib_3rd.Option[Config]
- func WithEnableCircuitBreaker(enable ...bool) lib_3rd.Option[Config]
- func WithEnableIdempotency(enable ...bool) lib_3rd.Option[Config]
- func WithHistogram(histogram metrics.Histogram) lib_3rd.Option[Config]
- func WithIdempotencyKeyHeader(header string) lib_3rd.Option[Config]
- func WithIdempotencyKeySecret(secret string) lib_3rd.Option[Config]
- func WithIdleConnTimeout(timeout time.Duration) lib_3rd.Option[Config]
- func WithMaxIdleConns(max int) lib_3rd.Option[Config]
- func WithMaxIdleConnsPerHost(max int) lib_3rd.Option[Config]
- func WithName(name string) lib_3rd.Option[Config]
- func WithRawDebug(rawDebug bool) lib_3rd.Option[Config]
- func WithReadTimeout(timeout time.Duration) lib_3rd.Option[Config]
- func WithSlowLogThreshold(threshold time.Duration) lib_3rd.Option[Config]
- func WithTokenSource(tokenSource auth_token.TokenSource) lib_3rd.Option[Config]
- type BodyAuthorization
- type Client
- type Config
- type FormAuthorization
- type RequestAuthorization
- type ValuesCodecInf
Constants
const ClientName = "Fountain HTTP Client/2.0"PackageName sets the package name
const KPackageName = "fhttp_client"Variables
var GetFountainInstance = Lib.GetFountainInstancevar GetFountainManager = Lib.GetFountainManagerSử dụng khi config instance ở dạng key:value; Nếu config instance ở dạng key:array thì sử dụng hàm InstallFountainInstances Nếu config ở dạng key:array thì sẽ chỉ install config phần tử đầu tiên mà thôi
Install with config format <key>:<value>; eg: fhttp_client:<value>
Usage:
config.yaml:
fhttp_client:
name: default_name
...
code.go
fhttp_client.InstallFountainInstance()
fhttp_client.WithConfigKey("fhttp_client").InstallFountainInstance()var InstallFountainInstance = Lib.InstallFountainInstancevar InstallFountainInstanceOnce = Lib.InstallFountainInstanceOncevar InstallFountainInstancePortable = Lib.InstallFountainInstancePortableSử dụng khi config instance ở dạng key:array<value>; Sẽ luôn cố gắng khởi tạo kể cả khi config ở dạng key:value
Install with config format <key>:array<value>; eg: fhttp_client:array<value>
Usage:
config.yaml:
fhttp_client:
- name: default_name
...
code.go
fhttp_client.InstallFountainInstances()
fhttp_client.WithConfigKey("fhttp_client").InstallFountainInstances()var InstallFountainInstances = Lib.InstallFountainInstancesvar KDefaultClientName = env.GetFullAppServiceServer(KPackageName)Truy cập thẳng tới bộ quản lý thư viện
var Lib = lib_3rd.NewLib(newClient, lib_3rd.WithDefaultConfigFunc[Config, Client](DefaultConfig))var WithConfigKey = Lib.WithConfigKeyfunc APIRestyCaller
func APIRestyCaller[T any](req *resty.Request, method string, endpoint string) (T, *froto.RpcError)func APIRestyCallerResponse
func APIRestyCallerResponse[T any](req *resty.Request, method string, endpoint string) (*fhttp.Response[T], *froto.RpcError)func APISimpleCaller
func APISimpleCaller[T any](client *http.Client, req *http.Request) (T, *froto.RpcError)func APISimpleCallerResponse
func APISimpleCallerResponse[T any](client *http.Client, req *http.Request) (*fhttp.Response[T], *froto.RpcError)func BodyRequest
func BodyRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)func DecryptionResponse
func DecryptionResponse[T any](res *resty.Response) (*T, *froto.RpcError)func DefaultContext
func DefaultContext() (context.Context, context.CancelFunc)func ExecuteRequest
func ExecuteRequest[T any](req *resty.Request, method string, endpoint string) (*T, *froto.RpcError)func FormRequest
func FormRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)func WithAddr
func WithAddr(addr string) lib_3rd.Option[Config]func WithCircuitBreakerFailureThreshold
func WithCircuitBreakerFailureThreshold(threshold uint32) lib_3rd.Option[Config]WithCircuitBreakerFailureThreshold sets the number of consecutive failures required to trip the circuit breaker. Default: 5.
func WithCircuitBreakerInterval
func WithCircuitBreakerInterval(interval time.Duration) lib_3rd.Option[Config]WithCircuitBreakerInterval sets the cyclic period of the closed state for the circuit breaker to clear internal counts. Default: 0 (never clears).
func WithCircuitBreakerMaxRequests
func WithCircuitBreakerMaxRequests(max uint32) lib_3rd.Option[Config]WithCircuitBreakerMaxRequests sets the maximum number of requests allowed to pass through when the circuit breaker is half-open. Default: 1.
func WithCircuitBreakerTimeout
func WithCircuitBreakerTimeout(timeout time.Duration) lib_3rd.Option[Config]WithCircuitBreakerTimeout sets the period of the open state, after which the state becomes half-open. Default: 60s.
func WithClient
func WithClient(client *http.Client) lib_3rd.Option[Config]func WithConfig
func WithConfig(conf *Config) lib_3rd.Option[Config]WithConfig
func WithCounter
func WithCounter(counter metrics.Counter) lib_3rd.Option[Config]func WithDebug
func WithDebug(debug bool) lib_3rd.Option[Config]func WithDisableAccessInterceptor
func WithDisableAccessInterceptor(disable ...bool) lib_3rd.Option[Config]func WithDisableAccessInterceptorRes
func WithDisableAccessInterceptorRes(disable ...bool) lib_3rd.Option[Config]func WithDisableKeepAlives
func WithDisableKeepAlives(disable ...bool) lib_3rd.Option[Config]func WithDisableTraceInterceptor
func WithDisableTraceInterceptor(disable ...bool) lib_3rd.Option[Config]func WithEnableCircuitBreaker
func WithEnableCircuitBreaker(enable ...bool) lib_3rd.Option[Config]WithEnableCircuitBreaker enables the circuit breaker for this client. When enabled, the client will fail fast if the downstream service is unhealthy.
func WithEnableIdempotency
func WithEnableIdempotency(enable ...bool) lib_3rd.Option[Config]WithEnableIdempotency enables automatic X-Idempotency-Key header generation for non-safe HTTP methods (POST, PUT, PATCH, DELETE). The server must support idempotency key deduplication.
func WithHistogram
func WithHistogram(histogram metrics.Histogram) lib_3rd.Option[Config]func WithIdempotencyKeyHeader
func WithIdempotencyKeyHeader(header string) lib_3rd.Option[Config]WithIdempotencyKeyHeader sets the header name for the idempotency key. Default: "X-Idempotency-Key".
func WithIdempotencyKeySecret
func WithIdempotencyKeySecret(secret string) lib_3rd.Option[Config]WithIdempotencyKeySecret sets the HMAC secret for signing idempotency keys. Must match the server's IdempotencyKeySecret for validation to pass.
func WithIdleConnTimeout
func WithIdleConnTimeout(timeout time.Duration) lib_3rd.Option[Config]func WithMaxIdleConns
func WithMaxIdleConns(max int) lib_3rd.Option[Config]func WithMaxIdleConnsPerHost
func WithMaxIdleConnsPerHost(max int) lib_3rd.Option[Config]func WithName
func WithName(name string) lib_3rd.Option[Config]func WithRawDebug
func WithRawDebug(rawDebug bool) lib_3rd.Option[Config]func WithReadTimeout
func WithReadTimeout(timeout time.Duration) lib_3rd.Option[Config]func WithSlowLogThreshold
func WithSlowLogThreshold(threshold time.Duration) lib_3rd.Option[Config]func WithTokenSource
func WithTokenSource(tokenSource auth_token.TokenSource) lib_3rd.Option[Config]type BodyAuthorization
type BodyAuthorization func(map[string]any)type Client
Component component
type Client struct {
*resty.Client
// contains filtered or unexported fields
}func (*Client) CircuitBreaker
func (c *Client) CircuitBreaker() *gobreaker.CircuitBreakerCircuitBreaker trả về circuit breaker instance của client.
func (*Client) CreateToken
func (c *Client) CreateToken() *oauth2.Tokenfunc (*Client) GetToken
func (c *Client) GetToken() *oauth2.Tokentype Config
Config HTTP configuration options
type Config struct {
lib_3rd.BaseConfig `conf:",squash"` // default Fountain HTTP Client
Addr string `conf:"addr" json:"addr,omitempty"` // connection address
Debug bool `conf:"debug" json:"debug,omitempty"` // Whether to enable debugging, it is not enabled by default, after opening and adding export FOUNTAIN_DEBUG=true, you can see each request, configuration name, address, time consumption, request data, response data
RawDebug bool `conf:"raw_debug" json:"raw_debug,omitempty"` // Whether to enable native debugging, not enabled by default
ReadTimeout time.Duration `conf:"read_timeout" json:"read_timeout,omitempty"` // read timeout, default 2s
SlowLogThreshold time.Duration `conf:"slow_log_threshold" json:"slow_log_threshold,omitempty"` // Threshold for slow logging, default 500ms
IdleConnTimeout time.Duration `conf:"idle_conn_timeout" json:"idle_conn_timeout,omitempty"` // Set idle connection time, default 90 * time.Second
MaxIdleConns int `conf:"max_idle_conns" json:"max_idle_conns,omitempty"` // Set the maximum number of idle connections
MaxIdleConnsPerHost int `conf:"max_idle_conns_per_host" json:"max_idle_conns_per_host,omitempty"` // Set the number of long connections
DisableTraceInterceptor bool `conf:"disable_trace_interceptor" json:"disable_trace_interceptor,omitempty"` // Whether to enable link tracking, it is enabled by default
DisableKeepAlives bool `conf:"disable_keep_alives" json:"disable_keep_alives,omitempty"` // Whether to open a long connection, open by default
DisableAccessInterceptor bool `conf:"disable_access_interceptor" json:"disable_access_interceptor,omitempty"` // Whether to enable record request data, not enabled by default
DisableAccessInterceptorRes bool `conf:"disable_access_interceptor_res" json:"disable_access_interceptor_res,omitempty"` // Whether to enable the record response parameter, not enabled by default
MaxRetries int `conf:"max_retries" json:"max_retires,omitempty"` // Maximum number of retries, default 3
EnableIdempotency bool `conf:"enable_idempotency" json:"enable_idempotency,omitempty"` // Whether to enable automatic idempotency key generation for non-safe methods
IdempotencyKeyHeader string `conf:"idempotency_key_header" json:"idempotency_key_header,omitempty"` // Custom idempotency header name, default: X-Idempotency-Key
IdempotencyKeySecret string `conf:"idempotency_key_secret" json:"idempotency_key_secret,omitempty"` // HMAC secret for signing idempotency keys (must match server)
EnableCircuitBreaker bool `conf:"enable_circuit_breaker" json:"enable_circuit_breaker,omitempty"` // Whether to enable circuit breaker for this client
CircuitBreakerMaxRequests uint32 `conf:"circuit_breaker_max_requests" json:"circuit_breaker_max_requests,omitempty"` // Maximum number of requests allowed to pass through when the circuit breaker is half-open, default 1
CircuitBreakerInterval time.Duration `conf:"circuit_breaker_interval" json:"circuit_breaker_interval,omitempty"` // Cyclic period of the closed state for the circuit breaker to clear internal counts, default 0 (never clears)
CircuitBreakerTimeout time.Duration `conf:"circuit_breaker_timeout" json:"circuit_breaker_timeout,omitempty"` // Period of the open state, after which the state becomes half-open, default 60s
CircuitBreakerFailureThreshold uint32 `conf:"circuit_breaker_failure_threshold" json:"circuit_breaker_failure_threshold,omitempty"` // Number of consecutive failures to trip the circuit breaker, default 5
// contains filtered or unexported fields
}func DefaultConfig
func DefaultConfig() *Configfunc (*Config) Validate
func (conf *Config) Validate() errortype FormAuthorization
type FormAuthorization func(url.Values)type RequestAuthorization
type RequestAuthorization func(*resty.Request)func AuthorizeBasic
func AuthorizeBasic(user, password string) RequestAuthorizationtype ValuesCodecInf
type ValuesCodecInf interface {
Decode(dst any, src map[string][]string) error
Encode(src any, dst map[string][]string) error
}Generated by gomarkdoc