(url, method string, timeout int)
| 170 | } |
| 171 | |
| 172 | func HandleRequest(url, method string, timeout int) (int, []byte, error) { |
| 173 | defer func() { |
| 174 | if r := recover(); r != nil { |
| 175 | global.LOG.Errorf("handle request failed, error message: %v", r) |
| 176 | return |
| 177 | } |
| 178 | }() |
| 179 | |
| 180 | transport := loadRequestTransport() |
| 181 | client := http.Client{Timeout: time.Duration(timeout) * time.Second, Transport: transport} |
| 182 | ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) |
| 183 | defer cancel() |
| 184 | request, err := http.NewRequestWithContext(ctx, method, url, nil) |
| 185 | if err != nil { |
| 186 | return 0, nil, err |
| 187 | } |
| 188 | request.Header.Set("Content-Type", "application/json") |
| 189 | resp, err := client.Do(request) |
| 190 | if err != nil { |
| 191 | return 0, nil, err |
| 192 | } |
| 193 | defer resp.Body.Close() |
| 194 | if resp.StatusCode != http.StatusOK { |
| 195 | return 0, nil, errors.New(resp.Status) |
| 196 | } |
| 197 | body, err := io.ReadAll(resp.Body) |
| 198 | if err != nil { |
| 199 | return 0, nil, err |
| 200 | } |
| 201 | |
| 202 | return resp.StatusCode, body, nil |
| 203 | } |
| 204 | |
| 205 | func loadRequestTransport() *http.Transport { |
| 206 | return &http.Transport{ |
no test coverage detected