(ctx context.Context, method string, endpoint string, query url.Values, body io.Reader, headers map[string]string, httpClient *http.Client)
| 516 | } |
| 517 | |
| 518 | func fetchWebSearchAPI(ctx context.Context, method string, endpoint string, query url.Values, body io.Reader, headers map[string]string, httpClient *http.Client) ([]byte, error) { |
| 519 | parsedURL, err := url.Parse(endpoint) |
| 520 | if err != nil { |
| 521 | return nil, err |
| 522 | } |
| 523 | |
| 524 | searchQuery := parsedURL.Query() |
| 525 | for key, values := range query { |
| 526 | for _, value := range values { |
| 527 | searchQuery.Add(key, value) |
| 528 | } |
| 529 | } |
| 530 | parsedURL.RawQuery = searchQuery.Encode() |
| 531 | |
| 532 | req, err := http.NewRequestWithContext(ctx, method, parsedURL.String(), body) |
| 533 | if err != nil { |
| 534 | return nil, err |
| 535 | } |
| 536 | req.Header.Set("User-Agent", webSearchUserAgent) |
| 537 | req.Header.Set("Accept", "application/json") |
| 538 | for key, value := range headers { |
| 539 | req.Header.Set(key, value) |
| 540 | } |
| 541 | |
| 542 | resp, err := httpClient.Do(req) |
| 543 | if err != nil { |
| 544 | return nil, err |
| 545 | } |
| 546 | defer resp.Body.Close() |
| 547 | |
| 548 | bodyBytes, err := io.ReadAll(io.LimitReader(resp.Body, webSearchMaxResponseSize+1)) |
| 549 | if err != nil { |
| 550 | return nil, err |
| 551 | } |
| 552 | if len(bodyBytes) > webSearchMaxResponseSize { |
| 553 | return nil, fmt.Errorf("response body exceeds %d bytes", webSearchMaxResponseSize) |
| 554 | } |
| 555 | if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { |
| 556 | message := strings.TrimSpace(string(bodyBytes)) |
| 557 | if message == "" { |
| 558 | message = resp.Status |
| 559 | } |
| 560 | return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, message) |
| 561 | } |
| 562 | |
| 563 | return bodyBytes, nil |
| 564 | } |
| 565 | |
| 566 | func fetchWebSearchHTML(ctx context.Context, endpoint string, query url.Values, httpClient *http.Client) (string, error) { |
| 567 | parsedURL, err := url.Parse(endpoint) |
no test coverage detected