Utility function for making HTTPs requests.
(logger, url, jobs_manager, download=False, timeout_attempt=0)
| 76 | |
| 77 | |
| 78 | def make_https_request(logger, url, jobs_manager, download=False, timeout_attempt=0): |
| 79 | """ |
| 80 | Utility function for making HTTPs requests. |
| 81 | """ |
| 82 | try: |
| 83 | req = requests_retry_session().get(url, timeout=120) |
| 84 | req.raise_for_status() |
| 85 | except requests.exceptions.ConnectionError as c_err: |
| 86 | logger.error("FATAL: Connection Error while fetching the cert list") |
| 87 | logger.error(str(c_err)) |
| 88 | jobs_manager.record_job_error() |
| 89 | exit(1) |
| 90 | except requests.exceptions.HTTPError as h_err: |
| 91 | logger.warning("HTTP Error while fetching the cert list") |
| 92 | logger.warning(str(h_err)) |
| 93 | return None |
| 94 | except requests.exceptions.RequestException as err: |
| 95 | # Allow to pass through to capture last index |
| 96 | logger.warning("HTTP Error while fetching the cert list") |
| 97 | logger.warning(str(err)) |
| 98 | return None |
| 99 | except Timeout as t_err: |
| 100 | if timeout_attempt == 0: |
| 101 | logger.warning("Timeout occurred. Attempting again...") |
| 102 | result = make_https_request( |
| 103 | logger, url, jobs_manager, download, timeout_attempt=1 |
| 104 | ) |
| 105 | return result |
| 106 | else: |
| 107 | logger.error("FATAL: Too many timeouts. Exiting") |
| 108 | jobs_manager.record_job_error() |
| 109 | exit(1) |
| 110 | except Exception as e: |
| 111 | logger.error("FATAL: UNKNOWN ERROR with the HTTP Request: " + str(e)) |
| 112 | jobs_manager.record_job_error() |
| 113 | exit(1) |
| 114 | |
| 115 | if req.status_code != 200: |
| 116 | logger.error("ERROR: Status code " + str(req.status_code)) |
| 117 | return None |
| 118 | |
| 119 | if download: |
| 120 | return req.content |
| 121 | |
| 122 | return req.text |
| 123 | |
| 124 | |
| 125 | def fetch_sth(logger, url, jobs_manager): |
no test coverage detected