forked from quandl/quandl-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.py
114 lines (92 loc) · 3.81 KB
/
connection.py
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
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 quandl.errors.quandl_error import (
QuandlError, LimitExceededError, InternalServerError,
AuthenticationError, ForbiddenError, InvalidRequestError,
NotFoundError, ServiceUnavailableError)
class Connection:
@classmethod
def request(cls, 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.quandl+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)
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 QuandlError(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 quandl_error response
# throw generic error
if 'quandl_error' not in error_body:
raise QuandlError(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, QuandlError)
raise klass(message, resp.status_code, resp.text, resp.headers, code)