net.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Helpers for downloading files from the network."""
  2. import os
  3. import httpx
  4. from . import console
  5. def _httpx_verify_kwarg() -> bool:
  6. """Get the value of the HTTPX verify keyword argument.
  7. Returns:
  8. True if SSL verification is enabled, False otherwise
  9. """
  10. ssl_no_verify = os.environ.get("SSL_NO_VERIFY", "").lower() in ["true", "1", "yes"]
  11. return not ssl_no_verify
  12. def get(url: str, **kwargs) -> httpx.Response:
  13. """Make an HTTP GET request.
  14. Args:
  15. url: The URL to request.
  16. **kwargs: Additional keyword arguments to pass to httpx.get.
  17. Returns:
  18. The response object.
  19. Raises:
  20. httpx.ConnectError: If the connection cannot be established.
  21. """
  22. kwargs.setdefault("verify", _httpx_verify_kwarg())
  23. try:
  24. return httpx.get(url, **kwargs)
  25. except httpx.ConnectError as err:
  26. if "CERTIFICATE_VERIFY_FAILED" in str(err):
  27. # If the error is a certificate verification error, recommend mitigating steps.
  28. console.error(
  29. f"Certificate verification failed for {url}. Set environment variable SSL_CERT_FILE to the "
  30. "path of the certificate file or SSL_NO_VERIFY=1 to disable verification."
  31. )
  32. raise