Skip to content

mirascope.tools.web._httpx

HTTPXConfig

Bases: _ConfigurableToolConfig

Configuration for HTTPX requests

HTTPX

Bases: _BaseHTTPX

call

call() -> str

Make an HTTP request to the given URL using HTTPX.

Returns:

Name Type Description
str str

Response text if successful, error message if request fails

Source code in mirascope/tools/web/_httpx.py
def call(self) -> str:
    """
    Make an HTTP request to the given URL using HTTPX.

    Returns:
        str: Response text if successful, error message if request fails
    """
    try:
        # Configure timeout - None means no timeout
        timeout = (
            httpx.Timeout(self._get_config().timeout)
            if self._get_config().timeout is not None
            else None
        )

        # Make request using context manager for proper resource cleanup
        with httpx.Client(timeout=timeout) as client:
            response = client.request(
                method=self.method,
                url=self.url,
                params=self.params,
                json=self.json_,
                data=self.data,
                headers=self.headers,
                follow_redirects=self.follow_redirects,
            )
            response.raise_for_status()
            return response.text

    except httpx.RequestError as e:
        return f"Request error occurred: {str(e)}"
    except httpx.HTTPStatusError as e:
        return f"HTTP error occurred: {e.response.status_code} - {str(e)}"
    except Exception as e:
        return f"{type(e).__name__}: Failed to make request to {self.url}"

AsyncHTTPX

Bases: _BaseHTTPX

call async

call() -> str

Make an asynchronous HTTP request to the given URL using HTTPX.

Returns:

Name Type Description
str str

Response text if successful, error message if request fails

Source code in mirascope/tools/web/_httpx.py
async def call(self) -> str:
    """
    Make an asynchronous HTTP request to the given URL using HTTPX.

    Returns:
        str: Response text if successful, error message if request fails
    """
    try:
        # Configure timeout - None means no timeout
        timeout = (
            httpx.Timeout(self._get_config().timeout)
            if self._get_config().timeout is not None
            else None
        )

        # Make async request using async context manager
        async with httpx.AsyncClient(timeout=timeout) as client:
            response = await client.request(
                method=self.method,
                url=self.url,
                params=self.params,
                json=self.json_,
                data=self.data,
                headers=self.headers,
                follow_redirects=self.follow_redirects,
            )
            response.raise_for_status()
            return response.text

    except httpx.RequestError as e:
        return f"Request error occurred: {str(e)}"
    except httpx.HTTPStatusError as e:
        return f"HTTP error occurred: {e.response.status_code} - {str(e)}"
    except Exception as e:
        return f"{type(e).__name__}: Failed to make request to {self.url}"