httpclient

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2019 License: MIT Imports: 16 Imported by: 3

README

httpclient

GoDoc Go Report Card

More smooth package for http operations as a client:

  • http request builder
  • http response processor
  • http client extension
http.Request Builder

import "github.com/x-mod/httpclient"

requestBuilder := httpclient.NewRequestBuilder(
        httpclient.URL("https://url"),
        httpclient.Method("GET"),
        httpclient.Query("key", "value"),
        httpclient.Header("key", "value"),
        httpclient.BasicAuth("user", "pass"),
        httpclient.Credential(*tlsconfig),
        httpclient.Body(
            httpclient.JSON(map[string]interface{}{
                "a": "hello",
                "b": true,
                "c": 1,
            }),
        ),
    )

req, err := requestBuilder.Get()

http.Response Processor
//ResponseProcessor interface
type ResponseProcessor interface {
	Process(context.Context, *http.Response) error
}

Implement your own ResponseProcessor

http.Client Extension

extend the http.Client with more useful interfaces:

import "github.com/x-mod/httpclient"

client := httpclient.New(
    httpclient.MaxConnsPerHost(16),
    httpclient.Retry(3),
    httpclient.Response(
        httpclient.NewDumpResponse(),
    ),
)

//get standard http.Client
c := client.GetClient()
//get standard http.Transport
tr := client.GetTransport()

//extension fn
err := client.Execute(context.TODO())
err := client.ExecuteRequest(context.TODO(), request)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//DefaultMaxConnsPerHost default max connections for per host
	DefaultMaxConnsPerHost = 32
	//DefaultMaxIdleConnsPerHost default max idle connections for per host
	DefaultMaxIdleConnsPerHost = 8
	//DefaultClientTimeout default client timeout for each do request
	DefaultClientTimeout = 30 * time.Second
	//DefaultTLSHandhakeTimeout default client tls hands hake timeout
	DefaultTLSHandhakeTimeout = 10 * time.Second
)
View Source
var DefaultTLSConfig *tls.Config

DefaultTLSConfig default tls.config is nil

Functions

func DebugDialer added in v0.2.0

func DebugDialer(ctx context.Context, network, addr string) (net.Conn, error)

DebugDialer debug dialer

Types

type Body

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

Body struct

func (*Body) ContentType

func (b *Body) ContentType() string

ContentType Body Content-Type

func (*Body) Get

func (b *Body) Get() (io.Reader, error)

Get Body io.Reader

type BodyOpt

type BodyOpt func(*bodyConfig)

BodyOpt type

func Binary

func Binary(bytes []byte) BodyOpt

Binary opt

func Form

func Form(obj url.Values) BodyOpt

Form opt

func JSON

func JSON(obj map[string]interface{}) BodyOpt

JSON opt

func PB

func PB(obj proto.Message) BodyOpt

PB opt

func PBJSON

func PBJSON(obj proto.Message) BodyOpt

PBJSON opt

func Text

func Text(txt string) BodyOpt

Text opt

func XML

func XML(obj map[string]interface{}) BodyOpt

XML opt

type Client

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

Client struct

func New

func New(opts ...Opt) *Client

New client

func (*Client) Close added in v0.2.0

func (c *Client) Close()

Close Client release connection resource

func (*Client) Do

func (c *Client) Do(req *http.Request) (resp *http.Response, err error)

Do reimpl

func (*Client) DoRequest added in v0.2.0

func (c *Client) DoRequest(ctx context.Context, req *http.Request) (resp *http.Response, err error)

DoRequest do request with context

func (*Client) Execute

func (c *Client) Execute(ctx context.Context) error

Execute client

func (*Client) ExecuteRequest added in v0.2.0

func (c *Client) ExecuteRequest(ctx context.Context, req *http.Request) (err error)

ExecuteRequest do custom request with response processor

func (*Client) GetClient added in v0.2.1

func (c *Client) GetClient() *http.Client

GetClient get standard http.Client

