-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathconnection.py
More file actions
136 lines (109 loc) · 4.43 KB
/
connection.py
File metadata and controls
136 lines (109 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import re
import requests
import urllib
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from .util import Util
from .version import VERSION
from .api_config import ApiConfig, get_config_from_kwargs
from nasdaqdatalink.errors.data_link_error import (
DataLinkError, LimitExceededError, InternalServerError,
AuthenticationError, ForbiddenError, InvalidRequestError,
NotFoundError, ServiceUnavailableError)
KW_TO_REMOVE = [
'session',
'api_config'
]
class Connection:
@classmethod
def request(cls, http_verb, url, **options):
if 'headers' in options:
headers = options['headers']
else:
headers = {}
api_config = get_config_from_kwargs(options)
accept_value = 'application/json'
if api_config.api_version:
accept_value += ", application/vnd.data.nasdaq+json;version=%s" % api_config.api_version
headers = Util.merge_to_dicts({'accept': accept_value,
'request-source': 'python',
'request-source-version': VERSION}, headers)
if api_config.api_key:
headers = Util.merge_to_dicts({'x-api-token': api_config.api_key}, headers)
options['headers'] = headers
abs_url = '%s/%s' % (api_config.api_base, url)
return cls.execute_request(http_verb, abs_url, **options)
@classmethod
def execute_request(cls, http_verb, url, **options):
session = options.get('params', {}).get('session', None)
if session is None:
session = cls.get_session()
api_config = get_config_from_kwargs(options)
cls.options_kw_strip(options)
try:
response = session.request(method=http_verb,
url=url,
verify=api_config.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 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 'quandl_error' not in error_body:
raise DataLinkError(http_status=resp.status_code, http_body=resp.text)
code = error_body['quandl_error']['code']
message = error_body['quandl_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)
@classmethod
def options_kw_strip(self, options):
for kw in KW_TO_REMOVE:
options.get('params', {}).pop(kw, None)