|
19 | 19 | import logging |
20 | 20 | import os |
21 | 21 | import sys |
| 22 | +import time |
22 | 23 | from json.decoder import JSONDecodeError |
23 | 24 | import requests |
24 | 25 | import uuid |
25 | 26 | import http.client as http_client |
26 | 27 |
|
27 | | -DEFAULT_URL = "https://osskb.org/api/scan/direct" |
| 28 | +DEFAULT_URL = "https://osskb.org/api/scan/direct" |
28 | 29 | SCANOSS_SCAN_URL = os.environ.get("SCANOSS_SCAN_URL") if os.environ.get("SCANOSS_SCAN_URL") else DEFAULT_URL |
29 | | -SCANOSS_API_KEY = os.environ.get("SCANOSS_API_KEY") if os.environ.get("SCANOSS_API_KEY") else '' |
| 30 | +SCANOSS_API_KEY = os.environ.get("SCANOSS_API_KEY") if os.environ.get("SCANOSS_API_KEY") else '' |
30 | 31 |
|
31 | 32 |
|
32 | 33 | class ScanossApi: |
@@ -82,22 +83,50 @@ def scan(self, wfp: str, context: str = None): |
82 | 83 | if context: |
83 | 84 | form_data['context'] = context |
84 | 85 | scan_files = {'file': ("%s.wfp" % uuid.uuid1().hex, wfp)} |
85 | | - r = None |
86 | | - try: |
87 | | - r = requests.post(self.url, files=scan_files, data=form_data, headers=self.headers, timeout=120) |
88 | | - except requests.Timeout: |
89 | | - raise Exception(f"ERROR: The SCANOSS API request timed out for {self.url}") |
| 86 | + r = None |
| 87 | + retry = 0 # Add some retry logic to cater for timeouts, etc. |
| 88 | + while retry <= 5: |
| 89 | + retry += 1 |
| 90 | + try: |
| 91 | + r = None |
| 92 | + r = requests.post(self.url, files=scan_files, data=form_data, headers=self.headers, timeout=120) |
| 93 | + except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e: |
| 94 | + if retry > 5: # Timed out 5 or more times, fail |
| 95 | + self.print_stderr(f'ERROR: Timeout/Connection Error POSTing data: {scan_files}') |
| 96 | + raise Exception(f"ERROR: The SCANOSS API request timed out for {self.url}") from e |
| 97 | + else: |
| 98 | + self.print_stderr(f'Warning: Timeout/Connection Error communicating with {self.url}. Retrying...') |
| 99 | + time.sleep(5) |
| 100 | + except Exception as e: |
| 101 | + self.print_stderr(f'ERROR: Exception POSTing data: {scan_files}') |
| 102 | + raise Exception(f"ERROR: The SCANOSS API request failed for {self.url}") from e |
| 103 | + else: |
| 104 | + if not r: |
| 105 | + if retry > 5: # No response 5 or more times, fail |
| 106 | + raise Exception(f"ERROR: The SCANOSS API request response object is empty for {self.url}") |
| 107 | + else: |
| 108 | + self.print_stderr(f'Warning: No response received from {self.url}. Retrying...') |
| 109 | + time.sleep(5) |
| 110 | + elif r.status_code >= 400: |
| 111 | + if retry > 5: # No response 5 or more times, fail |
| 112 | + raise Exception(f"ERROR: The SCANOSS API returned the following error: HTTP {r.status_code}, {r.text}") |
| 113 | + else: |
| 114 | + self.print_stderr(f'Warning: Error response code {r.status_code} from {self.url}. Retrying...') |
| 115 | + time.sleep(5) |
| 116 | + else: |
| 117 | + retry = 6 |
| 118 | + break # Valid response, break out of the retry loop |
| 119 | + # End of while loop |
90 | 120 | if not r: |
91 | 121 | raise Exception(f"ERROR: The SCANOSS API request response object is empty for {self.url}") |
92 | | - if r.status_code >= 400: |
93 | | - raise Exception(f"ERROR: The SCANOSS API returned the following error: HTTP {r.status_code}, {r.text}") |
94 | 122 | try: |
95 | 123 | if 'xml' in self.scan_format: |
96 | 124 | return r.text |
97 | 125 | json_resp = r.json() |
98 | 126 | return json_resp |
99 | | - except JSONDecodeError: |
100 | | - self.print_stderr('The SCANOSS API returned an invalid JSON. Please look in bad_json.json') |
| 127 | + except (JSONDecodeError, Exception) as e: |
| 128 | + self.print_stderr(f'The SCANOSS API returned an invalid JSON: {e}') |
| 129 | + self.print_stderr(f'Ignoring result. Please look in "bad_json.json" for more details.') |
101 | 130 | with open('bad_json.json', 'w') as f: |
102 | 131 | f.write(r.text) |
103 | 132 | return None |
|
0 commit comments