"""One diagnostic GET; set PROXY_URL, PROXY_USER and PROXY_PASSWORD privately."""
import asyncio
import os
import ssl
import sys

import httpx


async def main():
    proxy = httpx.Proxy(
        os.environ["PROXY_URL"],
        auth=(os.environ["PROXY_USER"], os.environ["PROXY_PASSWORD"]),
    )
    tls = ssl.create_default_context(cafile=os.environ.get("SSL_CERT_FILE"))
    async with httpx.AsyncClient(
        proxy=proxy,
        verify=tls,
        trust_env=False,
        follow_redirects=False,
        timeout=httpx.Timeout(20.0, connect=10.0, pool=5.0),
        limits=httpx.Limits(max_connections=2, max_keepalive_connections=2),
    ) as client:
        response = await client.get(os.environ.get("TARGET_URL", "https://example.com/"))
        print(f"HTTP {response.status_code}")
        response.raise_for_status()
        print(f"Received {len(response.content)} bytes")


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except Exception as error:
        # Exception strings can contain request URLs or credentials.
        print(f"Request failed: {type(error).__name__}", file=sys.stderr)
        sys.exit(1)
