-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
53 lines (45 loc) · 964 Bytes
/
do.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package httpclient
import (
"context"
"net/http"
"net/http/cookiejar"
"time"
)
func (c *Calling) reDoInit() *Calling {
c.response = nil
c.respBody = nil
c.timeExpend = nil
return c
}
func (c *Calling) ReDo(ctx ...context.Context) error {
return c.reDoInit().Do(ctx...)
}
func (c *Calling) Do(ctx ...context.Context) error {
if len(ctx) > 0 {
c.WithContext(ctx[0])
}
if c.Url() == nil {
return errorf("url has not set")
}
if len(c.Cookies()) > 0 {
jar, _ := cookiejar.New(nil)
jar.SetCookies(c.Url(), c.Cookies())
}
if c.Request() == nil {
request, err := http.NewRequestWithContext(c.Context(), c.Method(), c.Url().String(), c.Body())
if err != nil {
return errorf(err)
}
c.WithRequest(request)
}
c.fillOptions()
var err error
timeStart := time.Now()
c.response, err = c.Client().Do(c.Request())
timeExpend := time.Since(timeStart)
c.timeExpend = &timeExpend
if err != nil {
return errorf(err)
}
return nil
}