fix lint temp

This commit is contained in:
holooooo
2021-05-23 23:01:01 +08:00
parent 4c4fdf496e
commit 84fa2ccd9a
7 changed files with 49 additions and 45 deletions

View File

@@ -9,7 +9,7 @@ httplib is an libs help you to curl remote url.
you can use Get to crawl data.
import "github.com/beego/beego/v2/client/httplib"
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
// error
@@ -39,7 +39,7 @@ Example:
// GET
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
// POST
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)

View File

@@ -22,7 +22,7 @@ import (
)
type ClientOption func(client *Client)
type BeegoHttpRequestOption func(request *BeegoHTTPRequest)
type BeegoHTTPRequestOption func(request *BeegoHTTPRequest)
// WithEnableCookie will enable cookie in all subsequent request
func WithEnableCookie(enable bool) ClientOption {
@@ -83,28 +83,28 @@ func WithEnableGzip(enable bool) ClientOption {
// BeegoHttpRequestOption
// WithTimeout sets connect time out and read-write time out for BeegoRequest.
func WithTimeout(connectTimeout, readWriteTimeout time.Duration) BeegoHttpRequestOption {
func WithTimeout(connectTimeout, readWriteTimeout time.Duration) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.SetTimeout(connectTimeout, readWriteTimeout)
}
}
// WithHeader adds header item string in request.
func WithHeader(key, value string) BeegoHttpRequestOption {
func WithHeader(key, value string) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.Header(key, value)
}
}
// WithCookie adds a cookie to the request.
func WithCookie(cookie *http.Cookie) BeegoHttpRequestOption {
func WithCookie(cookie *http.Cookie) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.Header("Cookie", cookie.String())
}
}
// Withtokenfactory adds a custom function to set Authorization
func WithTokenFactory(tokenFactory func() string) BeegoHttpRequestOption {
func WithTokenFactory(tokenFactory func() string) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
t := tokenFactory()
@@ -113,7 +113,7 @@ func WithTokenFactory(tokenFactory func() string) BeegoHttpRequestOption {
}
// WithBasicAuth adds a custom function to set basic auth
func WithBasicAuth(basicAuth func() (string, string)) BeegoHttpRequestOption {
func WithBasicAuth(basicAuth func() (string, string)) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
username, password := basicAuth()
request.SetBasicAuth(username, password)
@@ -121,21 +121,21 @@ func WithBasicAuth(basicAuth func() (string, string)) BeegoHttpRequestOption {
}
// WithFilters will use the filter as the invocation filters
func WithFilters(fcs ...FilterChain) BeegoHttpRequestOption {
func WithFilters(fcs ...FilterChain) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.SetFilters(fcs...)
}
}
// WithContentType adds ContentType in header
func WithContentType(contentType string) BeegoHttpRequestOption {
func WithContentType(contentType string) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.Header(contentTypeKey, contentType)
}
}
// WithParam adds query param in to request.
func WithParam(key, value string) BeegoHttpRequestOption {
func WithParam(key, value string) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.Param(key, value)
}
@@ -145,7 +145,7 @@ func WithParam(key, value string) BeegoHttpRequestOption {
// default is 0 (never retry)
// -1 retry indefinitely (forever)
// Other numbers specify the exact retry amount
func WithRetry(times int, delay time.Duration) BeegoHttpRequestOption {
func WithRetry(times int, delay time.Duration) BeegoHTTPRequestOption {
return func(request *BeegoHTTPRequest) {
request.Retries(times)
request.RetryDelay(delay)

View File

@@ -25,29 +25,29 @@ import (
type Client struct {
Name string
Endpoint string
CommonOpts []BeegoHttpRequestOption
CommonOpts []BeegoHTTPRequestOption
Setting BeegoHTTPSettings
}
// HttpResponseCarrier If value implement HttpResponseCarrier. http.Response will pass to SetHttpResponse
type HttpResponseCarrier interface {
// HTTPResponseCarrier If value implement HTTPResponseCarrier. http.Response will pass to SetHttpResponse
type HTTPResponseCarrier interface {
SetHttpResponse(resp *http.Response)
}
// HttpBodyCarrier If value implement HttpBodyCarrier. http.Response.Body will pass to SetReader
type HttpBodyCarrier interface {
// HTTPBodyCarrier If value implement HTTPBodyCarrier. http.Response.Body will pass to SetReader
type HTTPBodyCarrier interface {
SetReader(r io.ReadCloser)
}
// HttpBytesCarrier If value implement HttpBytesCarrier.
// HTTPBytesCarrier If value implement HTTPBytesCarrier.
// All the byte in http.Response.Body will pass to SetBytes
type HttpBytesCarrier interface {
type HTTPBytesCarrier interface {
SetBytes(bytes []byte)
}
// HttpStatusCarrier If value implement HttpStatusCarrier. http.Response.StatusCode will pass to SetStatusCode
type HttpStatusCarrier interface {
// HTTPStatusCarrier If value implement HTTPStatusCarrier. http.Response.StatusCode will pass to SetStatusCode
type HTTPStatusCarrier interface {
SetStatusCode(status int)
}
@@ -70,7 +70,7 @@ func NewClient(name string, endpoint string, opts ...ClientOption) (*Client, err
return res, nil
}
func (c *Client) customReq(req *BeegoHTTPRequest, opts []BeegoHttpRequestOption) {
func (c *Client) customReq(req *BeegoHTTPRequest, opts []BeegoHTTPRequestOption) {
req.Setting(c.Setting)
opts = append(c.CommonOpts, opts...)
for _, o := range opts {
@@ -98,7 +98,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
return err
}
if carrier, ok := value.(HttpResponseCarrier); ok {
if carrier, ok := value.(HTTPResponseCarrier); ok {
b, err := req.Bytes()
if err != nil {
return err
@@ -106,7 +106,7 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
resp.Body = ioutil.NopCloser(bytes.NewReader(b))
carrier.SetHttpResponse(resp)
}
if carrier, ok := value.(HttpBodyCarrier); ok {
if carrier, ok := value.(HTTPBodyCarrier); ok {
b, err := req.Bytes()
if err != nil {
return err
@@ -114,14 +114,14 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
reader := ioutil.NopCloser(bytes.NewReader(b))
carrier.SetReader(reader)
}
if carrier, ok := value.(HttpBytesCarrier); ok {
if carrier, ok := value.(HTTPBytesCarrier); ok {
b, err := req.Bytes()
if err != nil {
return err
}
carrier.SetBytes(b)
}
if carrier, ok := value.(HttpStatusCarrier); ok {
if carrier, ok := value.(HTTPStatusCarrier); ok {
carrier.SetStatusCode(resp.StatusCode)
}
if carrier, ok := value.(HttpHeadersCarrier); ok {
@@ -131,14 +131,14 @@ func (c *Client) handleCarrier(value interface{}, req *BeegoHTTPRequest) error {
}
// Get Send a GET request and try to give its result value
func (c *Client) Get(value interface{}, path string, opts ...BeegoHttpRequestOption) error {
func (c *Client) Get(value interface{}, path string, opts ...BeegoHTTPRequestOption) error {
req := Get(c.Endpoint + path)
c.customReq(req, opts)
return c.handleResponse(value, req)
}
// Post Send a POST request and try to give its result value
func (c *Client) Post(value interface{}, path string, body interface{}, opts ...BeegoHttpRequestOption) error {
func (c *Client) Post(value interface{}, path string, body interface{}, opts ...BeegoHTTPRequestOption) error {
req := Post(c.Endpoint + path)
c.customReq(req, opts)
if body != nil {
@@ -148,7 +148,7 @@ func (c *Client) Post(value interface{}, path string, body interface{}, opts ...
}
// Put Send a Put request and try to give its result value
func (c *Client) Put(value interface{}, path string, body interface{}, opts ...BeegoHttpRequestOption) error {
func (c *Client) Put(value interface{}, path string, body interface{}, opts ...BeegoHTTPRequestOption) error {
req := Put(c.Endpoint + path)
c.customReq(req, opts)
if body != nil {
@@ -158,14 +158,14 @@ func (c *Client) Put(value interface{}, path string, body interface{}, opts ...B
}
// Delete Send a Delete request and try to give its result value
func (c *Client) Delete(value interface{}, path string, opts ...BeegoHttpRequestOption) error {
func (c *Client) Delete(value interface{}, path string, opts ...BeegoHTTPRequestOption) error {
req := Delete(c.Endpoint + path)
c.customReq(req, opts)
return c.handleResponse(value, req)
}
// Head Send a Head request and try to give its result value
func (c *Client) Head(value interface{}, path string, opts ...BeegoHttpRequestOption) error {
func (c *Client) Head(value interface{}, path string, opts ...BeegoHTTPRequestOption) error {
req := Head(c.Endpoint + path)
c.customReq(req, opts)
return c.handleResponse(value, req)

View File

@@ -124,7 +124,6 @@ type BeegoHTTPRequest struct {
setting BeegoHTTPSettings
resp *http.Response
body []byte
dump []byte
}
// GetRequest returns the request object
@@ -199,7 +198,7 @@ func (b *BeegoHTTPRequest) SetHost(host string) *BeegoHTTPRequest {
// SetProtocolVersion sets the protocol version for incoming requests.
// Client requests always use HTTP/1.1
func (b *BeegoHTTPRequest) SetProtocolVersion(vers string) *BeegoHTTPRequest {
if len(vers) == 0 {
if vers == "" {
vers = "HTTP/1.1"
}
@@ -511,18 +510,16 @@ func (b *BeegoHTTPRequest) buildTrans() http.RoundTripper {
DialContext: TimeoutDialerCtx(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout),
MaxIdleConnsPerHost: 100,
}
} else {
} else if t, ok := trans.(*http.Transport); ok {
// if b.transport is *http.Transport then set the settings.
if t, ok := trans.(*http.Transport); ok {
if t.TLSClientConfig == nil {
t.TLSClientConfig = b.setting.TLSClientConfig
}
if t.Proxy == nil {
t.Proxy = b.setting.Proxy
}
if t.DialContext == nil {
t.DialContext = TimeoutDialerCtx(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)
}
if t.TLSClientConfig == nil {
t.TLSClientConfig = b.setting.TLSClientConfig
}
if t.Proxy == nil {
t.Proxy = b.setting.Proxy
}
if t.DialContext == nil {
t.DialContext = TimeoutDialerCtx(b.setting.ConnectTimeout, b.setting.ReadWriteTimeout)
}
}
return trans

View File

@@ -57,7 +57,7 @@ func NewSimpleCondition(path string, opts ...simpleConditionOption) *SimpleCondi
}
func (sc *SimpleCondition) Match(ctx context.Context, req *httplib.BeegoHTTPRequest) bool {
res := true
var res bool
if len(sc.path) > 0 {
res = sc.matchPath(ctx, req)
} else if len(sc.pathReg) > 0 {