@@ -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