|
6 | 6 |
|
7 | 7 | import os |
8 | 8 | import json |
| 9 | +from typing import Optional |
9 | 10 | from pathlib import Path |
10 | 11 |
|
11 | 12 | import requests |
|
22 | 23 | if ELOG_PROXY_URL: |
23 | 24 | os.environ["HTTP_PROXY"] = ELOG_PROXY_URL |
24 | 25 | os.environ["HTTPS_PROXY"] = ELOG_PROXY_URL |
| 26 | + logger.info(f"ELOG client configured to use proxy: {ELOG_PROXY_URL}") |
| 27 | + |
| 28 | + |
| 29 | +def test_proxy_connection() -> tuple[bool, Optional[str]]: |
| 30 | + """ |
| 31 | + Tests proxy connectivity if a proxy is configured. |
| 32 | +
|
| 33 | + :return: A tuple of (success, error_message). If successful, error_message is None. |
| 34 | + If failed, error_message contains a detailed description of the failure. |
| 35 | + """ |
| 36 | + if not ELOG_PROXY_URL: |
| 37 | + # No proxy configured, consider this a success |
| 38 | + return True, None |
| 39 | + |
| 40 | + if not ELOG_API_URL: |
| 41 | + return False, "ELOG API URL is not configured. Cannot test proxy connection." |
| 42 | + |
| 43 | + error_msg = ( |
| 44 | + f"Failed to connect through proxy {ELOG_PROXY_URL}. " |
| 45 | + f"Please check your network connection and proxy configuration." |
| 46 | + ) |
25 | 47 |
|
26 | | - # Test proxy connectivity |
27 | 48 | try: |
28 | | - test_response = requests.head(ELOG_API_URL, timeout=5) |
29 | | - logger.info(f"ELOG client using proxy: {ELOG_PROXY_URL} - Connection test successful") |
| 49 | + requests.head(ELOG_API_URL, timeout=2) |
| 50 | + logger.info(f"Proxy connection test successful: {ELOG_PROXY_URL}") |
| 51 | + return True, None |
30 | 52 | except requests.exceptions.ProxyError as e: |
31 | | - logger.error(f"Proxy connection failed: {ELOG_PROXY_URL} is not accessible - {e}") |
| 53 | + logger.error(f"Proxy connection test failed: {e}") |
| 54 | + return False, error_msg |
32 | 55 | except requests.exceptions.ConnectionError as e: |
33 | | - logger.error( |
34 | | - f"""Connection failed through proxy {ELOG_PROXY_URL}. |
35 | | - Check network connectivity and proxy configuration - {e}""" |
36 | | - ) |
| 56 | + logger.error(f"Proxy connection test failed: {e}") |
| 57 | + return False, error_msg |
37 | 58 | except requests.exceptions.Timeout as e: |
38 | | - logger.error( |
39 | | - f"Connection timeout through proxy {ELOG_PROXY_URL}.The proxy or server may be slow or unresponsive - {e}" |
| 59 | + error_msg = ( |
| 60 | + f"Connection timeout through proxy {ELOG_PROXY_URL}. " f"The proxy or server may be slow or unresponsive." |
40 | 61 | ) |
| 62 | + logger.error(f"Proxy connection test failed: {e}") |
| 63 | + return False, error_msg |
41 | 64 | except requests.exceptions.RequestException as e: |
42 | | - logger.error(f"Proxy connection test failed for {ELOG_PROXY_URL}: {e}") |
| 65 | + logger.error(f"Proxy connection test failed: {e}") |
| 66 | + return False, error_msg |
43 | 67 |
|
44 | 68 |
|
45 | 69 | def get_user() -> tuple[int, dict | Exception]: |
|
0 commit comments