diff --git a/CHANGELOG.md b/CHANGELOG.md index f3defcd..b227ac7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [2.0] +## [2.0.1] +### Changed +- Updated `use_connection_pooling` option to address session overwriting. + +## [2.0.0] ### Changed - Updated transactional email request optional argument `amp_body` to `body_amp` for consistency across APIs ([#93](https://github.com/customerio/customerio-python/pull/93)) diff --git a/customerio/__version__.py b/customerio/__version__.py index 8298d6f..76e22df 100644 --- a/customerio/__version__.py +++ b/customerio/__version__.py @@ -1,4 +1,4 @@ -VERSION = (2, 0, 0, 'final', 0) +VERSION = (2, 0, 1, 'final', 0) def get_version(): version = '%s.%s' % (VERSION[0], VERSION[1]) diff --git a/customerio/client_base.py b/customerio/client_base.py index 60735d2..e6368a7 100644 --- a/customerio/client_base.py +++ b/customerio/client_base.py @@ -26,22 +26,20 @@ def __init__(self, retries=3, timeout=10, backoff_factor=0.02, use_connection_po @property def http(self): if self._current_session is None: - self._current_session = self._get_session() + self._current_session = self._build_session() return self._current_session def send_request(self, method, url, data): '''Dispatches the request and returns a response''' - try: - response = self.http.request( - method, url=url, json=self._sanitize(data), timeout=self.timeout) - - result_status = response.status_code - if result_status != 200: - raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text)) - return response.text - + if self.use_connection_pooling: + response = self.http.request( + method, url=url, json=self._sanitize(data), timeout=self.timeout) + else: + with self._build_session() as http: + response = http.request( + method, url=url, json=self._sanitize(data), timeout=self.timeout) except Exception as e: # Raise exception alerting user that the system might be # experiencing an outage and refer them to system status page. @@ -51,8 +49,10 @@ def send_request(self, method, url, data): '''.format(klass=type(e), message=e, count=self.retries) raise CustomerIOException(message) - finally: - self._close() + result_status = response.status_code + if result_status != 200: + raise CustomerIOException('%s: %s %s %s' % (result_status, url, data, response.text)) + return response.text def _sanitize(self, data): for k, v in data.items(): @@ -77,21 +77,6 @@ def _stringify_list(self, customer_ids): 'customer_ids cannot be {type}'.format(type=type(v))) return customer_string_ids - # gets a session based on whether we want pooling or not. If no pooling is desired, we create a new session each time. - def _get_session(self): - if (self.use_connection_pooling): - if (self._current_session is None): - self._current_session = self._build_session() - - # if we're using pooling, return the existing session. - logging.debug("Using existing session...") - return self._current_session - else: - # if we're not using pooling, build a new session. - logging.debug("Creating new session...") - self._current_session = self._build_session() - return self._current_session - # builds the session. def _build_session(self): session = Session() @@ -104,10 +89,3 @@ def _build_session(self): HTTPAdapter(max_retries=Retry(total=self.retries, backoff_factor=self.backoff_factor))) return session - - # closes the session if we're not using connection pooling. - def _close(self): - # if we're not using pooling; clean up the resources. - if (not self.use_connection_pooling): - self._current_session.close() - self._current_session = None diff --git a/customerio/track.py b/customerio/track.py index de90b16..e6fc7d0 100644 --- a/customerio/track.py +++ b/customerio/track.py @@ -220,4 +220,4 @@ def _build_session(self): session = super()._build_session() session.auth = (self.site_id, self.api_key) - return session \ No newline at end of file + return session