Skip to content

Commit

Permalink
Merge branch 'DLPLAT-352-connection-module-change' into devel
Browse files Browse the repository at this point in the history
* DLPLAT-352-connection-module-change:
  fix lint
  add test for session reuse
  remove debug print
  fix lint
  change module naming, fix test
  change initialization flow to allow configurate
  reuse adapter
  change connection from class to module

Signed-off-by: Jamie Couture <[email protected]>
  • Loading branch information
couture-ql committed May 3, 2022
2 parents d8af6f1 + 9bc8dd0 commit 4e18853
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 160 deletions.
207 changes: 108 additions & 99 deletions nasdaqdatalink/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,107 +14,116 @@
AuthenticationError, ForbiddenError, InvalidRequestError,
NotFoundError, ServiceUnavailableError)

# global session
session = None

class Connection:
@classmethod
def request(cls, http_verb, url, **options):
if 'headers' in options:
headers = options['headers']

def request(http_verb, url, **options):
if 'headers' in options:
headers = options['headers']
else:
headers = {}

accept_value = 'application/json'
if ApiConfig.api_version:
accept_value += ", application/vnd.data.nasdaq+json;version=%s" % ApiConfig.api_version

headers = Util.merge_to_dicts({
'accept': accept_value,
'request-source': 'python',
'request-source-version': VERSION
}, headers)
if ApiConfig.api_key:
headers = Util.merge_to_dicts({'x-api-token': ApiConfig.api_key}, headers)

options['headers'] = headers

abs_url = '%s/%s' % (ApiConfig.api_base, url)

return execute_request(http_verb, abs_url, **options)


def execute_request(http_verb, url, **options):
session = get_session()

try:
response = session.request(
method=http_verb,
url=url,
verify=ApiConfig.verify_ssl,
**options
)
if response.status_code < 200 or response.status_code >= 300:
handle_api_error(response)
else:
headers = {}

accept_value = 'application/json'
if ApiConfig.api_version:
accept_value += ", application/vnd.data.nasdaq+json;version=%s" % ApiConfig.api_version

headers = Util.merge_to_dicts({'accept': accept_value,
'request-source': 'python',
'request-source-version': VERSION}, headers)
if ApiConfig.api_key:
headers = Util.merge_to_dicts({'x-api-token': ApiConfig.api_key}, headers)

options['headers'] = headers

abs_url = '%s/%s' % (ApiConfig.api_base, url)

return cls.execute_request(http_verb, abs_url, **options)

@classmethod
def execute_request(cls, http_verb, url, **options):
session = cls.get_session()

try:
response = session.request(method=http_verb,
url=url,
verify=ApiConfig.verify_ssl,
**options)
if response.status_code < 200 or response.status_code >= 300:
cls.handle_api_error(response)
else:
return response
except requests.exceptions.RequestException as e:
if e.response:
cls.handle_api_error(e.response)
raise e

@classmethod
def get_session(cls):
session = requests.Session()
adapter = HTTPAdapter(max_retries=cls.get_retries())
session.mount(ApiConfig.api_protocol, adapter)

proxies = urllib.request.getproxies()
if proxies is not None:
session.proxies.update(proxies)
return response
except requests.exceptions.RequestException as e:
if e.response:
handle_api_error(e.response)
raise e


def get_retries():
if not ApiConfig.use_retries:
return Retry(total=0)

Retry.BACKOFF_MAX = ApiConfig.max_wait_between_retries
retries = Retry(total=ApiConfig.number_of_retries,
connect=ApiConfig.number_of_retries,
read=ApiConfig.number_of_retries,
status_forcelist=ApiConfig.retry_status_codes,
backoff_factor=ApiConfig.retry_backoff_factor,
raise_on_status=False)

return retries


def get_session():
global session
if session is not None:
return session

@classmethod
def get_retries(cls):
if not ApiConfig.use_retries:
return Retry(total=0)

Retry.BACKOFF_MAX = ApiConfig.max_wait_between_retries
retries = Retry(total=ApiConfig.number_of_retries,
connect=ApiConfig.number_of_retries,
read=ApiConfig.number_of_retries,
status_forcelist=ApiConfig.retry_status_codes,
backoff_factor=ApiConfig.retry_backoff_factor,
raise_on_status=False)

return retries

@classmethod
def parse(cls, response):
try:
return response.json()
except ValueError:
raise DataLinkError(http_status=response.status_code, http_body=response.text)

@classmethod
def handle_api_error(cls, resp):
error_body = cls.parse(resp)

# if our app does not form a proper data_link_error response
# throw generic error
if 'error' not in error_body:
raise DataLinkError(http_status=resp.status_code, http_body=resp.text)

