-
Notifications
You must be signed in to change notification settings - Fork 7
/
service.go
49 lines (46 loc) · 1.14 KB
/
service.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
package excel2json
import (
"fmt"
"github.com/gojektech/heimdall/v6/hystrix"
"io/ioutil"
"net/http"
"time"
)
// getFileUrl fetch file body with http client
// fileUrl string url
func getFileUrl(fileUrl string) ([]byte, error) {
var (
body []byte
res = new(http.Response)
err error
)
// Create a new hystrix-wrapped HTTP client with the command name, along with other required options
client := hystrix.NewClient(
hystrix.WithHTTPTimeout(5*time.Second),
hystrix.WithCommandName(fmt.Sprintf(`%s||%s`, fileUrl, "GetFileUrl")),
hystrix.WithHystrixTimeout(10*time.Second),
hystrix.WithMaxConcurrentRequests(30),
hystrix.WithErrorPercentThreshold(20),
)
// Create an http.Request instance
if res, err = client.Get(fileUrl, nil); err != nil {
return nil, err
}
if body, err = ioutil.ReadAll(res.Body); err != nil {
return nil, err
}
return body, nil
}
// getFilePath fetch file body with http client
// filePath string url
func getFilePath(filePath string) ([]byte, error) {
var (
body []byte
err error
)
// just pass the file name
if body, err = ioutil.ReadFile(filePath); err != nil {
return nil, err
}
return body, nil
}