Skip to content

Commit a857ca7

Browse files
authored
chore: add response http status code to the logs (#1208)
1 parent fda3ff1 commit a857ca7

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

functions-python/batch_process_dataset/src/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def download_content(self, temporary_file_path):
123123
authentication_type=self.authentication_type,
124124
api_key_parameter_name=self.api_key_parameter_name,
125125
credentials=self.feed_credentials,
126+
logger=self.logger,
126127
)
127128
is_zip = zipfile.is_zipfile(temporary_file_path)
128129
return file_hash, is_zip

functions-python/helpers/tests/test_helpers.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def test_download_and_get_hash(self):
5353

5454
mock_response = MagicMock()
5555
mock_response.read.side_effect = [mock_binary_data, b""]
56+
mock_response.status = 200
5657
mock_response.__enter__.return_value = mock_response
5758

5859
with patch("urllib3.PoolManager.request", return_value=mock_response):
@@ -80,6 +81,7 @@ def test_download_and_get_hash_auth_type_header(self):
8081
credentials = "Bearer token123"
8182

8283
mock_response = MagicMock()
84+
mock_response.status = 200
8385
mock_response.read.side_effect = [mock_binary_data, b""]
8486
mock_response.__enter__.return_value = mock_response
8587

@@ -105,6 +107,7 @@ def test_download_and_get_hash_auth_type_header(self):
105107
"User-Agent": expected_user_agent,
106108
api_key_parameter_name: credentials,
107109
},
110+
redirect=True,
108111
)
109112

110113
if os.path.exists(file_path):
@@ -113,28 +116,26 @@ def test_download_and_get_hash_auth_type_header(self):
113116
def test_download_and_get_hash_auth_type_api_key(self):
114117
"""
115118
Test the download_and_get_hash function for authentication type 1 (API key).
116-
117-
This test verifies that the download_and_get_hash function correctly handles authentication type 1,
118-
where the credentials are passed as a query parameter in the URL. It mocks the necessary components
119-
and checks that the request is made with the appropriate URL containing the API key.
120-
121119
"""
122120
mock_binary_data = b"binary data for auth type 1"
123121
expected_hash = hashlib.sha256(mock_binary_data).hexdigest()
124122
file_path = "test_file.txt"
125123
base_url = "https://test.com"
126124
api_key_parameter_name = "api_key"
127125
credentials = "key123"
128-
129126
modified_url = f"{base_url}?{api_key_parameter_name}={credentials}"
130127

131128
mock_response = MagicMock()
132129
mock_response.read.side_effect = [mock_binary_data, b""]
130+
mock_response.status = 200
131+
mock_response.release_conn = MagicMock()
133132
mock_response.__enter__.return_value = mock_response
134133

135-
with patch(
136-
"urllib3.PoolManager.request", return_value=mock_response
137-
) as mock_request:
134+
mock_http = MagicMock()
135+
mock_http.request.return_value = mock_response
136+
mock_http.__enter__.return_value = mock_http
137+
138+
with patch("urllib3.PoolManager", return_value=mock_http):
138139
result_hash = download_and_get_hash(
139140
base_url,
140141
file_path,
@@ -148,15 +149,15 @@ def test_download_and_get_hash_auth_type_api_key(self):
148149
self.assertEqual(
149150
result_hash,
150151
expected_hash,
151-
msg=f"Hash mismatch: got {result_hash},"
152-
f" but expected {expected_hash}",
152+
msg=f"Hash mismatch: got {result_hash}, but expected {expected_hash}",
153153
)
154154

155-
mock_request.assert_called_with(
155+
mock_http.request.assert_called_with(
156156
"GET",
157157
modified_url,
158158
preload_content=False,
159159
headers={"User-Agent": expected_user_agent},
160+
redirect=True,
160161
)
161162

162163
if os.path.exists(file_path):

functions-python/helpers/utils.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ def download_and_get_hash(
8686
authentication_type=0,
8787
api_key_parameter_name=None,
8888
credentials=None,
89+
logger=None,
8990
):
9091
"""
9192
Downloads the content of a URL and stores it in a file and returns the hash of the file
9293
"""
94+
logger = logger or logging.getLogger(__name__)
9395
try:
9496
hash_object = hashlib.new(hash_algorithm)
9597

@@ -114,21 +116,27 @@ def download_and_get_hash(
114116

115117
with urllib3.PoolManager(ssl_context=ctx) as http:
116118
with http.request(
117-
"GET", url, preload_content=False, headers=headers
119+
"GET", url, preload_content=False, headers=headers, redirect=True
118120
) as r, open(file_path, "wb") as out_file:
119-
while True:
120-
data = r.read(chunk_size)
121-
if not data:
122-
break
123-
hash_object.update(data)
124-
out_file.write(data)
125-
r.release_conn()
121+
if 200 <= r.status < 300:
122+
logger.info(f"HTTP response code: [{r.status}]")
123+
while True:
124+
data = r.read(chunk_size)
125+
if not data:
126+
break
127+
hash_object.update(data)
128+
out_file.write(data)
129+
r.release_conn()
130+
else:
131+
raise ValueError(f"Invalid HTTP response code: [{r.status}]")
126132
return hash_object.hexdigest()
127133
except Exception as e:
128-
print(e)
129-
# Delete file if it exists
130134
if os.path.exists(file_path):
131-
os.remove(file_path)
135+
try:
136+
os.remove(file_path)
137+
except Exception:
138+
logger.error(f"Delete file: [{file_path}]")
139+
132140
raise e
133141

134142

0 commit comments

Comments
 (0)