(req *http.Request, cfg *HTTPConf)
| 98 | } |
| 99 | |
| 100 | func parseResponse(req *http.Request, cfg *HTTPConf) (*simplejson.Json, error) { |
| 101 | errResponse, _ := simplejson.NewJson([]byte("{}")) |
| 102 | // TODO: 通过配置文件获取API超时时间 |
| 103 | client := &http.Client{Timeout: cfg.Timeout} |
| 104 | resp, err := client.Do(req) |
| 105 | if err != nil { |
| 106 | return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, err)) |
| 107 | } |
| 108 | |
| 109 | defer resp.Body.Close() |
| 110 | respBytes, err := io.ReadAll(resp.Body) |
| 111 | if err != nil { |
| 112 | return errResponse, errors.New(fmt.Sprintf("read (%s) body failed, (%v)", req.URL, err)) |
| 113 | } |
| 114 | if resp.StatusCode != http.StatusOK { |
| 115 | return errResponse, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, string(respBytes))) |
| 116 | } |
| 117 | |
| 118 | response, err := simplejson.NewJson(respBytes) |
| 119 | if err != nil { |
| 120 | return errResponse, errors.New(fmt.Sprintf("parse (%s) body failed, (%v)", req.URL, err)) |
| 121 | } |
| 122 | |
| 123 | optStatus := response.Get("OPT_STATUS").MustString() |
| 124 | if optStatus != "" && optStatus != SUCCESS { |
| 125 | description := response.Get("DESCRIPTION").MustString() |
| 126 | return response, errors.New(fmt.Sprintf("curl (%s) failed, (%v)", req.URL, description)) |
| 127 | } |
| 128 | return response, nil |
| 129 | } |
| 130 | |
| 131 | func CURLPostFormData(url, contentType string, body *bytes.Buffer, opts ...HTTPOption) (*simplejson.Json, error) { |
| 132 | cfg := &HTTPConf{} |
no test coverage detected