Skip to content

Commit

Permalink
Read response HTTP status when doing API Upload before trying to pars…
Browse files Browse the repository at this point in the history
…e JSON
  • Loading branch information
cristianp-fossid committed Jul 4, 2024
1 parent f7811b7 commit f53715d
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions workbench-agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def upload_files(self, scan_code: str, path: str, enable_chunk_upload: bool = Tr
# Use chunked upload for files bigger than size_limit
# First delete possible existing files because chunk uploading works by appending existing file on disk.
self.remove_uploaded_content(filename, scan_code)
print(f"Uploading...")
headers = {
"FOSSID-SCAN-CODE": scan_code_base64,
"FOSSID-FILE-NAME": filename_base64,
Expand All @@ -121,11 +122,25 @@ def upload_files(self, scan_code: str, path: str, enable_chunk_upload: bool = Tr
auth=(self.api_user, self.api_token),
timeout=1800,
)
try:
resp.json()
except:
print(f"Failed to decode json {resp.text}")
print(traceback.print_exc())
# Retrieve the HTTP status code
status_code = resp.status_code
print(f"HTTP Status Code: {status_code}")

# Check if the request was successful (status code 200)
if status_code == 200:
# Parse the JSON response
try:
resp.json()
except:
print(f"Failed to decode json {resp.text}")
print(traceback.print_exc())
sys.exit(1)
else:
print(f"Request failed with status code {status_code}")
reason = resp.reason
print(f"Reason: {reason}")
response_text = resp.text
print(f"Response Text: {response_text}")
sys.exit(1)
except IOError:
# Error opening file
Expand All @@ -147,11 +162,25 @@ def upload_files(self, scan_code: str, path: str, enable_chunk_upload: bool = Tr
auth=(self.api_user, self.api_token),
timeout=1800,
)
try:
resp.json()
except:
print(f"Failed to decode json {resp.text}")
print(traceback.print_exc())
# Retrieve the HTTP status code
status_code = resp.status_code
print(f"HTTP Status Code: {status_code}")

# Check if the request was successful (status code 200)
if status_code == 200:
# Parse the JSON response
try:
resp.json()
except:
print(f"Failed to decode json {resp.text}")
print(traceback.print_exc())
sys.exit(1)
else:
print(f"Request failed with status code {status_code}")
reason = resp.reason
print(f"Reason: {reason}")
response_text = resp.text
print(f"Response Text: {response_text}")
sys.exit(1)
except IOError:
# Error opening file
Expand Down

0 comments on commit f53715d

Please sign in to comment.