"""Test helper to fetch Shelly config using aiohttp digest authentication."""

import asyncio

from aiohttp import ClientSession, DigestAuthMiddleware


async def fetch_with_digest_auth(url: str, username: str, password: str) -> str:
    """Fetch a URL using HTTP Digest Authentication."""
    # aiohttp supports digest auth via DigestAuth class
    digest_auth = DigestAuthMiddleware(login=username, password=password)
    async with (
        ClientSession() as session,
        session.get(url, middlewares=(digest_auth,)) as response,
    ):
        print(f"Status: {response.status}")
        return await response.text()


async def main() -> None:
    """Main function to run the fetch."""

    url = "http://192.168.1.93/rpc/Shelly.GetConfig"
    username = "admin"
    password = "Simo"

    try:
        content = await fetch_with_digest_auth(url, username, password)
        print("Response content:")
        print(content)
    except Exception as e:
        print(f"Error: {e}")


if __name__ == "__main__":
    asyncio.run(main())
