def _get_or_create_session(self, sensor_id): sensor_sessions = [ s for s in self._cb.get_object( "{cblr_base}/session?active_only=true".format( cblr_base=self.cblr_base)) if s["sensor_id"] == sensor_id and s["status"] in ("pending", "active") ] if len(sensor_sessions) > 0: session_id = sensor_sessions[0]["id"] else: session_id = self._create_session(sensor_id) try: res = poll_status(self._cb, "{cblr_base}/session/{0}".format( session_id, cblr_base=self.cblr_base), desired_status="active", delay=1, timeout=360) except (ObjectNotFoundError, TimeoutError): # "close" the session, otherwise it will stay in a pending state self._close_session(session_id) # the Cb server will return a 404 if we don't establish a session in time, so convert this to a "timeout" raise TimeoutError( uri="{cblr_base}/session/{0}".format(session_id, cblr_base=self.cblr_base), message="Could not establish session with sensor {0}".format( sensor_id), error_code=404) else: return session_id, res
def _get_or_create_session(self, sensor_id): session_id = self._create_session(sensor_id) try: res = poll_status(self._cb, "{cblr_base}/session/{0}".format(session_id, cblr_base=self.cblr_base), desired_status="ACTIVE", delay=1, timeout=360) except Exception: # "close" the session, otherwise it will stay in a pending state self._close_session(session_id) # the Cb server will return a 404 if we don't establish a session in time, so convert this to a "timeout" raise TimeoutError(uri="{cblr_base}/session/{0}".format(session_id, cblr_base=self.cblr_base), message="Could not establish session with sensor {0}".format(sensor_id), error_code=404) else: return session_id, res