1313 AuthenticationError , ForbiddenError , InvalidRequestError ,
1414 NotFoundError , ServiceUnavailableError )
1515
16+ # global session
17+ session = None
18+
19+
1620def request (http_verb , url , ** options ):
1721 if 'headers' in options :
1822 headers = options ['headers' ]
@@ -23,9 +27,11 @@ def request(http_verb, url, **options):
2327 if ApiConfig .api_version :
2428 accept_value += ", application/vnd.data.nasdaq+json;version=%s" % ApiConfig .api_version
2529
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 )
2935 if ApiConfig .api_key :
3036 headers = Util .merge_to_dicts ({'x-api-token' : ApiConfig .api_key }, headers )
3137
@@ -35,14 +41,17 @@ def request(http_verb, url, **options):
3541
3642 return execute_request (http_verb , abs_url , ** options )
3743
44+
3845def execute_request (http_verb , url , ** options ):
3946 session = get_session ()
4047
4148 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+ )
4655 if response .status_code < 200 or response .status_code >= 300 :
4756 handle_api_error (response )
4857 else :
@@ -52,6 +61,7 @@ def execute_request(http_verb, url, **options):
5261 handle_api_error (e .response )
5362 raise e
5463
64+
5565def get_retries ():
5666 if not ApiConfig .use_retries :
5767 return Retry (total = 0 )
@@ -66,24 +76,24 @@ def get_retries():
6676
6777 return retries
6878
69- session = None
7079
7180def get_session ():
7281 global session
7382 if session is None :
83+ print ("initialized" )
7484 session = requests .Session ()
7585 adapter = HTTPAdapter (max_retries = get_retries ())
7686 session .mount (ApiConfig .api_protocol , adapter )
7787 return session
7888
89+
7990def parse (response ):
8091 try :
8192 return response .json ()
8293 except ValueError :
8394 raise DataLinkError (http_status = response .status_code , http_body = response .text )
8495
8596
86-
8797def handle_api_error (resp ):
8898 error_body = parse (resp )
8999
0 commit comments