Content-Length: 327113 | pFad | http://github.com/Nasdaq/data-link-python/pull/16/commits/d9105388e72621ae50498bb884b49b4dc2a4aabe

B6 session-per-request to session-over-time by runawaycoast · Pull Request #16 · Nasdaq/data-link-python · GitHub
Skip to content

session-per-request to session-over-time #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix lint
  • Loading branch information
runawaycoast committed Apr 5, 2022
commit d9105388e72621ae50498bb884b49b4dc2a4aabe
30 changes: 20 additions & 10 deletions nasdaqdatalink/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
AuthenticationError, ForbiddenError, InvalidRequestError,
NotFoundError, ServiceUnavailableError)

# global session
session = None


def request(http_verb, url, **options):
if 'headers' in options:
headers = options['headers']
Expand All @@ -23,9 +27,11 @@ def request(http_verb, url, **options):
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)
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)

Expand All @@ -35,14 +41,17 @@ def request(http_verb, url, **options):

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)
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:
Expand All @@ -52,6 +61,7 @@ def execute_request(http_verb, url, **options):
handle_api_error(e.response)
raise e


def get_retries():
if not ApiConfig.use_retries:
return Retry(total=0)
Expand All @@ -66,24 +76,24 @@ def get_retries():

return retries

session = None

def get_session():
global session
if session is None:
print("initialized")
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)

Expand All @@ -109,4 +119,4 @@ def handle_api_error(resp):
}
klass = d_klass.get(code_letter, DataLinkError)

raise klass(message, resp.status_code, resp.text, resp.headers, code)
raise klass(message, resp.status_code, resp.text, resp.headers, code)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Nasdaq/data-link-python/pull/16/commits/d9105388e72621ae50498bb884b49b4dc2a4aabe

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy