httpclient

package module
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: May 4, 2021 License: MIT Imports: 18 Imported by: 3

README

httpclient

Build Status Go Report Card Coverage Status GoDoc

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

func MakeRequest added in v0.3.0

func MakeRequest(opts ...ReqOpt) (*http.Request, error)

MakeRequest make a http.Request

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 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 Reader added in v0.3.2

func Reader(rd io.Reader) BodyOpt

Reader opt

func Text

func Text(txt string) BodyOpt

Text opt

func XML

func XML(obj 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, req *http.Request, processor ResponseProcessor) error

Execute client

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 Debug added in v0.3.0

func Debug(flag bool) Opt

Debug opt

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 Retry added in v0.1.1

func Retry(retry int) Opt

Retry opt for client.Do, only > 1

func TLSConfig added in v0.3.1

func TLSConfig(cred *tls.Config) Opt

TLSConfig for tls

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 BearerAuth added in v0.4.3

func BearerAuth(token string) ReqOpt

BearerAuth opt

func BearerAuthFunc added in v0.4.3

func BearerAuthFunc(token TokenFunc) ReqOpt

BearerAuthFunc opt

func Content

func Content(opts ...BodyOpt) ReqOpt

Content opt

func Cookie(cookie *http.Cookie) ReqOpt

Cookie 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 SetURL added in v0.3.0

func SetURL(Url string) ReqOpt

URL opt

func URL

func URL(opts ...URLOpt) ReqOpt

URL opt

type RequestBuilder

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

RequestBuilder struct

func NewRequestBuilder

func NewRequestBuilder(opts ...ReqOpt) *RequestBuilder

NewRequestBuilder new

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

type TokenFunc added in v0.4.3

type TokenFunc func() string

type URLOpt added in v0.3.0

type URLOpt func(*url.URL)

func Fragment

func Fragment(name string) URLOpt

Fragment opt

func Host added in v0.3.0

func Host(host string) URLOpt

Host opt [ip:port]

func Scheme added in v0.4.2

func Scheme(scheme string) URLOpt

Scheme opt

func URI added in v0.3.0

func URI(uri string) URLOpt

URI opt

func User added in v0.3.0

func User(user string) URLOpt

User opt

func UserPassword added in v0.3.0

func UserPassword(username string, password string) URLOpt

UserPassword opt

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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