Skip to content

Commit b76a728

Browse files
committed
return refactoring
1 parent bbc4944 commit b76a728

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

cycode/cyclient/scan_client.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,36 @@ def content_scan(self, scan_type: str, file_name: str, content: str, is_git_diff
3131
)
3232
return self.parse_scan_response(response)
3333

34+
def get_zipped_file_scan_url_path(self, scan_type: str) -> str:
35+
return f'{self.scan_config.get_service_name(scan_type)}/{self.SCAN_CONTROLLER_PATH}/zipped-file'
36+
3437
def zipped_file_scan(
3538
self, scan_type: str, zip_file: InMemoryZip, scan_id: str, scan_parameters: dict, is_git_diff: bool = False
3639
) -> models.ZippedFileScanResult:
37-
url_path = f'{self.scan_config.get_service_name(scan_type)}/{self.SCAN_CONTROLLER_PATH}/zipped-file'
3840
files = {'file': ('multiple_files_scan.zip', zip_file.read())}
3941

4042
response = self.scan_cycode_client.post(
41-
url_path=url_path,
43+
url_path=self.get_zipped_file_scan_url_path(scan_type),
4244
data={'scan_id': scan_id, 'is_git_diff': is_git_diff, 'scan_parameters': json.dumps(scan_parameters)},
4345
files=files,
4446
hide_response_content_log=self._hide_response_log,
4547
)
4648

4749
return self.parse_zipped_file_scan_response(response)
4850

51+
def get_zipped_file_scan_async_url_path(self, scan_type: str) -> str:
52+
async_scan_type = self.scan_config.get_async_scan_type(scan_type)
53+
async_entity_type = self.scan_config.get_async_entity_type(scan_type)
54+
55+
url_prefix = self.scan_config.get_scans_prefix()
56+
return f'{url_prefix}/{self.SCAN_CONTROLLER_PATH}/{async_scan_type}/{async_entity_type}'
57+
4958
def zipped_file_scan_async(
5059
self, zip_file: InMemoryZip, scan_type: str, scan_parameters: dict, is_git_diff: bool = False
5160
) -> models.ScanInitializationResponse:
52-
url_path = f'{self.scan_config.get_scans_prefix()}/{self.SCAN_CONTROLLER_PATH}/{scan_type}/repository'
5361
files = {'file': ('multiple_files_scan.zip', zip_file.read())}
5462
response = self.scan_cycode_client.post(
55-
url_path=url_path,
63+
url_path=self.get_zipped_file_scan_async_url_path(scan_type),
5664
data={'is_git_diff': is_git_diff, 'scan_parameters': json.dumps(scan_parameters)},
5765
files=files,
5866
)
@@ -80,13 +88,17 @@ def multiple_zipped_file_scan_async(
8088
)
8189
return models.ScanInitializationResponseSchema().load(response.json())
8290

91+
def get_scan_details_path(self, scan_id: str) -> str:
92+
return f'{self.scan_config.get_scans_prefix()}/{self.SCAN_CONTROLLER_PATH}/{scan_id}'
93+
8394
def get_scan_details(self, scan_id: str) -> models.ScanDetailsResponse:
84-
url_path = f'{self.scan_config.get_scans_prefix()}/{self.SCAN_CONTROLLER_PATH}/{scan_id}'
85-
response = self.scan_cycode_client.get(url_path=url_path)
95+
response = self.scan_cycode_client.get(url_path=self.get_scan_details_path(scan_id))
8696
return models.ScanDetailsResponseSchema().load(response.json())
8797

98+
def get_scan_detections_path(self) -> str:
99+
return f'{self.scan_config.get_detections_prefix()}/{self.DETECTIONS_SERVICE_CONTROLLER_PATH}'
100+
88101
def get_scan_detections(self, scan_id: str) -> List[dict]:
89-
url_path = f'{self.scan_config.get_detections_prefix()}/{self.DETECTIONS_SERVICE_CONTROLLER_PATH}'
90102
params = {'scan_id': scan_id}
91103

92104
page_size = 200
@@ -100,7 +112,9 @@ def get_scan_detections(self, scan_id: str) -> List[dict]:
100112
params['page_number'] = page_number
101113

