Skip to content

Commit

Permalink
Merge pull request #14 from SimFin/DEV-231-Autherntication-with-heade…
Browse files Browse the repository at this point in the history
…r-api-key

Dev 231 autherntication with header api key
  • Loading branch information
thf24 authored Feb 22, 2023
2 parents 47ca12b + 4078dc6 commit 7b0df50
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# This is also defined in simfin/__init__.py and must be
# updated in both places.
MY_VERSION = '0.8.4'
MY_VERSION = '0.9.0'

setup(
name='simfin',
Expand Down
2 changes: 1 addition & 1 deletion simfin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This is also defined in setup.py and must be updated in both places.
__version__ = "0.8.4"
__version__ = "0.9.0"

# Expose the following as top-level imports.

Expand Down
31 changes: 19 additions & 12 deletions simfin/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

##########################################################################

def _headers_dataset():
api_key = get_api_key()
if api_key is not None:
return {'Authorization': 'api-key ' + api_key}
else:
raise ValueError('Api-Key is not set')


def _url_dataset(dataset, market=None, variant=None):
"""
Compose the URL for a dataset on the SimFin server.
Expand All @@ -46,13 +54,8 @@ def _url_dataset(dataset, market=None, variant=None):
if market is not None:
args += '&market=' + market

# API key.
api_key = get_api_key()
if api_key is not None:
args += '&api-key=' + api_key

# Base URL for the bulk-download API on the SimFin server.
base_url = 'https://simfin.com/api/bulk?'
base_url = 'https://backend.simfin.com/api/bulk-download?'

# Combine base URL and arguments.
url = base_url + args
Expand Down Expand Up @@ -107,7 +110,7 @@ def _print_download_progress(downloaded_size, total_size):

##########################################################################

def _download(url, download_path):
def _download(url, headers, download_path):
"""
Download a file from an internet URL and save it to disk.
Expand All @@ -130,7 +133,8 @@ def _download(url, download_path):
"""

# Open a streaming connection to the server.
with requests.get(url, stream=True) as response:
with requests.get(url, headers=headers,
stream=True) as response:
# Get the status code for the connection.
status_code = response.status_code

Expand Down Expand Up @@ -173,7 +177,7 @@ def _download(url, download_path):

##########################################################################

def _maybe_download(name, url, path, download_path, refresh_days):
def _maybe_download(name, url, headers, path, download_path, refresh_days):
"""
Check if the file in `path` exists on disk or if it is too old, and then
download the file from `url` and save it to disk.
Expand Down Expand Up @@ -236,7 +240,7 @@ def _maybe_download(name, url, path, download_path, refresh_days):
if must_download:
# Download the file from the SimFin server.
# This is assumed to succeed unless an exception is raised.
_download(url=url, download_path=download_path)
_download(url=url, headers=headers, download_path=download_path)

if download_path.endswith('zip'):
# Downloaded file must be unzipped into the data-dir.
Expand Down Expand Up @@ -285,12 +289,14 @@ def _maybe_download_dataset(refresh_days, **kwargs):
# Full path for the downloaded file.
download_path = _path_download_dataset(**kwargs)

headers = _headers_dataset()

# URL to SimFin's server where the file is located.
url = _url_dataset(**kwargs)

return _maybe_download(name=dataset_name, path=path,
download_path=download_path,
url=url, refresh_days=refresh_days)
url=url, headers=headers, refresh_days=refresh_days)

##########################################################################

Expand All @@ -313,8 +319,9 @@ def _maybe_download_info(name, refresh_days):
# URL to SimFin's server where the file is located.
url = _url_info(name=name)

headers = _headers_dataset()
return _maybe_download(name=name, path=path,
download_path=download_path,
url=url, refresh_days=refresh_days)
url=url,headers=headers, refresh_days=refresh_days)

##########################################################################
4 changes: 2 additions & 2 deletions simfin/names_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
SPS = SALES_PER_SHARE = 'Sales Per Share'

#: Net Income / Number of Shares.
EPS_BASIC = EPS = EARNINGS_PER_SHARE = 'Earnings per Share, Basic'
EPS_BASIC = EPS = EARNINGS_PER_SHARE = 'Earnings Per Share, Basic'

#: Net Income / Number of Shares.
EPS_DILUTED = 'Earnings per Share, Diluted'
EPS_DILUTED = 'Earnings Per Share, Diluted'

#: Total Equity / Number of Shares.
EQ_PS = EQUITY_PER_SHARE = BV_PS = BOOK_VALUE_PER_SHARE = 'Equity Per Share'
Expand Down
Loading

0 comments on commit 7b0df50

Please sign in to comment.