13
13
AuthenticationError , ForbiddenError , InvalidRequestError ,
14
14
NotFoundError , ServiceUnavailableError )
15
15
16
+ # global session
17
+ session = None
18
+
19
+
16
20
def request (http_verb , url , ** options ):
17
21
if 'headers' in options :
18
22
headers = options ['headers' ]
@@ -23,9 +27,11 @@ def request(http_verb, url, **options):
23
27
if ApiConfig .api_version :
24
28
accept_value += ", application/vnd.data.nasdaq+json;version=%s" % ApiConfig .api_version
25
29
26
- headers = Util .merge_to_dicts ({'accept' : accept_value ,
27
- 'request-source' : 'python' ,
28
- 'request-source-version' : VERSION }, headers )
30
+ headers = Util .merge_to_dicts ({
31
+ 'accept' : accept_value ,
32
+ 'request-source' : 'python' ,
33
+ 'request-source-version' : VERSION
34
+ }, headers )
29
35
if ApiConfig .api_key :
30
36
headers = Util .merge_to_dicts ({'x-api-token' : ApiConfig .api_key }, headers )
31
37
@@ -35,14 +41,17 @@ def request(http_verb, url, **options):
35
41
36
42
return execute_request (http_verb , abs_url , ** options )
37
43
44
+
38
45
def execute_request (http_verb , url , ** options ):
39
46
session = get_session ()
40
47
41
48
try :
42
- response = session .request (method = http_verb ,
43
- url = url ,
44
- verify = ApiConfig .verify_ssl ,
45
- ** options )
49
+ response = session .request (
50
+ method = http_verb ,
51
+ url = url ,
52
+ verify = ApiConfig .verify_ssl ,
53
+ ** options
54
+ )
46
55
if response .status_code < 200 or response .status_code >= 300 :
47
56
handle_api_error (response )
48
57
else :
@@ -52,6 +61,7 @@ def execute_request(http_verb, url, **options):
52
61
handle_api_error (e .response )
53
62
raise e
54
63
64
+
55
65
def get_retries ():
56
66
if not ApiConfig .use_retries :
57
67
return Retry (total = 0 )
@@ -66,24 +76,24 @@ def get_retries():
66
76
67
77
return retries
68
78
69
- session = None
70
79
71
80
def get_session ():
72
81
global session
73
82
if session is None :
83
+ print ("initialized" )
74
84
session = requests .Session ()
75
85
adapter = HTTPAdapter (max_retries = get_retries ())
76
86
session .mount (ApiConfig .api_protocol , adapter )
77
87
return session
78
88
89
+
79
90
def parse (response ):
80
91
try :
81
92
return response .json ()
82
93
except ValueError :
83
94
raise DataLinkError (http_status = response .status_code , http_body = response .text )
84
95
85
96
86
-
87
97
def handle_api_error (resp ):
88
98
error_body = parse (resp )
89
99
@@ -109,4 +119,4 @@ def handle_api_error(resp):
109
119
}
110
120
klass = d_klass .get (code_letter , DataLinkError )
111
121
112
- raise klass (message , resp .status_code , resp .text , resp .headers , code )
122
+ raise klass (message , resp .status_code , resp .text , resp .headers , code )
0 commit comments