102114
response = self.scan_cycode_client.get(
103-
url_path=url_path, params=params, hide_response_content_log=self._hide_response_log
115+
url_path=self.get_scan_detections_path(),
116+
params=params,
117+
hide_response_content_log=self._hide_response_log,
104118
).json()
105119
detections.extend(response)
106120

@@ -109,9 +123,13 @@ def get_scan_detections(self, scan_id: str) -> List[dict]:
109123

110124
return detections
111125

126+
def get_get_scan_detections_count_path(self) -> str:
127+
return f'{self.scan_config.get_detections_prefix()}/{self.DETECTIONS_SERVICE_CONTROLLER_PATH}/count'
128+
112129
def get_scan_detections_count(self, scan_id: str) -> int:
113-
url_path = f'{self.scan_config.get_detections_prefix()}/{self.DETECTIONS_SERVICE_CONTROLLER_PATH}/count'
114-
response = self.scan_cycode_client.get(url_path=url_path, params={'scan_id': scan_id})
130+
response = self.scan_cycode_client.get(
131+
url_path=self.get_get_scan_detections_count_path(), params={'scan_id': scan_id}
132+
)
115133
return response.json().get('count', 0)
116134

117135
def commit_range_zipped_file_scan(
@@ -126,9 +144,11 @@ def commit_range_zipped_file_scan(
126144
)
127145
return self.parse_zipped_file_scan_response(response)
128146

147+
def get_report_scan_status_path(self, scan_type: str, scan_id: str) -> str:
148+
return f'{self.scan_config.get_service_name(scan_type)}/{self.SCAN_CONTROLLER_PATH}/{scan_id}/status'
149+
129150
def report_scan_status(self, scan_type: str, scan_id: str, scan_status: dict) -> None:
130-
url_path = f'{self.scan_config.get_service_name(scan_type)}/{self.SCAN_CONTROLLER_PATH}/{scan_id}/status'
131-
self.scan_cycode_client.post(url_path=url_path, body=scan_status)
151+
self.scan_cycode_client.post(url_path=self.get_report_scan_status_path(scan_type, scan_id), body=scan_status)
132152

133153
@staticmethod
134154
def parse_scan_response(response: Response) -> models.ScanResult:
@@ -140,6 +160,7 @@ def parse_zipped_file_scan_response(response: Response) -> models.ZippedFileScan
140160

141161
@staticmethod
142162
def get_service_name(scan_type: str) -> Optional[str]:
163+
# TODO(MarshalX): get_service_name should be removed from ScanClient? Because it exists in ScanConfig
143164
if scan_type == 'secret':
144165
return 'secret'
145166
if scan_type == 'iac':

cycode/cyclient/scan_config_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ class ScanConfigBase(ABC):
66
def get_service_name(self, scan_type: str) -> str:
77
...
88

9+
@staticmethod
10+
def get_async_scan_type(scan_type: str) -> str:
11+
if scan_type == 'secret':
12+
return 'Secrets'
13+
if scan_type == 'iac':
14+
return 'InfraConfiguration'
15+
16+
return scan_type.upper()
17+
18+
@staticmethod
19+
def get_async_entity_type(_: str) -> str:
20+
# we are migrating to "zippedfile" entity type. will be used later
21+
return 'repository'
22+
923
@abstractmethod
1024
def get_scans_prefix(self) -> str:
1125
...

tests/cyclient/test_scan_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def zip_scan_resources(scan_type: str, scan_client: ScanClient) -> Tuple[str, In
2828

2929
def get_zipped_file_scan_url(scan_type: str, scan_client: ScanClient) -> str:
3030
api_url = scan_client.scan_cycode_client.api_url
31-
# TODO(MarshalX): create method in the scan client to build this url
32-
service_url = f'{api_url}/{scan_client.scan_config.get_service_name(scan_type)}'
33-
return f'{service_url}/{scan_client.SCAN_CONTROLLER_PATH}/zipped-file'
31+
service_url = scan_client.get_zipped_file_scan_url_path(scan_type)
32+
return f'{api_url}/{service_url}'
3433

3534

3635
def get_test_zip_file(scan_type: str) -> InMemoryZip:

0 commit comments

Comments
 (0)