|
| 1 | +package xhttp |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "net" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type Client struct { |
| 11 | + HttpClient *http.Client |
| 12 | + bodySize int // body size limit(MB), default is 10MB |
| 13 | +} |
| 14 | + |
| 15 | +func defaultClient() *Client { |
| 16 | + return &Client{ |
| 17 | + HttpClient: &http.Client{ |
| 18 | + Timeout: 60 * time.Second, |
| 19 | + Transport: &http.Transport{ |
| 20 | + Proxy: http.ProxyFromEnvironment, |
| 21 | + DialContext: defaultTransportDialContext(&net.Dialer{ |
| 22 | + Timeout: 30 * time.Second, |
| 23 | + KeepAlive: 30 * time.Second, |
| 24 | + }), |
| 25 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 26 | + MaxIdleConns: 100, |
| 27 | + IdleConnTimeout: 90 * time.Second, |
| 28 | + TLSHandshakeTimeout: 10 * time.Second, |
| 29 | + ExpectContinueTimeout: 1 * time.Second, |
| 30 | + DisableKeepAlives: true, |
| 31 | + ForceAttemptHTTP2: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + bodySize: 10, // default is 10MB |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// NewClient , default tls.Config{InsecureSkipVerify: true} |
| 39 | +func NewClient() (client *Client) { |
| 40 | + return defaultClient() |
| 41 | +} |
| 42 | + |
| 43 | +func (c *Client) SetTransport(transport *http.Transport) (client *Client) { |
| 44 | + c.HttpClient.Transport = transport |
| 45 | + return c |
| 46 | +} |
| 47 | + |
| 48 | +func (c *Client) SetTLSConfig(tlsCfg *tls.Config) (client *Client) { |
| 49 | + c.HttpClient.Transport.(*http.Transport).TLSClientConfig = tlsCfg |
| 50 | + return c |
| 51 | +} |
| 52 | + |
| 53 | +func (c *Client) SetTimeout(timeout time.Duration) (client *Client) { |
| 54 | + c.HttpClient.Timeout = timeout |
| 55 | + return c |
| 56 | +} |
| 57 | + |
| 58 | +// set body size (MB), default is 10MB |
| 59 | +func (c *Client) SetBodySize(sizeMB int) (client *Client) { |
| 60 | + c.bodySize = sizeMB |
| 61 | + return c |
| 62 | +} |
| 63 | + |
| 64 | +// typeStr is request type and response type |
| 65 | +// default is TypeJSON |
| 66 | +// first param is request type |
| 67 | +// second param is response data type |
| 68 | +func (c *Client) Req(typeStr ...string) *Request { |
| 69 | + var ( |
| 70 | + reqTp = TypeJSON // default |
| 71 | + resTp = ResTypeJSON // default |
| 72 | + tLen = len(typeStr) |
| 73 | + ) |
| 74 | + switch { |
| 75 | + case tLen == 1: |
| 76 | + tpp := typeStr[0] |
| 77 | + if _, ok := _ReqContentTypeMap[tpp]; ok { |
| 78 | + reqTp = tpp |
| 79 | + } |
| 80 | + case tLen > 1: |
| 81 | + // first param is request type |
| 82 | + tpp := typeStr[0] |
| 83 | + if _, ok := _ReqContentTypeMap[tpp]; ok { |
| 84 | + reqTp = tpp |
| 85 | + } |
| 86 | + // second param is response data type |
| 87 | + stpp := typeStr[1] |
| 88 | + if _, ok := _ResTypeMap[stpp]; ok { |
| 89 | + resTp = stpp |
| 90 | + } |
| 91 | + } |
| 92 | + if c == nil { |
| 93 | + c = defaultClient() |
| 94 | + } |
| 95 | + r := &Request{ |
| 96 | + client: c, |
| 97 | + Header: make(http.Header), |
| 98 | + requestType: reqTp, |
| 99 | + responseType: resTp, |
| 100 | + } |
| 101 | + r.Header.Set("Content-Type", _ReqContentTypeMap[reqTp]) |
| 102 | + return r |
| 103 | +} |
0 commit comments