Deepbits Platform: Unlink a file from a project
This script demonstrates how to unlink a file from a project on the Deepbits platform using the PUT /project/{project_id} API endpoint. It removes the specified file (file_id) from the project (project_id) by updating the project's asset list.
The unlink_file_from_project function removes a file from a project by fetching the project name and the current list of file IDs associated with the project. It updates the project by excluding the specified file ID from the asset list.
Below is a demo script showing how to unlink a file (FILE_ID) from a project (PROJECT_ID). Adjust the placeholders , , and with your specific values for integration.
import requests
API_KEY = '<Your API key generated from app.deepbits.com>'
API_BASE = 'https://api.deepbits.com/api/v1'
default_headers = {"x-api-key": API_KEY }
FILE_ID = '<Your File ID>'
PROJECT_ID = '<Your Project ID>'
def main():
unlink_file_from_project(FILE_ID, PROJECT_ID)
def unlink_file_from_project(file_id, project_id):
try:
# Fetch project name
project_name = get_project_name(project_id)
if not project_name:
print(f"Failed to get project name for project ID {project_id}")
return None
# Fetch file ID list
file_id_list = get_file_id_list_by_project_id(project_id)
if file_id_list is None:
print(f"Failed to get file ID list for project ID {project_id}")
return None
# Remove file ID from the list if present
if file_id in file_id_list:
file_id_list.remove(file_id)
else:
print(f"File {file_id} is not linked to project {project_id}")
return None
response = requests.put(
f"{API_BASE}/project/{project_id}",
headers=default_headers,
json={
"name": project_name,
"assets": [
{
"assetType": "SBOMBuilder",
"assetIds": file_id_list
}
]
}
)
response.raise_for_status()
print(f"Successfully unlinked file {file_id} from project {project_id}")
return None
except requests.RequestException as err:
print(f"An error occurred while making the request: {err}")
return None
except ValueError as err:
print(f"An error occurred while processing the response: {err}")
return None
def get_project_name(project_id):
try:
resp = requests.get(f"{API_BASE}/project/{project_id}", headers=default_headers)
resp.raise_for_status()
return resp.json().get('data', {}).get('name')
except (requests.RequestException, ValueError, KeyError) as err:
print(f"An error occurred: {err}")
return None
def get_file_id_list_by_project_id(project_id):
try:
resp = requests.get(f"{API_BASE}/project/{project_id}/assets", headers=default_headers)
resp.raise_for_status()
data = resp.json().get('data', {})
docs = data.get('docs', [])
if docs:
return docs[0].get('assetIds', [])
else:
return []
except (requests.RequestException, ValueError, KeyError) as err:
print(f"An error occurred: {err}")
return []
if __name__ == "__main__":
main()
Updated about 2 months ago