Skip to content

Instantly share code, notes, and snippets.

@TomerAdmon
Last active June 7, 2024 12:17
Show Gist options
  • Save TomerAdmon/8b171527dd9e6a6e63735672f9032777 to your computer and use it in GitHub Desktop.
Save TomerAdmon/8b171527dd9e6a6e63735672f9032777 to your computer and use it in GitHub Desktop.
import requests
import time
base_url = "https://api.zafran.io/api/v2/"
key = ""
def export_init():
request_url = base_url + "findings/export"
headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/json"
}
response = requests.post(request_url, headers=headers)
if response.status_code != 200:
print(f"POST request failed with status code: {response.status_code}, details: {response.text}")
exit(1)
print(f"Export proccess started. Follow {response.json()["export_id"]} for more information")
return response.json()["export_id"]
def get_export_status(export_id):
request_url = base_url + "findings/export/status/" + export_id
headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/json"
}
response = requests.get(request_url, headers=headers)
if response.status_code != 200:
print(f"GET request failed with status code: {response.status_code}, details: {response.text}")
return response.json()["status"]
def wait_for_success(url, timeout=720):
start_time = time.time()
while True:
if time.time() - start_time > timeout:
print("Timeout reached. Exiting.")
break
response = get_export_status(url)
if response == "completed":
break
else:
print("Export in progress...")
time.sleep(60)
def get_exported_file(export_id):
request_url = base_url + "findings/export/" + export_id
headers = {
"Authorization": "Bearer " + key,
"Content-Type": "application/json"
}
response = requests.get(request_url, headers=headers)
if response.status_code == 200:
# Write the content of the response to a file
with open(export_id + '.gz', 'wb') as f:
f.write(response.content)
print("Findings were exported successfully.")
export_id = export_init()
wait_for_success(export_id)
get_exported_file(export_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment