httpclient

package module
v3.0.9 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2021 License: MIT Imports: 12 Imported by: 0

README

httpclient

http client

Install:

go get github.com/go-light/httpclient/v3

Usage

Making a simple GET request

// httpClient 
httpClient := NewClientV3(
		WithRetryCount(1),
		WithTimeout(Duration(2*time.Second)),
		WithMaxIdleConns(20000),
		WithMaxIdleConnsPerHost(100),
	)

headers := http.Header{}
headers.Set("Content-Type", "application/json")

type Data struct {
	Method string `json:"method"`
}

type Reply struct {
	ErrorCode int    `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
	Data      []Data `json:"data"`
}

reply := Reply{}
ret := NewClient("test.get",
	WithRetryCount(1),
	WithTimeout(2*time.Second),
	WithMaxIdleConns(20000),
	WithMaxIdleConnsPerHost(100),
).Get(context.Background(), url, headers, &reply)

fmt.Println(ret.LogEntry.Text())
fmt.Printf("%+v\n", reply)  

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, httpHeader http.Header, res interface{}) (ret *Resp)

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, body io.Reader, httpHeader http.Header, res interface{}) (ret *Resp)

type ClientConfig

type ClientConfig struct {
	Name       string
	Timeout    Duration
	RetryCount int

	MaxIdleConns        int
	MaxIdleConnsPerHost int
}

ClientConfig is http client conf.

type Duration

type Duration xtime.Duration

Duration be used toml unmarshal string time, like 1s, 500ms.

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

UnmarshalText unmarshal text to duration.

type HttpClient

type HttpClient interface {
	Get(ctx context.Context, url string, headers http.Header, res interface{}) (ret *Resp)
	Post(ctx context.Context, url string, body io.Reader, headers http.Header, res interface{}) (ret *Resp)
}

func NewClientV3

func NewClientV3(options ...Option) HttpClient

type Option

type Option interface {
	Apply(*Client)
}

Option represents the client options

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) Option

WithMaxIdleConns sets controls the maximum number of idle (keep-alive)

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(maxIdleConnsPerHost int) Option

WithMaxIdleConnsPerHost sets controls the maximum idle

func WithRetryCount

func WithRetryCount(retryCount int) Option

WithRetryCount can be used to

func WithTimeout

func WithTimeout(timeout Duration) Option

type OptionFunc

type OptionFunc func(*Client)

OptionFunc is a function that configures a client.

func (OptionFunc) Apply

func (f OptionFunc) Apply(client *Client)

Apply calls f(client)

type Resp

type Resp struct {
	StatusCode int
	Body       []byte
	Error      error
	LogEntry   logentry.HttpClientLogEntry
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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