2121
2222##########################################################################
2323
24+ def _headers_dataset ():
25+ api_key = get_api_key ()
26+ if api_key is not None :
27+ return {'Authorization' : 'api-key ' + api_key }
28+ else :
29+ raise ValueError ('Api-Key is not set' )
30+
31+
2432def _url_dataset (dataset , market = None , variant = None ):
2533 """
2634 Compose the URL for a dataset on the SimFin server.
@@ -46,13 +54,8 @@ def _url_dataset(dataset, market=None, variant=None):
4654 if market is not None :
4755 args += '&market=' + market
4856
49- # API key.
50- api_key = get_api_key ()
51- if api_key is not None :
52- args += '&api-key=' + api_key
53-
5457 # Base URL for the bulk-download API on the SimFin server.
55- base_url = 'https://simfin.com/api/bulk?'
58+ base_url = 'https://backend. simfin.com/api/bulk-download ?'
5659
5760 # Combine base URL and arguments.
5861 url = base_url + args
@@ -107,7 +110,7 @@ def _print_download_progress(downloaded_size, total_size):
107110
108111##########################################################################
109112
110- def _download (url , download_path ):
113+ def _download (url , headers , download_path ):
111114 """
112115 Download a file from an internet URL and save it to disk.
113116
@@ -130,7 +133,8 @@ def _download(url, download_path):
130133 """
131134
132135 # Open a streaming connection to the server.
133- with requests .get (url , stream = True ) as response :
136+ with requests .get (url , headers = headers ,
137+ stream = True ) as response :
134138 # Get the status code for the connection.
135139 status_code = response .status_code
136140
@@ -173,7 +177,7 @@ def _download(url, download_path):
173177
174178##########################################################################
175179
176- def _maybe_download (name , url , path , download_path , refresh_days ):
180+ def _maybe_download (name , url , headers , path , download_path , refresh_days ):
177181 """
178182 Check if the file in `path` exists on disk or if it is too old, and then
179183 download the file from `url` and save it to disk.
@@ -236,7 +240,7 @@ def _maybe_download(name, url, path, download_path, refresh_days):
236240 if must_download :
237241 # Download the file from the SimFin server.
238242 # This is assumed to succeed unless an exception is raised.
239- _download (url = url , download_path = download_path )
243+ _download (url = url , headers = headers , download_path = download_path )
240244
241245 if download_path .endswith ('zip' ):
242246 # Downloaded file must be unzipped into the data-dir.
@@ -285,12 +289,14 @@ def _maybe_download_dataset(refresh_days, **kwargs):
285289 # Full path for the downloaded file.
286290 download_path = _path_download_dataset (** kwargs )
287291
292+ headers = _headers_dataset ()
293+
288294 # URL to SimFin's server where the file is located.
289295 url = _url_dataset (** kwargs )
290296
291297 return _maybe_download (name = dataset_name , path = path ,
292298 download_path = download_path ,
293- url = url , refresh_days = refresh_days )
299+ url = url , headers = headers , refresh_days = refresh_days )
294300
295301##########################################################################
296302
@@ -313,8 +319,9 @@ def _maybe_download_info(name, refresh_days):
313319 # URL to SimFin's server where the file is located.
314320 url = _url_info (name = name )
315321
322+ headers = _headers_dataset ()
316323 return _maybe_download (name = name , path = path ,
317324 download_path = download_path ,
318- url = url , refresh_days = refresh_days )
325+ url = url ,headers = headers , refresh_days = refresh_days )
319326
320327##########################################################################
0 commit comments