-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathhttp.go
53 lines (49 loc) · 1.21 KB
/
http.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 storage
import (
"errors"
"github.com/xfyun/aiges/conf"
"github.com/xfyun/aiges/frame"
aigesUtils "github.com/xfyun/aiges/utils"
"io/ioutil"
"net/http"
)
// 封装http协议,用于与外部业务数据交互的上传下载;
func HttpDownload(url string) (data []byte, code int, err error) {
defer func() {
aigesUtils.CommonLogger.Debugw("resource http download ",
"url", url, "dataLength", len(data), "code", code, "err ", err)
}()
endFlag := make(chan bool)
for i := 0; i < conf.HttpRetry; i++ {
go func() {
defer func() { endFlag <- true }()
var resp *http.Response
resp, err = http.Get(url)
if err != nil {
code = frame.AigesErrorHttpReq
err = errors.New("http download fail with" + err.Error())
return
} else if resp.StatusCode != 200 {
code = frame.AigesErrorHttpFail
err = errors.New("http download fail with " + resp.Status)
_ = resp.Body.Close()
return
}
data, err = ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
code = frame.AigesErrorHttpInvalidData
err = errors.New("http download fail with" + err.Error())
return
}
}()
select {
case <-endFlag:
if code != 0 {
continue
}
return
}
}
return
}