ahvn.utils.basic.request_utils module

Network and request helper utilities (proxies, contexts).

class ahvn.utils.basic.request_utils.NetworkProxy(http_proxy=None, https_proxy=None, no_proxy=None, **kwargs)[源代码]

基类:object

A robust context manager for temporarily setting proxy environment variables.

This implementation properly handles: - Both uppercase and lowercase proxy environment variables - Proper restoration of original values (including deletion when they didn't exist) - Empty string handling (removes proxy variables instead of setting to empty) - NO_PROXY support for bypassing proxy for specific hosts

参数:
  • http_proxy (str | None)

  • https_proxy (str | None)

  • no_proxy (str | None)

PROXY_VARS = ['HTTP_PROXY', 'http_proxy', 'HTTPS_PROXY', 'https_proxy', 'NO_PROXY', 'no_proxy']
__init__(http_proxy=None, https_proxy=None, no_proxy=None, **kwargs)[源代码]

Initialize the NetworkProxy with optional proxy settings.

参数:
  • http_proxy (Optional[str]) -- The HTTP proxy URL. If empty empty string, HTTP proxy will be disabled. If None, the existing setting will be preserved.

  • https_proxy (Optional[str]) -- The HTTPS proxy URL. If empty empty string, HTTPS proxy will be disabled. If None, the existing setting will be preserved.

  • no_proxy (Optional[str]) -- Comma-separated list of hosts to bypass proxy for. If empty empty string, no_proxy will be disabled. If None, the existing setting will be preserved.

  • **kwargs -- Additional keyword arguments for future extensions.

__enter__()[源代码]

Enter the context manager, saving current proxy settings and applying new ones.

__exit__(exc_type, exc_val, exc_tb)[源代码]

Exit the context manager, restoring the original proxy settings.

ahvn.utils.basic.request_utils.google_download(file_id, path, http_proxy=None, https_proxy=None, *args, **kwargs)[源代码]

Download a file from Google Drive using its file ID. The file must be publicly accessible.

参数:
  • file_id (str) -- The Google Drive file ID.

  • path (str) -- The local path to save the downloaded file.

  • http_proxy (Optional[str]) -- HTTP proxy URL. If empty string, disables HTTP proxy.

  • https_proxy (Optional[str]) -- HTTPS proxy URL. If empty string, disables HTTPS proxy.

  • *args -- Additional positional arguments to pass to gdown.download.

  • **kwargs -- Additional keyword arguments to pass to gdown.download.

返回:

The path to the downloaded file, or None if download failed.

返回类型:

str