功能:调用其他模块API并获取返回结果
(method string, url string, body map[string]interface{}, strBody string, opts ...HTTPOption)
| 64 | |
| 65 | // 功能:调用其他模块API并获取返回结果 |
| 66 | func CURLPerform(method string, url string, body map[string]interface{}, strBody string, opts ...HTTPOption) (*simplejson.Json, error) { |
| 67 | cfg := &HTTPConf{} |
| 68 | for _, opt := range opts { |
| 69 | opt(cfg) |
| 70 | } |
| 71 | |
| 72 | var err error |
| 73 | var contentType string |
| 74 | req := &http.Request{} |
| 75 | if strBody != "" { |
| 76 | reader := strings.NewReader(strBody) |
| 77 | req, err = http.NewRequest(method, url, reader) |
| 78 | contentType = "application/x-www-form-urlencoded" |
| 79 | } else { |
| 80 | bodyStr, _ := json.Marshal(&body) |
| 81 | reader := bytes.NewReader(bodyStr) |
| 82 | req, err = http.NewRequest(method, url, reader) |
| 83 | contentType = "application/json" |
| 84 | } |
| 85 | |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | if cfg.ORGID != 0 { |
| 90 | req.Header.Set(HEADER_KEY_X_ORG_ID, strconv.Itoa(cfg.ORGID)) |
| 91 | } |
| 92 | req.Header.Set("Content-Type", contentType) |
| 93 | req.Header.Set("Accept", "application/json, text/plain") |
| 94 | req.Header.Set("X-User-Id", "1") |
| 95 | req.Header.Set("X-User-Type", "1") |
| 96 | |
| 97 | return parseResponse(req, cfg) |
| 98 | } |
| 99 | |
| 100 | func parseResponse(req *http.Request, cfg *HTTPConf) (*simplejson.Json, error) { |
| 101 | errResponse, _ := simplejson.NewJson([]byte("{}")) |
no test coverage detected