code = error_body['error']['code']
message = error_body['error']['message']
prog = re.compile('^QE([a-zA-Z])x')
if prog.match(code):
code_letter = prog.match(code).group(1)

d_klass = {
'L': LimitExceededError,
'M': InternalServerError,
'A': AuthenticationError,
'P': ForbiddenError,
'S': InvalidRequestError,
'C': NotFoundError,
'X': ServiceUnavailableError
}
klass = d_klass.get(code_letter, DataLinkError)

raise klass(message, resp.status_code, resp.text, resp.headers, code)
session = requests.Session()
adapter = HTTPAdapter(max_retries=get_retries())
session.mount(ApiConfig.api_protocol, adapter)

proxies = urllib.request.getproxies()
if proxies is not None:
session.proxies.update(proxies)

return session


def parse(response):
try:
return response.json()
except ValueError:
raise DataLinkError(http_status=response.status_code, http_body=response.text)


def handle_api_error(resp):
error_body = parse(resp)

# if our app does not form a proper data_link_error response
# throw generic error
if 'error' not in error_body:
raise DataLinkError(http_status=resp.status_code, http_body=resp.text)

code = error_body['error']['code']
message = error_body['error']['message']
prog = re.compile('^QE([a-zA-Z])x')
if prog.match(code):
code_letter = prog.match(code).group(1)

d_klass = {
'L': LimitExceededError,
'M': InternalServerError,
'A': AuthenticationError,
'P': ForbiddenError,
'S': InvalidRequestError,
'C': NotFoundError,
'X': ServiceUnavailableError
}
klass = d_klass.get(code_letter, DataLinkError)

raise klass(message, resp.status_code, resp.text, resp.headers, code)
4 changes: 2 additions & 2 deletions nasdaqdatalink/model/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import nasdaqdatalink.model.dataset
from nasdaqdatalink.api_config import ApiConfig
from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.errors.data_link_error import DataLinkError
from nasdaqdatalink.message import Message
from nasdaqdatalink.operations.get import GetOperation
Expand Down Expand Up @@ -43,7 +43,7 @@ def bulk_download_to_file(self, file_or_folder_path, **options):
path_url = self._bulk_download_path()

options['stream'] = True
r = Connection.request('get', path_url, **options)
r = connection.request('get', path_url, **options)
file_path = file_or_folder_path
if os.path.isdir(file_or_folder_path):
file_path = file_or_folder_path + '/' + os.path.basename(urlparse(r.url).path)
Expand Down
4 changes: 2 additions & 2 deletions nasdaqdatalink/model/datatable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from six.moves.urllib.request import urlopen

from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.errors.data_link_error import DataLinkError
from nasdaqdatalink.message import Message
from nasdaqdatalink.operations.get import GetOperation
Expand Down Expand Up @@ -51,7 +51,7 @@ def _request_file_info(self, file_or_folder_path, **options):

updated_options = Util.convert_options(request_type=request_type, **options)

r = Connection.request(request_type, url, **updated_options)
r = connection.request(request_type, url, **updated_options)

response_data = r.json()

Expand Down
4 changes: 2 additions & 2 deletions nasdaqdatalink/operations/get.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from inflection import singularize

from .operation import Operation
from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.util import Util


Expand All @@ -21,7 +21,7 @@ def __get_raw_data__(self):

path = Util.constructed_path(cls.get_path(), options['params'])

r = Connection.request('get', path, **options)
r = connection.request('get', path, **options)
response_data = r.json()
Util.convert_to_dates(response_data)
self._raw_data = response_data[singularize(cls.lookup_key())]
Expand Down
6 changes: 3 additions & 3 deletions nasdaqdatalink/operations/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .operation import Operation
from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.util import Util
from nasdaqdatalink.model.paginated_list import PaginatedList
from nasdaqdatalink.utils.request_type_util import RequestType
Expand All @@ -12,7 +12,7 @@ def all(cls, **options):
if 'params' not in options:
options['params'] = {}
path = Util.constructed_path(cls.list_path(), options['params'])
r = Connection.request('get', path, **options)
r = connection.request('get', path, **options)
response_data = r.json()
Util.convert_to_dates(response_data)
resource = cls.create_list_from_response(response_data)
Expand All @@ -27,7 +27,7 @@ def page(cls, datatable, **options):

updated_options = Util.convert_options(request_type=request_type, **options)

r = Connection.request(request_type, path, **updated_options)
r = connection.request(request_type, path, **updated_options)

