def construct_session(session_obj=None): # NOTE(morganfainberg): if the logic in this function changes be sure to # update the betamax fixture's '_construct_session_with_betamax" function # as well. if not session_obj: session_obj = requests.Session() # Use TCPKeepAliveAdapter to fix bug 1323862 for scheme in list(session_obj.adapters): session_obj.mount(scheme, TCPKeepAliveAdapter()) return session_obj
def _request(self, method, url, params=None, body=None, async_=False, **kwargs): """Transmit the request to REST API server.""" kwargs.setdefault('ignore_error', []) kwargs['no_retry_error'] = (kwargs['ignore_error'] + REST_NO_RETRY_ERRORS) kwargs.setdefault('no_retry', False) kwargs.setdefault('do_raise', True) kwargs.setdefault('ignore_message_id', []) kwargs.setdefault('no_relogin', False) kwargs.setdefault('ignore_return_code', []) kwargs.setdefault('ignore_all_errors', False) kwargs.setdefault('timeout_message', None) kwargs.setdefault('no_log', False) kwargs.setdefault('timeout', _EXEC_MAX_WAITTIME) headers = dict(self.headers) if async_: read_timeout = (_JOB_API_RESPONSE_TIMEOUT + _RESPONSE_TIMEOUT_TOLERANCE) headers.update({ "Response-Max-Wait": str(_JOB_API_RESPONSE_TIMEOUT), "Response-Job-Status": "Completed;"}) else: read_timeout = _GET_API_RESPONSE_TIMEOUT auth_data = kwargs.get('auth', self.get_my_session()) timeout = (self.connect_timeout, read_timeout) interval = kwargs.get('interval', _EXEC_RETRY_INTERVAL) retry = True start_time = timeutils.utcnow() watch = timeutils.StopWatch() while retry: watch.restart() try: with requests.Session() as session: if self.tcp_keepalive: session.mount(_HTTPS, TCPKeepAliveAdapter()) rsp = session.request(method, url, params=params, json=body, headers=headers, auth=auth_data, timeout=timeout, verify=self.verify) except Exception as e: msg = utils.output_log( MSG.REST_SERVER_CONNECT_FAILED, exception=type(e), message=e, method=method, url=url, params=params, body=body) raise utils.HBSDError(msg) response = ResponseData(rsp) if (response['status_code'] == httpclient.INTERNAL_SERVER_ERROR and kwargs['timeout'] < _REST_SERVER_RESTART_TIMEOUT): kwargs['timeout'] = _REST_SERVER_RESTART_TIMEOUT if (response['status_code'] == httpclient.SERVICE_UNAVAILABLE and kwargs['timeout'] < _REST_SERVER_ERROR_TIMEOUT): kwargs['timeout'] = _REST_SERVER_ERROR_TIMEOUT retry, rsp_data, errobj = self._check_rest_api_response( response, start_time, method=method, url=url, params=params, body=body, **kwargs) if retry: watch.stop() idle = max(interval - watch.elapsed(), 0) greenthread.sleep(idle) if not kwargs['no_relogin'] and response.is_auth_fail(): auth_data = self.get_my_session() return rsp_data, errobj