Commit
This commit is contained in:
54
utils/http.go
Normal file
54
utils/http.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"nCovTrack-Backend/global"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ()
|
||||
|
||||
func GetWhioutHeader(url string) string {
|
||||
return GetWithHeader(url, nil)
|
||||
}
|
||||
|
||||
func GetWithHeader(url string, header map[string]string) string {
|
||||
client, err := global.GetHttpClient("")
|
||||
if err != nil {
|
||||
var logParams []interface{}
|
||||
logParams = append(logParams, "err", err)
|
||||
InitLogError("httpClient", logParams)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, url, nil)
|
||||
for k, v := range header {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
startTime := time.Now()
|
||||
res, err := client.Do(req)
|
||||
cost := time.Since(startTime)
|
||||
var logParams []interface{}
|
||||
logParams = append(logParams,
|
||||
"reqest", req,
|
||||
"cost", cost.String(),
|
||||
)
|
||||
if err != nil {
|
||||
logParams = append(logParams, "errors", err)
|
||||
UtilLogError("requestUtil", logParams...)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if cost.Seconds() > 1 {
|
||||
logParams = append(logParams, "response", res)
|
||||
UtilLogWarn("requestUtil", logParams...)
|
||||
}
|
||||
UtilLogInfo("requestUtil", logParams...)
|
||||
|
||||
bodyBytes, _ := ioutil.ReadAll(res.Body)
|
||||
defer res.Body.Close()
|
||||
|
||||
return string(bodyBytes)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user