22from typing import List
33
44from codeinsight_sdk .models import Project , ProjectInventory , ProjectInventoryItem
5+ from codeinsight_sdk .exceptions import CodeInsightError
56
67class Handler (abc .ABC ):
78 def __init__ (self , client , cls ):
@@ -11,9 +12,12 @@ def __init__(self, client, cls):
1112 @staticmethod
1213 def create (client , cls ):
1314 k = cls .__name__
14- handlers = {"Project" : ProjectHandler ,
15+ handlers = {"Project" : ProjectHandler ,
16+ "Report" : ReportHandler
1517 }
1618 handler = handlers .get (k )
19+ if handler is None :
20+ raise Exception (f"Handler not found for class '{ k } '" )
1721 return handler (client , cls )
1822
1923 @abc .abstractmethod
@@ -23,18 +27,34 @@ def get(self):
2327class ProjectHandler (Handler ):
2428 #Note API endpoints switch between projects and project...
2529 def all (self ) -> List [Project ]:
26- path = "projects"
27- resp = self .client .request ("GET" , url_part = path )
28- projects = []
29- for project_data in resp .json ()['data' ]:
30- projects .append (self .cls .from_dict (project_data ))
31- return projects
30+ """
31+ Retrieves all projects from the server.
32+
33+ Returns:
34+ A list of Project objects representing all the projects.
35+ """
36+
37+ path = "projects"
38+ resp = self .client .request ("GET" , url_part = path )
39+ projects = []
40+ for project_data in resp .json ()['data' ]:
41+ projects .append (self .cls .from_dict (project_data ))
42+ return projects
3243
33- def get (self , id :str ) -> Project :
34- path = f"projects/{ id } "
35- resp = self .client .request ("GET" , url_part = path )
36- project_data = resp .json ()['data' ]
37- return self .cls .from_dict (project_data )
44+ def get (self , id :int ) -> Project :
45+ """
46+ Retrieves a project by its ID.
47+
48+ Args:
49+ id (int): The ID of the project requested.
50+
51+ Returns:
52+ Project: The retrieved project.
53+ """
54+ path = f"projects/{ id } "
55+ resp = self .client .request ("GET" , url_part = path )
56+ project_data = resp .json ()['data' ]
57+ return self .cls .from_dict (project_data )
3858
3959 def get_id (self , projectName :str ) -> int :
4060 """
@@ -44,30 +64,69 @@ def get_id(self, projectName:str) -> int:
4464 projectName (str): The name of the project.
4565
4666 Returns:
47- str : The ID of the project.
67+ int : The ID of the project.
4868 """
4969 path = f"project/id"
5070 params = {"projectName" : projectName }
5171 resp = self .client .request ("GET" , url_part = path , params = params )
52- projectId = resp .json ()['Content' ]
72+ try :
73+ projectId = resp .json ()['Content: ' ] # Yes, the key is called 'Content: ' ...
74+ except KeyError :
75+ raise CodeInsightError (resp )
76+ #raise Exception(f"Content key not found in response: {resp.json()}")
5377 return projectId
5478
5579 def get_inventory_summary (self , project_id :int ) -> List [ProjectInventoryItem ]:
56- path = f"projects/{ project_id } /inventorySummary"
57- resp = self .client .request ("GET" , url_part = path )
58- inventory = []
59- for inv_item in resp .json ()['data' ]:
60- inventory .append (ProjectInventoryItem .from_dict (inv_item ))
61- return inventory
80+ """
81+ Retrieves the inventory summary for a specific project.
82+
83+ Args:
84+ project_id (int): The ID of the project.
85+
86+ Returns:
87+ List[ProjectInventoryItem]: A list of ProjectInventoryItem objects representing the inventory summary.
88+ """
89+
90+ path = f"projects/{ project_id } /inventorySummary"
91+ resp = self .client .request ("GET" , url_part = path )
92+ inventory = []
93+ for inv_item in resp .json ()['data' ]:
94+ inventory .append (ProjectInventoryItem .from_dict (inv_item ))
95+ return inventory
6296
6397 def get_inventory (self ,project_id :int ,
6498 skip_vulnerabilities : bool = False ,
6599 published :bool = True ,
100+ vendor :str = None ,
101+ product :str = None ,
102+ page_size : int = 100 ,
103+ page : int = 1 ,
104+ review_status : str = None ,
105+ alerts : str = None ,
106+ include_files : bool = True
66107 ) -> ProjectInventory :
67108 path = f"project/inventory/{ project_id } "
109+ #TODO: Add support for all parameters
68110 params = {"skipVulnerabilities" : skip_vulnerabilities }
69111 resp = self .client .request ("GET" , url_part = path , params = params )
70112 project_inventory = resp .json ()
71113 project_cls = ProjectInventory .from_dict (project_inventory )
72114 return project_cls
73115
116+
117+ class ReportHandler (Handler ):
118+ def get (self , id :int ):
119+ path = f"reports/{ id } "
120+ resp = self .client .request ("GET" , url_part = path )
121+ report_data = resp .json ()['data' ]
122+ report = self .cls .from_dict (report_data )
123+ return report
124+
125+ def all (self ):
126+ path = "reports"
127+ resp = self .client .request ("GET" , url_part = path )
128+ reports = []
129+ for report_data in resp .json ()['data' ]:
130+ reports .append (self .cls .from_dict (report_data ))
131+ return reports
132+
0 commit comments