func (*Client) GetTransport added in v0.2.1

func (c *Client) GetTransport() http.RoundTripper

GetTransport get standard http.RoundTripper Transport

type DialContext added in v0.2.0

type DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

DialContext dialer function

type DumpResponse

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

DumpResponse struct

func NewDumpResponse

func NewDumpResponse(opts ...DumpResponseOpt) *DumpResponse

NewDumpResponse new

func (*DumpResponse) Process

func (d *DumpResponse) Process(ctx context.Context, rsp *http.Response) error

Process of DumpResponse

type DumpResponseOpt added in v0.2.0

type DumpResponseOpt func(*DumpResponse)

DumpResponseOpt option

func Output added in v0.2.0

func Output(wr io.Writer) DumpResponseOpt

Output of DumpResponse

type Opt

type Opt func(*config)

Opt for client

func Credential added in v0.2.0

func Credential(cred *tls.Config) Opt

Credential for TLSConfig

func Dialer added in v0.2.0

func Dialer(dialer DialContext) Opt

Dialer opt

func ExecuteRetry added in v0.2.0

func ExecuteRetry(retry int) Opt

ExecuteRetry opt for client.Execute, only > 1

func HTTPClient added in v0.2.1

func HTTPClient(client *http.Client) Opt

HTTPClient opt, when this option is SET, All above option will ignore

func Keepalive added in v0.2.0

func Keepalive(keepalive time.Duration) Opt

Keepalive opt

func MaxConnsPerHost added in v0.2.0

func MaxConnsPerHost(max int) Opt

MaxConnsPerHost opt

func MaxIdleConnsPerHost added in v0.2.0

func MaxIdleConnsPerHost(max int) Opt

MaxIdleConnsPerHost opt

func Proxy added in v0.2.1

func Proxy(host string) Opt

Proxy opt

func Request

func Request(builder *RequestBuilder) Opt

Request opt

func Response

func Response(processor ResponseProcessor) Opt

Response opt

func Retry added in v0.1.1

func Retry(retry int) Opt

Retry opt for client.Do, only > 1

func Timeout added in v0.1.1

func Timeout(duration time.Duration) Opt

Timeout opt

func Transport

func Transport(transport http.RoundTripper) Opt

Transport opt, When this option is SET, Timeout/MaxConnsPerHost/MaxIdleConnsPerHost option will be IGNORED!!!

type ReqOpt

type ReqOpt func(*requestConfig)

ReqOpt opt

func BasicAuth added in v0.2.0

func BasicAuth(username string, password string) ReqOpt

BasicAuth opt

func Content

func Content(opts ...BodyOpt) ReqOpt

Content opt

func Cookie(cookie *http.Cookie) ReqOpt

Cookie opt

func Fragment

func Fragment(name string) ReqOpt

Fragment opt

func Header(name string, value string) ReqOpt

Header opt

func Method

func Method(method string) ReqOpt

Method opt

func Query

func Query(name string, value string) ReqOpt

Query opt

func URL

func URL(url string) ReqOpt

URL opt

type RequestBuilder

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

RequestBuilder struct

func NewRequestBuilder

func NewRequestBuilder(opts ...ReqOpt) *RequestBuilder

NewRequestBuilder new

func (*RequestBuilder) Clear added in v0.2.0

func (req *RequestBuilder) Clear()

Clear RequestBuilder

func (*RequestBuilder) Get added in v0.2.0

func (req *RequestBuilder) Get() (*http.Request, error)

Get http.Request

type ResponseProcessor

type ResponseProcessor interface {
	Process(context.Context, *http.Response) error
}

ResponseProcessor interface

type ResponseProcessorFunc added in v0.2.0

type ResponseProcessorFunc func(context.Context, *http.Response) error

ResponseProcessorFunc type

func (ResponseProcessorFunc) Process added in v0.2.0

func (f ResponseProcessorFunc) Process(ctx context.Context, rsp *http.Response) error

Process implemention of ResponseProcessor

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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