response_data = r.json()
Util.convert_to_dates(response_data)
Expand Down
24 changes: 18 additions & 6 deletions test/test_connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.api_config import ApiConfig
from nasdaqdatalink.errors.data_link_error import (
DataLinkError, LimitExceededError, InternalServerError,
Expand Down Expand Up @@ -42,7 +42,7 @@ def test_nasdaqdatalink_exceptions_no_retries(self, request_method):

for expected_error in data_link_errors:
self.assertRaises(
expected_error[2], lambda: Connection.request(request_method, 'databases'))
expected_error[2], lambda: connection.request(request_method, 'databases'))

@parameterized.expand(['GET', 'POST'])
def test_parse_error(self, request_method):
Expand All @@ -51,7 +51,7 @@ def test_parse_error(self, request_method):
"https://data.nasdaq.com/api/v3/databases",
body="not json", status=500)
self.assertRaises(
DataLinkError, lambda: Connection.request(request_method, 'databases'))
DataLinkError, lambda: connection.request(request_method, 'databases'))

@parameterized.expand(['GET', 'POST'])
def test_non_data_link_error(self, request_method):
Expand All @@ -62,16 +62,16 @@ def test_non_data_link_error(self, request_method):
{'foobar':
{'code': 'blah', 'message': 'something went wrong'}}), status=500)
self.assertRaises(
DataLinkError, lambda: Connection.request(request_method, 'databases'))
DataLinkError, lambda: connection.request(request_method, 'databases'))

@parameterized.expand(['GET', 'POST'])
@patch('nasdaqdatalink.connection.Connection.execute_request')
@patch('nasdaqdatalink.connection.execute_request')
def test_build_request(self, request_method, mock):
ApiConfig.api_key = 'api_token'
ApiConfig.api_version = '2015-04-09'
params = {'per_page': 10, 'page': 2}
headers = {'x-custom-header': 'header value'}
Connection.request(request_method, 'databases', headers=headers, params=params)
connection.request(request_method, 'databases', headers=headers, params=params)
expected = call(request_method, 'https://data.nasdaq.com/api/v3/databases',
headers={'x-custom-header': 'header value',
'x-api-token': 'api_token',
Expand All @@ -81,3 +81,15 @@ def test_build_request(self, request_method, mock):
'request-source-version': VERSION},
params={'per_page': 10, 'page': 2})
self.assertEqual(mock.call_args, expected)

def test_session_reuse(self):
session1 = connection.get_session()
session2 = connection.get_session()
areSessionsSame = session1 is session2

adapter1 = connection.get_session().get_adapter(ApiConfig.api_protocol)
adapter2 = connection.get_session().get_adapter(ApiConfig.api_protocol)
areAdaptersSame = adapter1 is adapter2

self.assertEqual(areAdaptersSame, True)
self.assertEqual(areSessionsSame, True)
2 changes: 1 addition & 1 deletion test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def tearDownClass(cls):
httpretty.disable()
httpretty.reset()

@patch('nasdaqdatalink.connection.Connection.request')
@patch('nasdaqdatalink.connection.request')
def test_data_calls_connection(self, mock):
Data.all(params={'database_code': 'NSE', 'dataset_code': 'OIL'})
expected = call('get', 'datasets/NSE/OIL/data', params={})
Expand Down
8 changes: 4 additions & 4 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from six.moves.urllib.parse import parse_qs, urlparse

from nasdaqdatalink.api_config import ApiConfig
from nasdaqdatalink.connection import Connection
import nasdaqdatalink.connection as connection
from nasdaqdatalink.errors.data_link_error import (InternalServerError, DataLinkError)
from nasdaqdatalink.model.database import Database
from test.factories.database import DatabaseFactory
Expand All @@ -34,7 +34,7 @@ def tearDownClass(cls):
httpretty.disable()
httpretty.reset()

@patch('nasdaqdatalink.connection.Connection.request')
@patch('nasdaqdatalink.connection.request')
def test_database_calls_connection(self, mock):
database = Database('NSE')
database.data_fields()
Expand Down Expand Up @@ -80,7 +80,7 @@ def tearDownClass(cls):
httpretty.disable()
httpretty.reset()

@patch('nasdaqdatalink.connection.Connection.request')
@patch('nasdaqdatalink.connection.request')
def test_databases_calls_connection(self, mock):
Database.all()
expected = call('get', 'databases', params={})
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_get_bulk_download_url_without_download_type(self):

def test_bulk_download_to_fileaccepts_download_type(self):
m = mock_open()
with patch.object(Connection, 'request') as mock_method:
with patch.object(connection, 'request') as mock_method:
mock_method.return_value.url = 'https://www.blah.com/download/db.zip'
with patch('nasdaqdatalink.model.database.open', m, create=True):
self.database.bulk_download_to_file(
Expand Down
Loading

0 comments on commit 4e18853

Please sign in to comment.