(ep Endpoint, reqURL string, body []byte)
| 333 | } |
| 334 | |
| 335 | func execHTTP(ep Endpoint, reqURL string, body []byte) error { |
| 336 | var r io.Reader |
| 337 | if body != nil { |
| 338 | r = bytes.NewReader(body) |
| 339 | } |
| 340 | req, err := http.NewRequest(ep.Method, reqURL, r) |
| 341 | if err != nil { |
| 342 | return err |
| 343 | } |
| 344 | if body != nil { |
| 345 | req.Header.Set("Content-Type", "application/json") |
| 346 | } |
| 347 | client := &http.Client{Timeout: 30 * time.Second} |
| 348 | resp, err := client.Do(req) |
| 349 | if err != nil { |
| 350 | return err |
| 351 | } |
| 352 | defer resp.Body.Close() |
| 353 | |
| 354 | payload, err := io.ReadAll(resp.Body) |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 359 | if len(payload) == 0 { |
| 360 | return fmt.Errorf("%s %s failed: %s", ep.Method, ep.Path, resp.Status) |
| 361 | } |
| 362 | return fmt.Errorf("%s %s failed: %s: %s", ep.Method, ep.Path, resp.Status, strings.TrimSpace(string(payload))) |
| 363 | } |
| 364 | |
| 365 | printJSON(payload) |
| 366 | return nil |
| 367 | } |
| 368 | |
| 369 | func execWebsocket(ep Endpoint, reqURL string) error { |
| 370 | wsURL := strings.Replace(reqURL, "http://", "ws://", 1) |
no test coverage detected