Skip to content

Commit

Permalink
fixed some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuzhenfeng-finogeeks committed Oct 16, 2018
1 parent 545c4a5 commit a316a51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
59 changes: 21 additions & 38 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,41 @@ import (
"time"
)

func newDefaultOptions() Options {
return Options{
Method: "GET",
Header: map[string]string{
"Accept-Encoding": "gzip,deflate",
"Accept": "*/*",
},
Body: nil,
Redirect: "follow",
Timeout: 20 * time.Millisecond,
Compress: true,
Size: 0,
Agent: nil,
}
}
// Fetch function
// url: Absolute url
// op: Fetch options
func Fetch(url string, op Options) (Resp, error) {
defaultOp := newDefaultOptions()
defaultOp.Method = op.Method
defaultOp.Header = op.Header
defaultOp.Body = op.Body
defaultOp.Timeout = op.Timeout

// Fetch request url
func Fetch(url string, option Options) (Resp, error) {
defaultOptions := newDefaultOptions()
defaultOptions.Method = option.Method
defaultOptions.Header = option.Header
defaultOptions.Body = option.Body
defaultOptions.Redirect = option.Redirect
defaultOptions.Timeout = option.Timeout
defaultOptions.Compress = option.Compress
defaultOptions.Size = option.Size
defaultOptions.Agent = option.Agent

client := http.Client{
// create a new http client
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultOptions.Timeout,
KeepAlive: defaultOptions.Timeout,
Timeout: defaultOp.Timeout,
KeepAlive: defaultOp.Timeout,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: defaultOptions.Timeout,
TLSHandshakeTimeout: defaultOptions.Timeout,
ExpectContinueTimeout: defaultOptions.Timeout,
IdleConnTimeout: defaultOp.Timeout,
TLSHandshakeTimeout: defaultOp.Timeout,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: defaultOptions.Timeout,
Timeout: defaultOp.Timeout,
}

req, err := http.NewRequest(defaultOptions.Method, url, bytes.NewReader(defaultOptions.Body))
req, err := http.NewRequest(defaultOp.Method, url, bytes.NewReader(defaultOp.Body))
if err != nil {
return Resp{}, err
}
for k, v := range defaultOptions.Header {
// set header
for k, v := range defaultOp.Header {
req.Header.Set(k, v)
}

// send request
resp, err := client.Do(req)
if err != nil {
return Resp{}, err
Expand Down
31 changes: 21 additions & 10 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ package fetch

import "time"

// Options http client option
// Options http client options
// default option
// method: GET
// body: nil
// header: {"Accept-Encoding": "gzip,deflate", "Accept": "*/*"}
// timeout: 20s
type Options struct {
Method string
Body []byte
Header map[string]string
Redirect string
Method string
Body []byte
Header map[string]string
Timeout time.Duration
}

Follow int
Timeout time.Duration
Compress bool
Size int
Agent interface{}
func newDefaultOptions() Options {
return Options{
Method: "GET",
Header: map[string]string{
"Accept-Encoding": "gzip,deflate",
"Accept": "*/*",
},
Body: nil,
Timeout: 20 * time.Millisecond,
}
}
5 changes: 3 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ type Resp struct {
StatusCode int
}

// BindJSON bind s
// BindJSON convert body to s
// s can be a map or a struct
func (resp Resp) BindJSON(s interface{}) error {
if err := json.Unmarshal(resp.Body, &s); err != nil {
return err
}
return nil
}

// ToString convert to string
// ToString convert response body to string
func (resp Resp) ToString() string {
return string(resp.Body)
}

0 comments on commit a316a51

Please sign in to comment.