Skip to content

Commit 7b94b74

Browse files
committed
add http status response code to the logs
1 parent 8bb3108 commit 7b94b74

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
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/utils.py

Lines changed: 20 additions & 10 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,20 +116,28 @@ 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
134+
logger.error(e)
130135
if os.path.exists(file_path):
136+
try:
137+
with open(file_path, "r") as file:
138+
logger.error(f"File content: {file.read()}")
139+
except Exception as read_error:
140+
logger.error(f"Failed to read file content: {read_error}")
131141
os.remove(file_path)
132142
raise e
133143

0 commit comments

Comments
 (0)