Skip to content

Import path: gitlab.soludian.com/soludian/fountain/clients/fhttp_client

fhttp_client

go
import "gitlab.soludian.com/soludian/fountain/clients/fhttp_client"

Index

Constants

go
const ClientName = "Fountain HTTP Client/2.0"

PackageName sets the package name

go
const KPackageName = "fhttp_client"

Variables

go
var GetFountainInstance = Lib.GetFountainInstance

go
var GetFountainManager = Lib.GetFountainManager

Sử 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()
go
var InstallFountainInstance = Lib.InstallFountainInstance

go
var InstallFountainInstanceOnce = Lib.InstallFountainInstanceOnce

go
var InstallFountainInstancePortable = Lib.InstallFountainInstancePortable

Sử 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()
go
var InstallFountainInstances = Lib.InstallFountainInstances

go
var KDefaultClientName = env.GetFullAppServiceServer(KPackageName)

Truy cập thẳng tới bộ quản lý thư viện

go
var Lib = lib_3rd.NewLib(newClient, lib_3rd.WithDefaultConfigFunc[Config, Client](DefaultConfig))

go
var WithConfigKey = Lib.WithConfigKey

func APIRestyCaller

go
func APIRestyCaller[T any](req *resty.Request, method string, endpoint string) (T, *froto.RpcError)

func APIRestyCallerResponse

go
func APIRestyCallerResponse[T any](req *resty.Request, method string, endpoint string) (*fhttp.Response[T], *froto.RpcError)

func APISimpleCaller

go
func APISimpleCaller[T any](client *http.Client, req *http.Request) (T, *froto.RpcError)

func APISimpleCallerResponse

go
func APISimpleCallerResponse[T any](client *http.Client, req *http.Request) (*fhttp.Response[T], *froto.RpcError)

func BodyRequest

go
func BodyRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)

func DecryptionResponse

go
func DecryptionResponse[T any](res *resty.Response) (*T, *froto.RpcError)

func DefaultContext

go
func DefaultContext() (context.Context, context.CancelFunc)

func ExecuteRequest

go
func ExecuteRequest[T any](req *resty.Request, method string, endpoint string) (*T, *froto.RpcError)

func FormRequest

go
func FormRequest(ctx context.Context, client *Client, bodyData any) (*resty.Request, *froto.RpcError)

func WithAddr

go
func WithAddr(addr string) lib_3rd.Option[Config]

func WithCircuitBreakerFailureThreshold

go
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

go
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

go
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

go
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

go
func WithClient(client *http.Client) lib_3rd.Option[Config]

func WithConfig

go
func WithConfig(conf *Config) lib_3rd.Option[Config]

WithConfig

func WithCounter

go
func WithCounter(counter metrics.Counter) lib_3rd.Option[Config]

func WithDebug

go
func WithDebug(debug bool) lib_3rd.Option[Config]

func WithDisableAccessInterceptor

go
func WithDisableAccessInterceptor(disable ...bool) lib_3rd.Option[Config]

func WithDisableAccessInterceptorRes

go
func WithDisableAccessInterceptorRes(disable ...bool) lib_3rd.Option[Config]

func WithDisableKeepAlives

go
func WithDisableKeepAlives(disable ...bool) lib_3rd.Option[Config]

func WithDisableTraceInterceptor

go
func WithDisableTraceInterceptor(disable ...bool) lib_3rd.Option[Config]

func WithEnableCircuitBreaker

go
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

go
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

go
func WithHistogram(histogram metrics.Histogram) lib_3rd.Option[Config]

func WithIdempotencyKeyHeader

go
func WithIdempotencyKeyHeader(header string) lib_3rd.Option[Config]

WithIdempotencyKeyHeader sets the header name for the idempotency key. Default: "X-Idempotency-Key".

func WithIdempotencyKeySecret

go
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

go
func WithIdleConnTimeout(timeout time.Duration) lib_3rd.Option[Config]

func WithMaxIdleConns

go
func WithMaxIdleConns(max int) lib_3rd.Option[Config]

func WithMaxIdleConnsPerHost

go
func WithMaxIdleConnsPerHost(max int) lib_3rd.Option[Config]

func WithName

go
func WithName(name string) lib_3rd.Option[Config]

func WithRawDebug

go
func WithRawDebug(rawDebug bool) lib_3rd.Option[Config]

func WithReadTimeout

go
func WithReadTimeout(timeout time.Duration) lib_3rd.Option[Config]

func WithSlowLogThreshold

go
func WithSlowLogThreshold(threshold time.Duration) lib_3rd.Option[Config]

func WithTokenSource

go
func WithTokenSource(tokenSource auth_token.TokenSource) lib_3rd.Option[Config]

type BodyAuthorization

go
type BodyAuthorization func(map[string]any)

type Client

Component component

go
type Client struct {
    *resty.Client
    // contains filtered or unexported fields
}

func (*Client) CircuitBreaker

go
func (c *Client) CircuitBreaker() *gobreaker.CircuitBreaker

CircuitBreaker trả về circuit breaker instance của client.

func (*Client) CreateToken

go
func (c *Client) CreateToken() *oauth2.Token

func (*Client) GetToken

go
func (c *Client) GetToken() *oauth2.Token

type Config

Config HTTP configuration options

go
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

go
func DefaultConfig() *Config

func (*Config) Validate

go
func (conf *Config) Validate() error

type FormAuthorization

go
type FormAuthorization func(url.Values)

type RequestAuthorization

go
type RequestAuthorization func(*resty.Request)

func AuthorizeBasic

go
func AuthorizeBasic(user, password string) RequestAuthorization

type ValuesCodecInf

go
type ValuesCodecInf interface {
    Decode(dst any, src map[string][]string) error
    Encode(src any, dst map[string][]string) error
}

Generated by gomarkdoc