Skip to content

Commit 04dbef2

Browse files
refactor(TKT-9082): add environment attribute to Alert and Incident classes
1 parent ee45213 commit 04dbef2

3 files changed

Lines changed: 42 additions & 21 deletions

File tree

intezer_sdk/_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,10 @@ def get_alert_by_alert_id(self, alert_id: str, environment: Optional[str] = None
726726

727727
return data_response['result'], data_response['status']
728728

729-
def get_incident_by_id(self, incident_id: str) -> dict:
729+
def get_incident_by_id(self, incident_id: str, environment: Optional[str] = None) -> dict:
730730
data = dict(incident_id=incident_id)
731+
if environment:
732+
data['environment'] = environment
731733
response = self.api.request_with_refresh_expired_access_token(method='GET',
732734
path='/incidents/get-by-id',
733735
data=data)

intezer_sdk/alerts.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class Alert:
270270

271271
def __init__(self,
272272
alert_id: Optional[str] = None,
273+
environment: Optional[str] = None,
273274
alert_stream: Optional[BinaryIO] = None,
274275
api: IntezerApiClient = None):
275276
"""
@@ -278,6 +279,7 @@ def __init__(self,
278279
instance with the given alert id.
279280
280281
:param alert_id: The alert id.
282+
:param environment: The environment of the alert.
281283
:param api: The API connection to Intezer.
282284
"""
283285
if alert_stream and alert_id:
@@ -294,6 +296,7 @@ def __init__(self,
294296
else:
295297
self.alert_id: str = alert_id
296298

299+
self.environment = environment
297300
self._intezer_api_client = api
298301
self._api = IntezerApi(api or get_global_api())
299302
self._report: Optional[Dict] = None
@@ -320,12 +323,14 @@ def check_status(self) -> AlertStatusCode:
320323
321324
"""
322325
try:
323-
alert, status = self._api.get_alert_by_alert_id(alert_id=self.alert_id)
326+
alert, status = self._api.get_alert_by_alert_id(alert_id=self.alert_id, environment=self.environment)
324327
except requests.HTTPError:
325328
self.status = AlertStatusCode.NOT_FOUND
326329
raise errors.AlertNotFoundError(self.alert_id)
327330

328331
self._report = alert
332+
if not self.environment:
333+
self.environment = alert['environment']
329334

330335
if status in (AlertStatusCode.IN_PROGRESS.value, AlertStatusCode.QUEUED.value):
331336
self.status = AlertStatusCode.IN_PROGRESS
@@ -359,6 +364,7 @@ def result(self) -> dict:
359364
@classmethod
360365
def from_id(cls,
361366
alert_id: str,
367+
environment: Optional[str] = None,
362368
api: IntezerApiClient = None,
363369
fetch_scans: bool = False,
364370
wait: bool = False,
@@ -367,6 +373,7 @@ def from_id(cls,
367373
Create a new Alert instance, and fetch the alert data from the Intezer Analyze API.
368374
369375
:param alert_id: The alert id.
376+
:param environment: The environment of the alert.
370377
:param api: The API connection to Intezer.
371378
:param fetch_scans: Whether to fetch the scans for the alert - this could take some time.
372379
:param wait: Wait for the alert to finish processing before returning.
@@ -375,7 +382,7 @@ def from_id(cls,
375382
:raises intezer_sdk.errors.AlertInProgressError: If the alert is still being processed.
376383
:return: The Alert instance, with the updated alert data.
377384
"""
378-
new_alert = cls(alert_id=alert_id, api=api)
385+
new_alert = cls(alert_id=alert_id, environment=environment, api=api)
379386
status = new_alert.check_status()
380387
if status == AlertStatusCode.IN_PROGRESS and not wait:
381388
raise errors.AlertInProgressError(alert_id)
@@ -488,7 +495,7 @@ def send_phishing_email(cls,
488495
send_alert_params = {key: value for key, value in send_alert_params.items() if value is not None}
489496
alert_id = _api.send_binary_alert(**send_alert_params)
490497

491-
alert = cls(alert_id=alert_id, api=api)
498+
alert = cls(alert_id=alert_id, environment=environment, api=api)
492499
if wait:
493500
alert.wait_for_completion(timeout=timeout)
494501
return alert
@@ -550,17 +557,20 @@ def _fetch_scan(scan_: dict,
550557
_fetch_scan(scan, 'url_analysis', UrlAnalysis)
551558

552559
def get_raw_data(self,
553-
environment: str,
560+
environment: Optional[str] = None,
554561
raw_data_type: str = 'raw_alert') -> dict:
555562
"""
556563
Get raw alert data.
557564
558-
:param environment: The environment to get raw data from.
565+
:param environment: The environment to get raw data from. If not provided, the environment will be taken from the alert.
559566
:param raw_data_type: The type of raw data to retrieve. Defaults to 'raw_alert'.
560567
:return: The raw alert data.
561568
"""
569+
if not environment and not self.environment:
570+
raise ValueError('Environment is required to get raw data.')
571+
562572
return self._api.get_raw_alert_data(
563573
alert_id=self.alert_id,
564-
environment=environment,
574+
environment=environment or self.environment,
565575
raw_data_type=raw_data_type
566576
)

intezer_sdk/incidents.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ class Incident:
159159
:vartype intezer_incident_url: str
160160
"""
161161

162-
def __init__(self, incident_id: Optional[str] = None, api: IntezerApiClient = None):
162+
def __init__(
163+
self,
164+
incident_id: Optional[str] = None,
165+
environment: Optional[str] = None,
166+
api: IntezerApiClient = None,
167+
):
163168
"""
164169
Create a new Incident instance with the given incident id.
165170
Please note that this does not query the Intezer Analyze API for the incident data, but rather creates an Incident
@@ -168,10 +173,11 @@ def __init__(self, incident_id: Optional[str] = None, api: IntezerApiClient = No
168173
If you wish to fetch the incident data from the Intezer Analyze API, use the `from_id` class method.
169174
170175
:param incident_id: The incident id.
176+
:param environment: The environment of the incident.
171177
:param api: The API connection to Intezer.
172178
"""
173179
self.incident_id = incident_id
174-
180+
self.environment = environment
175181
self._intezer_api_client = api
176182
self._api = IntezerApi(api or get_global_api())
177183
self._result: Optional[Dict] = None
@@ -192,7 +198,9 @@ def fetch_info(self):
192198
raise ValueError("Incident ID is required to fetch incident info.")
193199

194200
try:
195-
self._result = self._api.get_incident_by_id(self.incident_id)
201+
self._result = self._api.get_incident_by_id(
202+
self.incident_id, self.environment
203+
)
196204
except HTTPError as e:
197205
if e.response.status_code == 404:
198206
raise errors.IncidentNotFoundError(self.incident_id)
@@ -215,34 +223,35 @@ def result(self) -> Optional[dict]:
215223
return self._result
216224

217225
@classmethod
218-
def from_id(cls, incident_id: str, api: IntezerApiClient = None) -> 'Incident':
226+
def from_id(cls, incident_id: str, environment: Optional[str] = None, api: IntezerApiClient = None) -> 'Incident':
219227
"""
220228
Create a new Incident instance, and fetch the incident data from the Intezer Analyze API.
221229
222230
:param incident_id: The incident id.
231+
:param environment: The environment of the incident.
223232
:param api: The API connection to Intezer.
224233
:raises intezer_sdk.errors.IncidentNotFound: If the incident was not found.
225234
:return: The Incident instance, with the updated incident data.
226235
"""
227-
new_incident = cls(incident_id, api=api)
236+
new_incident = cls(incident_id, environment=environment, api=api)
228237
new_incident.fetch_info()
229238
return new_incident
230239

231-
def get_raw_data(self,
232-
environment: str,
233-
raw_data_type: str = 'raw_incident') -> dict:
240+
def get_raw_data(
241+
self, environment: Optional[str] = None, raw_data_type: str = 'raw_incident'
242+
) -> dict:
234243
"""
235244
Get raw incident data.
236245
237-
:param environment: The environment to get raw data from.
246+
:param environment: The environment to get raw data from. If not provided, the environment will be taken from the incident.
238247
:param raw_data_type: The type of raw data to retrieve. Defaults to 'raw_incident'.
239248
:return: The raw incident data.
240249
"""
241-
if not self.incident_id:
242-
raise ValueError('Incident ID is required to get raw data.')
243-
250+
if not self.incident_id and not self.environment:
251+
raise ValueError('Incident ID and environment are required to get raw data.')
252+
244253
return self._api.get_raw_incident_data(
245254
incident_id=self.incident_id,
246-
environment=environment,
247-
raw_data_type=raw_data_type
255+
environment=environment or self.environment,
256+
raw_data_type=raw_data_type,
248257
)

0 commit comments

Comments
 (0)