(client *http.Client, url, method string, timeout int)
| 54 | } |
| 55 | |
| 56 | func HandleRequestWithClient(client *http.Client, url, method string, timeout int) (int, []byte, error) { |
| 57 | defer func() { |
| 58 | if r := recover(); r != nil { |
| 59 | global.LOG.Errorf("handle request failed, error message: %v", r) |
| 60 | return |
| 61 | } |
| 62 | }() |
| 63 | |
| 64 | ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second) |
| 65 | defer cancel() |
| 66 | request, err := http.NewRequestWithContext(ctx, method, url, nil) |
| 67 | if err != nil { |
| 68 | return 0, nil, err |
| 69 | } |
| 70 | request.Header.Set("Content-Type", "application/json") |
| 71 | resp, err := client.Do(request) |
| 72 | if err != nil { |
| 73 | return 0, nil, err |
| 74 | } |
| 75 | defer resp.Body.Close() |
| 76 | if resp.StatusCode != http.StatusOK { |
| 77 | return 0, nil, errors.New(resp.Status) |
| 78 | } |
| 79 | body, err := io.ReadAll(resp.Body) |
| 80 | if err != nil { |
| 81 | return 0, nil, err |
| 82 | } |
| 83 | |
| 84 | return resp.StatusCode, body, nil |
| 85 | } |
| 86 | |
| 87 | type RequestResponse struct { |
| 88 | StatusCode int |
no test coverage detected