ghttp

package
v0.0.0-...-8506bae Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 25 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Client a HTTPClient, all request shared one http.Client object, keep cookies, keepalive etc.
	Client = NewHTTPClient()
)

Functions

func Close

func Close()

func CloseResponse

func CloseResponse(resp *http.Response)

func Download

func Download(u string, timeout time.Duration, queryData, header map[string]string) (data []byte, resp *http.Response, err error)

func DownloadToFile

func DownloadToFile(u string, timeout time.Duration, queryData, header map[string]string, file string) (resp *http.Response, err error)

func DownloadToWriter

func DownloadToWriter(u string, timeout time.Duration, queryData, header map[string]string, writer io.Writer) (resp *http.Response, err error)

func Get

func Get(u string, timeout time.Duration, queryData, header map[string]string) (body []byte, code int, resp *http.Response, err error)

func GetContent

func GetContent(_url string, timeout time.Duration) []byte

func GetContentE

func GetContentE(_url string, timeout time.Duration) ([]byte, error)

func GetResponseBody

func GetResponseBody(resp *http.Response) []byte

func GetResponseBodyE

func GetResponseBodyE(resp *http.Response) ([]byte, error)

func IsFormMethod

func IsFormMethod(method string) bool

func IsFormRequest

func IsFormRequest(req *http.Request) bool

func NewGet

func NewGet(URL string, timeout time.Duration, queryData, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)

func NewGetWithContext

func NewGetWithContext(ctx context.Context, URL string, queryData, header map[string]string) (req *http.Request, err error)

func NewPost

func NewPost(URL string, timeout time.Duration, data, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)

func NewPostReaderWithContext

func NewPostReaderWithContext(ctx context.Context, URL string, r io.Reader, header map[string]string) (req *http.Request, err error)

func NewPostWithContext

func NewPostWithContext(ctx context.Context, URL string, data, header map[string]string) (req *http.Request, err error)

func NewRequest

func NewRequest(method, URL string, timeout time.Duration, data, header map[string]string) (req *http.Request, cancel context.CancelFunc, err error)

func NewRequestWithContext

func NewRequestWithContext(ctx context.Context, method, URL string, data, header map[string]string) (req *http.Request, err error)

func Post

func Post(u string, data map[string]string, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)

func PostOfReader

func PostOfReader(u string, r io.Reader, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)

func Upload

func Upload(u, fieldName, filename string, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)

func UploadOfReader

func UploadOfReader(u, fieldName string, filename string, reader io.ReadCloser, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)

Types

type AfterDoClientFunc

type AfterDoClientFunc func(req *http.Request, resp *http.Response, err error)

type AfterDoFunc

type AfterDoFunc func(resp *Response)

type BatchRequest

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

func NewBatchGet

func NewBatchGet(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)

func NewBatchPost

func NewBatchPost(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)

func NewBatchRequest

func NewBatchRequest(reqArr []*http.Request, client *http.Client) *BatchRequest

NewBatchRequest new a BatchRequest by []*http.Request.

func NewBatchURL

func NewBatchURL(client *http.Client, method string, urlArr []string, timeout time.Duration, data, header map[string]string) (tr *BatchRequest, err error)

NewBatchURL new a BatchRequest by url slice []string.

func (*BatchRequest) AppendAfterDo

func (s *BatchRequest) AppendAfterDo(afterDo AfterDoFunc) *BatchRequest

AppendAfterDo add a callback call after request sent.

func (*BatchRequest) AppendBeforeDo

