-
Notifications
You must be signed in to change notification settings - Fork 267
/
curl.go
125 lines (108 loc) · 3.44 KB
/
curl.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package common
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
simplejson "github.com/bitly/go-simplejson"
httpcommon "github.com/deepflowio/deepflow/server/controller/http/common"
)
// 功能:获取用于API调用的IP地址
func GetCURLIP(ip string) string {
// IPV6地址在API调用时需要增加[]
if strings.Contains(ip, ":") && !strings.HasPrefix(ip, "[") {
return "[" + ip + "]"
}
return ip
}
type HeaderOption func(req *http.Request)
func WithHeader(key, value string) HeaderOption {
return func(req *http.Request) {
req.Header.Set(key, value)
}
}
func WithORGHeader(value string) HeaderOption {
return func(req *http.Request) {
req.Header.Set(HEADER_KEY_X_ORG_ID, value)
}
}
// 功能:调用其他模块API并获取返回结果
func CURLPerform(method string, url string, body map[string]interface{}, options ...HeaderOption) (*simplejson.Json, error) {
log.Debugf("curl perform: %s %s %+v", method, url, body)
errResponse, _ := simplejson.NewJson([]byte("{}"))
// TODO: 通过配置文件获取API超时时间
client := &http.Client{Timeout: time.Second * 30}
bodyStr, _ := json.Marshal(&body)
req, err := http.NewRequest(method, url, bytes.NewReader(bodyStr))
if err != nil {
log.Error(err)
return errResponse, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/plain")
req.Header.Set("X-User-Id", "1")
req.Header.Set("X-User-Type", "1")
for _, option := range options {
option(req)
}
resp, err := client.Do(req)
if err != nil {
log.Errorf("curl: %s failed, (%v)", url, err)
return errResponse, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errMsg := fmt.Sprintf("curl: %s failed, response detail: %+v", url, resp)
if respBytes, err := ioutil.ReadAll(resp.Body); err == nil {
if response, err := simplejson.NewJson(respBytes); err == nil {
description := response.Get("DESCRIPTION").MustString()
if description != "" {
errMsg += fmt.Sprintf(", description: %s", description)
}
}
}
log.Error(errMsg)
return errResponse, errors.New(errMsg)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("read (%s) body failed, (%v)", url, err)
return errResponse, err
}
response, err := simplejson.NewJson(respBytes)
if err != nil {
log.Errorf("parse (%s) body failed, (%v)", url, err)
return errResponse, err
}
optStatus := response.Get("OPT_STATUS").MustString()
if optStatus != "" && optStatus != httpcommon.SUCCESS {
description := response.Get("DESCRIPTION").MustString()
log.Errorf("curl: %s failed, (%s)", url, description)
e := httpcommon.ErrorFail
// PENDING used for api /v1/rpmod/
if optStatus == "PENDING" {
e = httpcommon.ErrorPending
}
return errResponse, fmt.Errorf("%w, %s", e, description)
}
return response, nil
}