httpclient

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 13 Imported by: 0

README

httpclient

http client

Install:

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

Usage

Making a simple GET request

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

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, headers http.Header, res interface{}) (ret *Resp)

func (*Client) Post

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

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 NewClient

func NewClient(name string, 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 time.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