MCPcopy
hub / github.com/the-open-agent/openagent / fetchWebSearchAPI

Function fetchWebSearchAPI

tool/web_search.go:518–564  ·  view source on GitHub ↗
(ctx context.Context, method string, endpoint string, query url.Values, body io.Reader, headers map[string]string, httpClient *http.Client)

Source from the content-addressed store, hash-verified

516}
517
518func 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
566func fetchWebSearchHTML(ctx context.Context, endpoint string, query url.Values, httpClient *http.Client) (string, error) {
567 parsedURL, err := url.Parse(endpoint)

Callers 4

runGoogleSearchFunction · 0.85
fetchBaiduSearchFunction · 0.85
runGoogleImageSearchFunction · 0.85
runDuckDuckGoImageSearchFunction · 0.85

Calls 3

QueryMethod · 0.65
StringMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected