Skip to content

Commit

Permalink
Using single session for connections (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwildenhain authored Jul 16, 2020
1 parent eaba033 commit b54e4fe
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions redcap/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
__copyright__ = ' Copyright 2014, Vanderbilt University'


from requests import post, RequestException
from requests import RequestException, Session
import json


RedcapError = RequestException

_session = Session()

class RCAPIError(Exception):
""" Errors corresponding to a misuse of the REDCap API """
Expand All @@ -40,7 +40,7 @@ class RCRequest(object):
biggest consumer.
"""

def __init__(self, url, payload, qtype):
def __init__(self, url, payload, qtype, session=_session):
"""
Constructor
Expand All @@ -56,6 +56,8 @@ def __init__(self, url, payload, qtype):
self.url = url
self.payload = payload
self.type = qtype
self.session = session

if qtype:
self.validate()
fmt_key = 'returnFormat' if 'returnFormat' in payload else 'format'
Expand Down Expand Up @@ -120,19 +122,19 @@ def execute(self, **kwargs):
Parameters
----------
kwargs :
passed to requests.post()
passed to requests.Session.post()
Returns
-------
response : list, str
data object from JSON decoding process if format=='json',
else return raw string (ie format=='csv'|'xml')
"""
r = post(self.url, data=self.payload, **kwargs)
response = self.session.post(self.url, data=self.payload, **kwargs)
# Raise if we need to
self.raise_for_status(r)
content = self.get_content(r)
return content, r.headers
self.raise_for_status(response)
content = self.get_content(response)
return content, response.headers

def get_content(self, r):
"""Abstraction for grabbing content from a returned response"""
Expand Down

0 comments on commit b54e4fe

Please sign in to comment.