Skip to content

Commit 1f6e43d

Browse files
Address PR comments
- Format import statements - Use @DataClass for Block class - Make is_common a property - Switch from " strings to ' strings - Use of HTTPStatus instead of int - Changed variable format to long-name
1 parent ae62ae2 commit 1f6e43d

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

intezer_sdk/code_reuse.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,62 @@
1+
from dataclasses import dataclass
2+
from http import HTTPStatus
13
from time import sleep
2-
from typing import Optional, List
4+
from typing import Optional
5+
from typing import List
36

47
from intezer_sdk import api
58
from intezer_sdk.api import IntezerApiClient
69
from intezer_sdk._api import IntezerApi
710

8-
11+
@dataclass
912
class Block:
10-
def __init__(self, address: int, software_type: str, families: List[str]):
11-
self.address = address
12-
self.software_type = software_type
13-
self.families = families
14-
self.is_common = software_type == "common"
13+
address: int
14+
software_type: str
15+
families: List[str]
16+
17+
@property
18+
def is_common(self):
19+
return self.software_type == 'common'
1520

1621

1722
class CodeReuse:
18-
"""
23+
'''
1924
Get code reuse for a file.
2025
2126
Parameters:
2227
api_client (Optional[IntezerApiClient]): An optional Intezer API client instance. If not provided, the global
2328
API client will be used.
24-
"""
29+
'''
2530

2631
def __init__(self, api_client: Optional[IntezerApiClient] = None):
2732
self._api = IntezerApi(api_client or api.get_global_api())
2833

2934
def _get_result_from_task(self, result_url: str):
3035
response = self._api.api.request_with_refresh_expired_access_token(
31-
"GET", result_url)
32-
while response.status_code == 202:
36+
'GET', result_url)
37+
while response.status_code == HTTPStatus.ACCEPTED:
3338
sleep(2)
3439
response = self._api.api.request_with_refresh_expired_access_token(
35-
"GET", result_url)
40+
'GET', result_url)
3641
response.raise_for_status()
3742
return response.json()['result']
3843

3944
def get_code_blocks(self, sha256: str) -> List[Block]:
40-
"""
45+
'''
4146
Retrieves a report containing information about reused code blocks for the given SHA-256 hash.
4247
4348
Parameters:
4449
sha256_hash (str): The SHA-256 hash of the file to analyze.
4550
4651
Returns:
4752
List[Block]: A sorted list of Block objects representing the code blocks found in the analysis.
48-
"""
53+
'''
4954
result_url = self._api.get_code_reuse_by_code_block(sha256)
5055
# This endpoint acts different. We don't get a status and instead have to use
5156
# the HTTP status code to wait for the report.
5257
result = self._get_result_from_task(result_url)
5358
blocks: list[Block] = []
54-
for addr, val in result["blocks"].items():
59+
for address, block in result['blocks'].items():
5560
blocks.append(
56-
Block(int(addr), val["software_type"], val["code_reuse"]))
61+
Block(int(address), block['software_type'], block['code_reuse']))
5762
return sorted(blocks, key=lambda b: b.address)

0 commit comments

Comments
 (0)