net.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Helpers for downloading files from the network."""
  2. import httpx
  3. from ..config import environment
  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. return not environment.SSL_NO_VERIFY.get()
  11. def get(url: str, **kwargs) -> httpx.Response:
  12. """Make an HTTP GET request.
  13. Args:
  14. url: The URL to request.
  15. **kwargs: Additional keyword arguments to pass to httpx.get.
  16. Returns:
  17. The response object.
  18. Raises:
  19. httpx.ConnectError: If the connection cannot be established.
  20. """
  21. kwargs.setdefault("verify", _httpx_verify_kwarg())
  22. try:
  23. return httpx.get(url, **kwargs)
  24. except httpx.ConnectError as err:
  25. if "CERTIFICATE_VERIFY_FAILED" in str(err):
  26. # If the error is a certificate verification error, recommend mitigating steps.
  27. console.error(
  28. f"Certificate verification failed for {url}. Set environment variable SSL_CERT_FILE to the "
  29. "path of the certificate file or SSL_NO_VERIFY=1 to disable verification."
  30. )
  31. raise