func (s *BatchRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *BatchRequest

AppendBeforeDo add a callback call before request sent.

func (*BatchRequest) CheckErrorFunc

func (s *BatchRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *BatchRequest

CheckErrorFunc if returns an error, the request treat as fail.

func (*BatchRequest) Close

func (s *BatchRequest) Close() *BatchRequest

Close close all response body can context cancel func.

func (*BatchRequest) DoFunc

func (s *BatchRequest) DoFunc(doFunc func(idx int, req *http.Request) (*http.Response, error)) *BatchRequest

DoFunc sets a request sender.

func (*BatchRequest) Err

func (s *BatchRequest) Err() error

Err returns first error of fail requests.

func (*BatchRequest) ErrAll

func (s *BatchRequest) ErrAll() (errs []error)

ErrAll returns all errors of fail requests.

func (*BatchRequest) ErrorCount

func (s *BatchRequest) ErrorCount() int

ErrorCount returns count of fail requests.

func (*BatchRequest) Execute

func (s *BatchRequest) Execute() *BatchRequest

Execute batch send requests,

In default Execute will wait all request done. If you want to get the first success response,

using BatchRequest.WaitFirstSuccess().Execute(), Execute() will return immediately when get a success response.

If all requests are fail, Execute() return after all requests done.

func (*BatchRequest) Keepalive

func (s *BatchRequest) Keepalive(keepalive bool) *BatchRequest

Keepalive sets enable or disable for request keepalive

func (*BatchRequest) MaxTry

func (s *BatchRequest) MaxTry(maxTry int) *BatchRequest

MaxTry sets the max retry count when a request error occurred.

func (*BatchRequest) Pool

func (s *BatchRequest) Pool(pool *gpool.Pool) *BatchRequest

Pool sets a *gpool.Pool to execute request. In default goroutine will be used.

func (*BatchRequest) Resp

func (s *BatchRequest) Resp() *Response

Resp returns first success response

func (*BatchRequest) RespAll

func (s *BatchRequest) RespAll() (responseAll []*Response)

RespAll returns all request's response, if you want to get if the response is success, checking if Response.Err() is nil.

func (*BatchRequest) SetAfterDo

func (s *BatchRequest) SetAfterDo(afterDo AfterDoFunc) *BatchRequest

SetAfterDo sets callback call after request sent.

func (*BatchRequest) SetBeforeDo

func (s *BatchRequest) SetBeforeDo(beforeDo BeforeDoFunc) *BatchRequest

SetBeforeDo sets callback call before request sent.

func (*BatchRequest) Success

func (s *BatchRequest) Success() bool

Success returns requests at least one is success in waitFirstSuccess mode. returns all requests are success in non waitFirstSuccess mode.

func (*BatchRequest) WaitFirstSuccess

func (s *BatchRequest) WaitFirstSuccess() *BatchRequest

WaitFirstSuccess sets Execute() return immediately when get a success response.

type BeforeDoClientFunc

type BeforeDoClientFunc func(req *http.Request)

type BeforeDoFunc

type BeforeDoFunc func(idx int, req *http.Request)

type CookieJar

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

func NewCookieJar

func NewCookieJar() *CookieJar

func (*CookieJar) Cookies

func (s *CookieJar) Cookies(u *url.URL) []*http.Cookie

func (*CookieJar) SetCookies

func (s *CookieJar) SetCookies(u *url.URL, cookies []*http.Cookie)

type HTTPClient

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

HTTPClient do http get, post etc.

func NewHTTPClient

func NewHTTPClient() *HTTPClient

NewHTTPClient new a HTTPClient, all request shared one http.Client object, keep cookies, keepalive etc. Keepalive is enabled in default.

func (*HTTPClient) AppendAfterDo

func (s *HTTPClient) AppendAfterDo(afterDo AfterDoClientFunc) *HTTPClient

AppendAfterDo add a callback call after request sent.

func (*HTTPClient) AppendBeforeDo

func (s *HTTPClient) AppendBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient

AppendBeforeDo add a callback call before request sent.

func (*HTTPClient) Close

func (s *HTTPClient) Close()

func (*HTTPClient) Do

func (s *HTTPClient) Do(req *http.Request, timeout time.Duration) (resp *http.Response, err error)

func (*HTTPClient) Download

func (s *HTTPClient) Download(u string, timeout time.Duration, queryData, header map[string]string) (data []byte, resp *http.Response, err error)

Download gets url bytes contents.

func (*HTTPClient) DownloadToFile

func (s *HTTPClient) DownloadToFile(u string, timeout time.Duration, queryData, header map[string]string, file string) (resp *http.Response, err error)

DownloadToFile gets url bytes contents and save to the file.

func (*HTTPClient) DownloadToWriter

func (s *HTTPClient) DownloadToWriter(u string, timeout time.Duration, queryData, header map[string]string, writer io.Writer) (resp *http.Response, err error)

DownloadToWriter gets url bytes contents and copy to the writer at realtime.

func (*HTTPClient) Get

func (s *HTTPClient) Get(u string, timeout time.Duration, queryData, header map[string]string) (body []byte, code int, resp *http.Response, err error)

Get send a HTTP GET request, no header, just passive nil.

func (*HTTPClient) NewBatchGet

func (s *HTTPClient) NewBatchGet(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)

NewBatchGet new a batch get requests

func (*HTTPClient) NewBatchPost

func (s *HTTPClient) NewBatchPost(urlArr []string, timeout time.Duration, data, header map[string]string) (br *BatchRequest, err error)

NewBatchPost new a batch post requests

func (*HTTPClient) NewTriableGet

func (s *HTTPClient) NewTriableGet(URL string, maxTry int, timeout time.Duration, queryData, header map[string]string) (tr *TriableRequest, err error)

NewTriableGet new a triable request with max retry count

func (*HTTPClient) NewTriablePost

func (s *HTTPClient) NewTriablePost(URL string, maxTry int, timeout time.Duration, data, header map[string]string) (tr *TriableRequest, err error)

NewTriablePost new a triable request with max retry count

func (*HTTPClient) Post

func (s *HTTPClient) Post(u string, data map[string]string, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)

Post send an HTTP POST request, no header, just passive nil. data is form key value.

func (*HTTPClient) PostOfReader

func (s *HTTPClient) PostOfReader(u string, r io.Reader, timeout time.Duration, header map[string]string) (body []byte, code int, resp *http.Response, err error)

PostOfReader send a HTTP POST request, no header, just passive nil. data is an io.Reader.

func (*HTTPClient) ProxyUsed

func (s *HTTPClient) ProxyUsed() *url.URL

ProxyUsed returns the final proxy URL used by connect to target

func (*HTTPClient) SetAfterDo

func (s *HTTPClient) SetAfterDo(afterDo AfterDoClientFunc) *HTTPClient

SetAfterDo sets callback call after request sent.

func (*HTTPClient) SetBasicAuth

func (s *HTTPClient) SetBasicAuth(username, password string)

func (*HTTPClient) SetBeforeDo

func (s *HTTPClient) SetBeforeDo(beforeDo BeforeDoClientFunc) *HTTPClient

SetBeforeDo sets callback call before request sent.

func (*HTTPClient) SetClientCert

func (s *HTTPClient) SetClientCert(certPemBytes, keyBytes []byte) (err error)

SetClientCert sets client certificate send to https server, when https server requires client certificate.

func (*HTTPClient) SetConnWrap

func (s *HTTPClient) SetConnWrap(fn func(c net.Conn) (conn net.Conn, err error))

SetConnWrap called after net.DialTimeout, you can change the conn,return it, if no change just return it.

func (*HTTPClient) SetDNS

func (s *HTTPClient) SetDNS(dns ...string) (err error)

SetDNS sets a dns for resolve domain in url when send request.

func (*HTTPClient) SetDialer

func (s *HTTPClient) SetDialer(dialer func(network, address string, timeout time.Duration) (net.Conn, error))

func (*HTTPClient) SetHttpClientFactory

func (s *HTTPClient) SetHttpClientFactory(httpClientFactory func(r *http.Request) *http.Client)

SetHttpClientFactory sets a http.Client factory, the http.Client it's returned, will be used to send the processed http.Request. the argument is the processed http.Request, you can modify it in your any purpose, and the modified http.Request will be sent by the returned client.

func (*HTTPClient) SetKeepalive

func (s *HTTPClient) SetKeepalive(keepalive bool)

func (*HTTPClient) SetPinCert

func (s *HTTPClient) SetPinCert(pemBytes []byte) (err error)

SetPinCert sets https server's certificate.

func (*HTTPClient) SetPreHandler

func (s *HTTPClient) SetPreHandler(preHandler func(r *http.Request))

SetPreHandler handle the processed http.Request, you can modify it in your any purpose, and the modified http.Request will be sent by the returned client.

func (*HTTPClient) SetProxy

func (s *HTTPClient) SetProxy(proxyURL string) (err error)

SetProxy sets the proxies used for send request.

if SetProxyFromEnv sets true and SetProxy sets a proxies url, SetProxy will be working.

func (*HTTPClient) SetProxyFromEnv

func (s *HTTPClient) SetProxyFromEnv(set bool)

SetProxyFromEnv sets if using http_proxy or all_proxy from system environment to send request.

func (*HTTPClient) SetRootCaCerts

func (s *HTTPClient) SetRootCaCerts(caPemBytes ...[]byte) (err error)

SetRootCaCerts sets root certificates to checking https server's certificate.

func (*HTTPClient) Upload

func (s *HTTPClient) Upload(u, fieldName, filename string, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)

Upload uploads a file from a file `filename`, fieldName is the form filed name in form, filename is the value of filed `fieldName` and also the file to upload. data is the additional form data.

func (*HTTPClient) UploadOfReader

func (s *HTTPClient) UploadOfReader(u, fieldName string, filename string, reader io.ReadCloser, data map[string]string, timeout time.Duration, header map[string]string) (body string, resp *http.Response, err error)

UploadOfReader upload a file from a io.Reader `reader`, fieldName is the form filed name in form, filename is the value of filed `fieldName`. data is the additional form data.

type Response

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

func (*Response) Body

func (s *Response) Body() []byte

func (*Response) BodyE

func (s *Response) BodyE() ([]byte, error)

func (*Response) Close

func (s *Response) Close()

func (*Response) EndTime

func (s *Response) EndTime() time.Time

func (*Response) Err

func (s *Response) Err() error

func (*Response) Idx

func (s *Response) Idx() int

func (*Response) Request

func (s *Response) Request() *http.Request

func (*Response) StartTime

func (s *Response) StartTime() time.Time

func (*Response) UsedTime

func (s *Response) UsedTime() time.Duration

type TriableRequest

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

TriableRequest when request fail, retry to request with MaxTryCount.

func NewTriableGet

func NewTriableGet(URL string, maxTry int, timeout time.Duration, queryData, header map[string]string) (tr *TriableRequest, err error)

func NewTriablePost

func NewTriablePost(URL string, maxTry int, timeout time.Duration, data, header map[string]string) (tr *TriableRequest, err error)

func NewTriableRequest

func NewTriableRequest(client *http.Client, req *http.Request, maxTry int, timeout time.Duration) *TriableRequest

NewTriableRequest new a TriableRequest by *http.Request, maxTry is the max retry count when a request error occurred. If client is nil, default client will be used.

func NewTriableRequestByURL

func NewTriableRequestByURL(client *http.Client, method string, URL string, maxTry int,
	timeout time.Duration, data map[string]string, header map[string]string) (tr *TriableRequest, err error)

NewTriableRequestByURL new a TriableRequest by URL.

func (*TriableRequest) AfterDo

func (s *TriableRequest) AfterDo(afterDo AfterDoFunc) *TriableRequest

AfterDo add a callback call after request sent.

func (*TriableRequest) AppendBeforeDo

func (s *TriableRequest) AppendBeforeDo(beforeDo BeforeDoFunc) *TriableRequest

AppendBeforeDo add a callback call before request sent.

func (*TriableRequest) CheckErrorFunc

func (s *TriableRequest) CheckErrorFunc(checkErrorFunc func(idx int, req *http.Request, resp *http.Response) error) *TriableRequest

CheckErrorFunc if returns an error, the request treat as fail.

func (*TriableRequest) Close

func (s *TriableRequest) Close() *TriableRequest

Close close all response body can context cancel func.

func (*TriableRequest) DoFunc

func (s *TriableRequest) DoFunc(doFunc func(req *http.Request) (*http.Response, error)) *TriableRequest

DoFunc sets a request sender.

func (*TriableRequest) Err

func (s *TriableRequest) Err() error

Err returns first error of fail requests.

func (*TriableRequest) ErrAll

func (s *TriableRequest) ErrAll() []error

ErrAll returns all requests error.

func (*TriableRequest) Execute

func (s *TriableRequest) Execute() *Response

Execute send request with retrying ability.

func (*TriableRequest) Keepalive

func (s *TriableRequest) Keepalive(keepalive bool) *TriableRequest

Keepalive sets enable or disable for request keepalive

func (*TriableRequest) SetBeforeDo

func (s *TriableRequest) SetBeforeDo(beforeDo BeforeDoFunc) *TriableRequest

SetBeforeDo sets callback call before request sent.

Jump to

Keyboard shortcuts

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