Skip to content

Commit ecdf270

Browse files
style: format code with Black
This commit fixes the style issues introduced in 1b12fb2 according to the output from Black. Details: None
1 parent 1b12fb2 commit ecdf270

17 files changed

Lines changed: 306 additions & 207 deletions

codeinsight_sdk/client.py

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,101 +8,116 @@
88

99
logger = logging.getLogger(__name__)
1010

11+
1112
class CodeInsightClient:
1213
"""Client for the code insight API."""
1314

14-
def __init__(self,
15-
base_url: str,
16-
api_token: str,
17-
timeout: int = 60,
18-
verify_ssl: bool = True,
19-
experimental: bool = False
20-
):
21-
15+
def __init__(
16+
self,
17+
base_url: str,
18+
api_token: str,
19+
timeout: int = 60,
20+
verify_ssl: bool = True,
21+
experimental: bool = False,
22+
):
2223
self.base_url = base_url
2324
self.api_url = f"{base_url}/codeinsight/api"
2425
self.__api_token = api_token
2526
self.__api_headers = {
26-
'Content-Type': 'application/json',
27+
"Content-Type": "application/json",
2728
"Authorization": "Bearer %s" % self.__api_token,
2829
"User-Agent": "codeinsight_sdk_python",
2930
}
3031
self.__timeout = timeout
3132
self.__verify_ssl = verify_ssl
3233
self.experimental_enabled = experimental
3334

34-
def request(self, method, url_part: str, params: dict = None, body: any = None, data: any = None, content_type: str = None):
35+
def request(
36+
self,
37+
method,
38+
url_part: str,
39+
params: dict = None,
40+
body: any = None,
41+
data: any = None,
42+
content_type: str = None,
43+
):
3544
url = f"{self.api_url}/{url_part}"
3645

3746
# Iterate over params and remove any that are None (Empty)
38-
if(params):
47+
if params:
3948
for k, v in list(params.items()):
4049
if v is None:
4150
del params[k]
4251

4352
# Update headers if content_type is specified
4453
headers = self.__api_headers
4554
if content_type:
46-
headers['Content-Type'] = content_type
55+
headers["Content-Type"] = content_type
4756

48-
response = requests.request(method, url,
49-
headers=self.__api_headers, params=params, json=body, data=data,
50-
timeout=self.__timeout, verify=self.__verify_ssl)
57+
response = requests.request(
58+
method,
59+
url,
60+
headers=self.__api_headers,
61+
params=params,
62+
json=body,
63+
data=data,
64+
timeout=self.__timeout,
65+
verify=self.__verify_ssl,
66+
)
5167

5268
if not response.ok:
53-
logger.error(f"Error: {response.status_code} - {response.reason}", exc_info=True)
69+
logger.error(
70+
f"Error: {response.status_code} - {response.reason}", exc_info=True
71+
)
5472
logger.error(response.text)
55-
raise CodeInsightError(response)
73+
raise CodeInsightError(response)
5674

5775
return response
5876

5977
@property
6078
def projects(self) -> ProjectHandler:
6179
return ProjectHandler(self)
62-
80+
6381
@property
6482
def reports(self) -> ReportHandler:
6583
return ReportHandler(self)
66-
84+
6785
@property
6886
def inventories(self):
6987
return InventoryHandler(self)
70-
88+
7189
@property
7290
def experimental(self) -> ExperimentalHandler:
7391
if self.experimental_enabled == False:
7492
raise CodeInsightError("Experimental API is not enabled for this instance")
7593
else:
7694
return ExperimentalHandler(self)
77-
78-
95+
7996
# Coming soon...?
8097

81-
8298
def vulnerabilites(self):
8399
raise NotImplementedError
84-
100+
85101
def users(self):
86102
raise NotImplementedError
87-
103+
88104
def licenses(self):
89105
raise NotImplementedError
90-
106+
91107
def tasks(self):
92108
raise NotImplementedError
93-
109+
94110
def rules(self):
95111
raise NotImplementedError
96-
112+
97113
def files(self):
98114
raise NotImplementedError
99-
115+
100116
def folders(self):
101117
raise NotImplementedError
102-
118+
103119
def jobs(self):
104120
raise NotImplementedError
105-
121+
106122
def components(self):
107123
raise NotImplementedError
108-

codeinsight_sdk/exceptions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
21
import requests
32
import json
43

5-
class GenericError(Exception): #pragma: no cover
4+
5+
class GenericError(Exception): # pragma: no cover
66
"""Generic error class, catch-all for most code insight API errors."""
7+
78
pass
89

9-
class NotYetImplementedError(Exception): #pragma: no cover
10+
11+
class NotYetImplementedError(Exception): # pragma: no cover
1012
"""Error class for API features that have not yet been implemented."""
13+
1114
pass
1215

