-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'rr/auth-session-support' into devel
* rr/auth-session-support: change way to remove params fix lint add test fix pop missing key fix get api_config before params assignment clean request payload fix session object not getting through add test add test for api_config fix null access api_config issue add AuthorizedSession support
- Loading branch information
Showing
11 changed files
with
288 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import nasdaqdatalink | ||
from nasdaqdatalink.api_config import ApiConfig | ||
from urllib3.util.retry import Retry | ||
from requests.adapters import HTTPAdapter | ||
import requests | ||
import urllib | ||
|
||
|
||
def get_retries(api_config=nasdaqdatalink.ApiConfig): | ||
retries = None | ||
if not api_config.use_retries: | ||
return Retry(total=0) | ||
|
||
Retry.BACKOFF_MAX = api_config.max_wait_between_retries | ||
retries = Retry(total=api_config.number_of_retries, | ||
connect=api_config.number_of_retries, | ||
read=api_config.number_of_retries, | ||
status_forcelist=api_config.retry_status_codes, | ||
backoff_factor=api_config.retry_backoff_factor, | ||
raise_on_status=False) | ||
return retries | ||
|
||
|
||
class AuthorizedSession: | ||
def __init__(self, api_config=ApiConfig) -> None: | ||
super(AuthorizedSession, self).__init__() | ||
if not isinstance(api_config, ApiConfig): | ||
api_config = ApiConfig | ||
self._api_config = api_config | ||
self._auth_session = requests.Session() | ||
retries = get_retries(self._api_config) | ||
adapter = HTTPAdapter(max_retries=retries) | ||
self._auth_session.mount(api_config.api_protocol, adapter) | ||
|
||
proxies = urllib.request.getproxies() | ||
if proxies is not None: | ||
self._auth_session.proxies.update(proxies) | ||
|
||
def get(self, dataset, **kwargs): | ||
nasdaqdatalink.get(dataset, session=self._auth_session, | ||
api_config=self._api_config, **kwargs) | ||
|
||
def bulkdownload(self, database, **kwargs): | ||
nasdaqdatalink.bulkdownload(database, session=self._auth_session, | ||
api_config=self._api_config, **kwargs) | ||
|
||
def export_table(self, datatable_code, **kwargs): | ||
nasdaqdatalink.export_table(datatable_code, session=self._auth_session, | ||
api_config=self._api_config, **kwargs) | ||
|
||
def get_table(self, datatable_code, **options): | ||
nasdaqdatalink.get_table(datatable_code, session=self._auth_session, | ||
api_config=self._api_config, **options) | ||
|
||
def get_point_in_time(self, datatable_code, **options): | ||
nasdaqdatalink.get_point_in_time(datatable_code, session=self._auth_session, | ||
api_config=self._api_config, **options) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import unittest | ||
from nasdaqdatalink.model.authorized_session import AuthorizedSession | ||
from nasdaqdatalink.api_config import ApiConfig | ||
from requests.sessions import Session | ||
from requests.adapters import HTTPAdapter | ||
from mock import patch | ||
|
||
|
||
class AuthorizedSessionTest(unittest.TestCase): | ||
def test_authorized_session_assign_correct_internal_config(self): | ||
authed_session = AuthorizedSession() | ||
self.assertTrue(issubclass(authed_session._api_config, ApiConfig)) | ||
authed_session = AuthorizedSession(None) | ||
self.assertTrue(issubclass(authed_session._api_config, ApiConfig)) | ||
api_config = ApiConfig() | ||
authed_session = AuthorizedSession(api_config) | ||
self.assertTrue(isinstance(authed_session._api_config, ApiConfig)) | ||
|
||
def test_authorized_session_pass_created_session(self): | ||
ApiConfig.use_retries = True | ||
ApiConfig.number_of_retries = 130 | ||
authed_session = AuthorizedSession() | ||
self.assertTrue(isinstance(authed_session._auth_session, Session)) | ||
adapter = authed_session._auth_session.get_adapter(ApiConfig.api_protocol) | ||
self.assertTrue(isinstance(adapter, HTTPAdapter)) | ||
self.assertEqual(adapter.max_retries.connect, 130) | ||
|
||
@patch("nasdaqdatalink.get") | ||
def test_call_get_with_session_and_api_config(self, mock): | ||
api_config = ApiConfig() | ||
authed_session = AuthorizedSession(api_config) | ||
authed_session.get('WIKI/AAPL') | ||
mock.assert_called_with('WIKI/AAPL', api_config=api_config, | ||
session=authed_session._auth_session) | ||
|
||
@patch("nasdaqdatalink.bulkdownload") | ||
def test_call_bulkdownload_with_session_and_api_config(self, mock): | ||
api_config = ApiConfig() | ||
authed_session = AuthorizedSession(api_config) | ||
authed_session.bulkdownload('NSE') | ||
mock.assert_called_with('NSE', api_config=api_config, | ||
session=authed_session._auth_session) | ||
|
||
@patch("nasdaqdatalink.export_table") | ||
def test_call_export_table_with_session_and_api_config(self, mock): | ||
authed_session = AuthorizedSession() | ||
authed_session.export_table('WIKI/AAPL') | ||
mock.assert_called_with('WIKI/AAPL', api_config=ApiConfig, | ||
session=authed_session._auth_session) | ||
|
||
@patch("nasdaqdatalink.get_table") | ||
def test_call_get_table_with_session_and_api_config(self, mock): | ||
authed_session = AuthorizedSession() | ||
authed_session.get_table('WIKI/AAPL') | ||
mock.assert_called_with('WIKI/AAPL', api_config=ApiConfig, | ||
session=authed_session._auth_session) | ||
|
||
@patch("nasdaqdatalink.get_point_in_time") | ||
def test_call_get_point_in_time_with_session_and_api_config(self, mock): | ||
authed_session = AuthorizedSession() | ||
authed_session.get_point_in_time('DATABASE/CODE', interval='asofdate', date='2020-01-01') | ||
mock.assert_called_with('DATABASE/CODE', interval='asofdate', | ||
date='2020-01-01', api_config=ApiConfig, | ||
session=authed_session._auth_session) |
Oops, something went wrong.