|
8 | 8 |
|
9 | 9 | logger = logging.getLogger(__name__) |
10 | 10 |
|
| 11 | + |
11 | 12 | class CodeInsightClient: |
12 | 13 | """Client for the code insight API.""" |
13 | 14 |
|
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 | + ): |
22 | 23 | self.base_url = base_url |
23 | 24 | self.api_url = f"{base_url}/codeinsight/api" |
24 | 25 | self.__api_token = api_token |
25 | 26 | self.__api_headers = { |
26 | | - 'Content-Type': 'application/json', |
| 27 | + "Content-Type": "application/json", |
27 | 28 | "Authorization": "Bearer %s" % self.__api_token, |
28 | 29 | "User-Agent": "codeinsight_sdk_python", |
29 | 30 | } |
30 | 31 | self.__timeout = timeout |
31 | 32 | self.__verify_ssl = verify_ssl |
32 | 33 | self.experimental_enabled = experimental |
33 | 34 |
|
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 | + ): |
35 | 44 | url = f"{self.api_url}/{url_part}" |
36 | 45 |
|
37 | 46 | # Iterate over params and remove any that are None (Empty) |
38 | | - if(params): |
| 47 | + if params: |
39 | 48 | for k, v in list(params.items()): |
40 | 49 | if v is None: |
41 | 50 | del params[k] |
42 | 51 |
|
43 | 52 | # Update headers if content_type is specified |
44 | 53 | headers = self.__api_headers |
45 | 54 | if content_type: |
46 | | - headers['Content-Type'] = content_type |
| 55 | + headers["Content-Type"] = content_type |
47 | 56 |
|
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 | + ) |
51 | 67 |
|
52 | 68 | 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 | + ) |
54 | 72 | logger.error(response.text) |
55 | | - raise CodeInsightError(response) |
| 73 | + raise CodeInsightError(response) |
56 | 74 |
|
57 | 75 | return response |
58 | 76 |
|
59 | 77 | @property |
60 | 78 | def projects(self) -> ProjectHandler: |
61 | 79 | return ProjectHandler(self) |
62 | | - |
| 80 | + |
63 | 81 | @property |
64 | 82 | def reports(self) -> ReportHandler: |
65 | 83 | return ReportHandler(self) |
66 | | - |
| 84 | + |
67 | 85 | @property |
68 | 86 | def inventories(self): |
69 | 87 | return InventoryHandler(self) |
70 | | - |
| 88 | + |
71 | 89 | @property |
72 | 90 | def experimental(self) -> ExperimentalHandler: |
73 | 91 | if self.experimental_enabled == False: |
74 | 92 | raise CodeInsightError("Experimental API is not enabled for this instance") |
75 | 93 | else: |
76 | 94 | return ExperimentalHandler(self) |
77 | | - |
78 | | - |
| 95 | + |
79 | 96 | # Coming soon...? |
80 | 97 |
|
81 | | - |
82 | 98 | def vulnerabilites(self): |
83 | 99 | raise NotImplementedError |
84 | | - |
| 100 | + |
85 | 101 | def users(self): |
86 | 102 | raise NotImplementedError |
87 | | - |
| 103 | + |
88 | 104 | def licenses(self): |
89 | 105 | raise NotImplementedError |
90 | | - |
| 106 | + |
91 | 107 | def tasks(self): |
92 | 108 | raise NotImplementedError |
93 | | - |
| 109 | + |
94 | 110 | def rules(self): |
95 | 111 | raise NotImplementedError |
96 | | - |
| 112 | + |
97 | 113 | def files(self): |
98 | 114 | raise NotImplementedError |
99 | | - |
| 115 | + |
100 | 116 | def folders(self): |
101 | 117 | raise NotImplementedError |
102 | | - |
| 118 | + |
103 | 119 | def jobs(self): |
104 | 120 | raise NotImplementedError |
105 | | - |
| 121 | + |
106 | 122 | def components(self): |
107 | 123 | raise NotImplementedError |
108 | | - |
|
0 commit comments