Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.24.0 (Unreleased)
-------
- Introduce File class
- Add code reuse blocks


1.23.1
Expand Down
2 changes: 1 addition & 1 deletion intezer_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.23.1'
__version__ = '1.24.0'
14 changes: 14 additions & 0 deletions intezer_sdk/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,20 @@ def get_string_related_samples_by_id(self,

return response.json()['result_url']

def get_code_reuse_by_code_block(self, sha256: str):
response = self.api.request_with_refresh_expired_access_token(
'POST', f'/files/{sha256}/code-reuse-by-code-block'
)

if response.status_code == HTTPStatus.NOT_FOUND:
raise errors.HashDoesNotExistError(response)
if response.status_code == HTTPStatus.CONFLICT:
raise ValueError('sha256 is not a code item')

raise_for_status(response)

return response.json()['result_url']

def get_url_result(self, url: str) -> dict:
"""
Send GET request to an url.
Expand Down
40 changes: 40 additions & 0 deletions intezer_sdk/file.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import datetime
from dataclasses import dataclass
from typing import IO
from typing import List
from typing import Optional
from typing import Union

from intezer_sdk import _operation
from intezer_sdk import consts
from intezer_sdk import operation
from intezer_sdk._api import IntezerApi
from intezer_sdk.api import IntezerApiClient
from intezer_sdk.api import get_global_api
from intezer_sdk.index import Index


@dataclass
class Block:
address: int
software_type: str
families: List[str]

@property
def is_common(self):
return self.software_type == 'common'


class File:
"""
File is a class for file-related operations including indexing and downloading.
Expand All @@ -33,6 +49,7 @@ def __init__(self,
self._sha256 = sha256
self._api = IntezerApi(api or get_global_api())
self._index: Optional[Index] = None
self._operations = {}

@property
def sha256(self) -> str:
Expand Down Expand Up @@ -137,3 +154,26 @@ def download(self,
raise ValueError('Download is only supported for sha256-based files')

self._api.download_file_by_sha256(self._sha256, path, output_stream, password_protection)

def get_code_blocks(self,
wait: Union[bool, int] = False,
wait_timeout: Optional[datetime.timedelta] = None) -> operation.Operation:
"""
Retrieves a report containing information about reused code blocks for the given SHA-256 hash.

:param wait: Should wait until the operation completes.
:param wait_timeout: Maximum duration to wait for operation completion.

Returns:
operation.Operation: An operation object that will contain the code blocks result.
"""
if not self._sha256:
raise ValueError('Code block report is only supported for sha256-based files')

result_url = self._api.get_code_reuse_by_code_block(self._sha256)
return _operation.handle_operation(self._operations,
self._api,
'Code blocks',
result_url,
wait,
wait_timeout)
Loading