File size: 1,085 Bytes
c36efba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import httpx
from colorama import Fore, Style  # type: ignore[import]
from langchain_core.tools import tool

HTTP_HEADERS = {
    "User-Agent": (
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
        "AppleWebKit/537.36 (KHTML, like Gecko) "
        "Chrome/120.0.0.0 Safari/537.36"
    )
}


@tool
def http_get(url: str) -> str:
    """Send an HTTP GET request to the given URL and return the response text.

    Useful for fetching web pages, API responses, or any text-based content
    from the internet.

    Args:
        url: The URL to send the GET request to.

    Returns:
        The response body as text, or an error message on failure.
    """
    try:
        print(f"{Fore.CYAN}[HTTP GET] {url}{Style.RESET_ALL}")
        resp = httpx.get(url, headers=HTTP_HEADERS, follow_redirects=True, timeout=60)
        resp.raise_for_status()
        print(f"{Fore.CYAN}[HTTP GET] Status: {resp.status_code} | "
              f"Length: {len(resp.text)} chars{Style.RESET_ALL}")
        return resp.text
    except Exception as e:
        return f"Error fetching URL: {e}"