16+
1317
class CodeInsightError(GenericError):
1418
"""Error class for code insight API errors."""
19+
1520
def __init__(self, response: requests.Response):
1621
try:
1722
resp = response.json()
1823
self.code = response.status_code
19-
self.message = resp['Error: ']
20-
self.arguments = resp['Arguments: ']
21-
self.error = resp['Key: ']
24+
self.message = resp["Error: "]
25+
self.arguments = resp["Arguments: "]
26+
self.error = resp["Key: "]
2227
self.add_note(f"Arguments: {self.arguments}")
2328
super().__init__("Error: %s - %s" % (self.code, self.message))
2429

codeinsight_sdk/experimental.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
11
from .handler import Handler
22
from .models import ProjectInventoryItem
33

4+
45
class ExperimentalHandler(Handler):
56
def __init__(self, client):
67
super().__init__(client)
7-
8+
89
def get(self):
910
# Do nothing, there is no get for this handler
1011
pass
11-
12-
def get_project_vulnerabilities(self, project_id:int) -> list[ProjectInventoryItem]:
12+
13+
def get_project_vulnerabilities(
14+
self, project_id: int
15+
) -> list[ProjectInventoryItem]:
1316
"""
1417
Get all vulnerabilities for a project.
15-
18+
1619
Args:
1720
project_id (int): The project id.
18-
21+
1922
Returns:
2023
dict: The vulnerabilities.
2124
"""
2225
# First we get the inventory summary for the project with vulnerability summary
2326
# Then we iterate over the inventory items and calling the inventory vulnerability endpoint for each item with a vulnerability
24-
inventory = self.client.projects.get_inventory_summary(project_id, vulnerabilitySummary=True)
27+
inventory = self.client.projects.get_inventory_summary(
28+
project_id, vulnerabilitySummary=True
29+
)
2530

2631
# Iterate over the inventory items, find which have vulnerabilities.
2732
item: ProjectInventoryItem
2833
vuln_items: list(ProjectInventoryItem) = []
2934
for item in inventory:
3035
if item.vulnerabilitySummary is None:
3136
continue
32-
37+
3338
# If the item has a vulnerability, get the vulnerability details for this item and append it
34-
if sum(item.vulnerabilitySummary[0]['CvssV3'].values()) > 0:
35-
36-
vul_detail = self.client.inventories.get_inventory_vulnerabilities(item.id)
39+
if sum(item.vulnerabilitySummary[0]["CvssV3"].values()) > 0:
40+
vul_detail = self.client.inventories.get_inventory_vulnerabilities(
41+
item.id
42+
)
3743
item.vulnerabilities = vul_detail
3844
vuln_items.append(item)
3945
else:
4046
# If the item has no vulnerabilities, skip it
4147
continue
4248

43-
return vuln_items
49+
return vuln_items

codeinsight_sdk/handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .models import Project, ProjectInventory, ProjectInventoryItem, Report
55
from .exceptions import CodeInsightError
66

7+
78
class Handler(abc.ABC):
89
def __init__(self, client):
910
self.client = client
@@ -12,4 +13,3 @@ def __init__(self, client):
1213
@abc.abstractmethod
1314
def get(self):
1415
pass
15-
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .inventory import InventoryHandler
22
from .project import ProjectHandler
3-
from .report import ReportHandler
3+
from .report import ReportHandler

codeinsight_sdk/handlers/inventory.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from ..models import ProjectInventoryItem, Vulnerability
22
from ..handler import Handler
33

4+
45
class InventoryHandler(Handler):
5-
""" Handles operations related to inventories."""
6+
"""Handles operations related to inventories."""
67

78
def __init__(self, client):
89
super().__init__(client)
910
self.cls = ProjectInventoryItem
10-
11+
1112
def get(self, inventoryId: int) -> list[ProjectInventoryItem]:
1213
"""
1314
Get an inventory item by id.
@@ -21,13 +22,13 @@ def get(self, inventoryId: int) -> list[ProjectInventoryItem]:
2122
path = f"inventories/{inventoryId}"
2223
resp = self.client.request("GET", url_part=path)
2324
inventory = []
24-
for inv_item in resp.json()['data']:
25+
for inv_item in resp.json()["data"]:
2526
inventory.append(ProjectInventoryItem.from_dict(inv_item))
2627
return inventory
27-
28-
def get_inventory_vulnerabilities(self, inventoryId: int,
29-
limit: int = 25,
30-
offset: int = 1) -> list[Vulnerability]:
28+
29+
def get_inventory_vulnerabilities(
30+
self, inventoryId: int, limit: int = 25, offset: int = 1
31+
) -> list[Vulnerability]:
3132
"""
3233
Get all vulnerabilities for an inventory item.
3334
@@ -44,7 +45,7 @@ def get_inventory_vulnerabilities(self, inventoryId: int,
4445
# TODO - Iterate pages
4546

4647
inventory_vuls: list(Vulnerability) = []
47-
for v in resp.json()['data']:
48+
for v in resp.json()["data"]:
4849
inventory_vuls.append(Vulnerability.from_dict(v))
4950

50-
return inventory_vuls
51+
return inventory_vuls

0 commit comments

Comments
 (0)