-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathconnection.py
More file actions
121 lines (93 loc) · 3.38 KB
/
connection.py
File metadata and controls
121 lines (93 loc) · 3.38 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
import re
import requests
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from .util import Util
from .version import VERSION
from .api_config import ApiConfig
from nasdaqdatalink.errors.data_link_error import (
DataLinkError, LimitExceededError, InternalServerError,
AuthenticationError, ForbiddenError, InvalidRequestError,
NotFoundError, ServiceUnavailableError)
# global session
session = None
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:
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 None:
session = requests.Session()
adapter = HTTPAdapter(max_retries=get_retries())
session.mount(ApiConfig.api_protocol, adapter)
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)