<s> from __future__ import absolute_import <EOL> from socket import timeout as SocketTimeout <EOL> from socket import error as SocketError <EOL> import time <EOL> import logging <EOL> try : <EOL> from . esthrift import Rest <EOL> from . esthrift . ttypes import Method , RestRequest <EOL> from thrift . transport import TTransport , TSocket , TSSLSocket <EOL> from thrift . protocol import TBinaryProtocol <EOL> from thrift . Thrift import TException <EOL> THRIFT_AVAILABLE = True <EOL> except ImportError : <EOL> THRIFT_AVAILABLE = False <EOL> from . . exceptions import ConnectionError , ImproperlyConfigured , ConnectionTimeout <EOL> from . pooling import PoolingConnection <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class ThriftConnection ( PoolingConnection ) : <EOL> """<STR_LIT>""" <EOL> transport_schema = '<STR_LIT>' <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , framed_transport = False , use_ssl = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not THRIFT_AVAILABLE : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" ) <EOL> super ( ThriftConnection , self ) . __init__ ( host = host , port = port , ** kwargs ) <EOL> self . _framed_transport = framed_transport <EOL> self . _tsocket_class = TSocket . TSocket <EOL> if use_ssl : <EOL> self . _tsocket_class = TSSLSocket . TSSLSocket <EOL> self . _tsocket_args = ( host , port ) <EOL> def _make_connection ( self ) : <EOL> socket = self . _tsocket_class ( * self . _tsocket_args ) <EOL> socket . setTimeout ( self . timeout * <NUM_LIT> ) <EOL> if self . _framed_transport : <EOL> transport = TTransport . TFramedTransport ( socket ) <EOL> else : <EOL> transport = TTransport . TBufferedTransport ( socket ) <EOL> protocol = TBinaryProtocol . TBinaryProtocolAccelerated ( transport ) <EOL> client = Rest . Client ( protocol ) <EOL> client . transport = transport <EOL> transport . open ( ) <EOL> return client <EOL> def perform_request ( self , method , url , params = None , body = None , timeout = None , ignore = ( ) ) : <EOL> request = RestRequest ( method = Method . _NAMES_TO_VALUES [ method . upper ( ) ] , uri = url , <EOL> parameters = params , body = body ) <EOL> start = time . time ( ) <EOL> tclient = None <EOL> try : <EOL> tclient = self . _get_connection ( ) <EOL> response = tclient . execute ( request ) <EOL> duration = time . time ( ) - start <EOL> except SocketTimeout as e : <EOL> self . log_request_fail ( method , url , body , time . time ( ) - start , exception = e ) <EOL> raise ConnectionTimeout ( '<STR_LIT>' , str ( e ) , e ) <EOL> except ( TException , SocketError ) as e : <EOL> self . log_request_fail ( method , url , body , time . time ( ) - start , exception = e ) <EOL> if tclient : <EOL> try : <EOL> tclient . transport . close ( ) <EOL> except Exception as e : <EOL> logger . warning ( <EOL> '<STR_LIT>' , <EOL> e , exc_info = True <EOL> ) <EOL> raise ConnectionError ( '<STR_LIT>' , str ( e ) , e ) <EOL> self . _release_connection ( tclient ) <EOL> if not ( <NUM_LIT:200> <= response . status < <NUM_LIT> ) and response . status not in ignore : <EOL> self . log_request_fail ( method , url , body , duration , response . status ) <EOL> self . _raise_error ( response . status , response . body ) <EOL> self . log_request_success ( method , url , url , body , response . status , <EOL> response . body , duration ) <EOL> headers = { } <EOL> if response . headers : <EOL> headers = dict ( ( k . lower ( ) , v ) for k , v in response . headers . items ( ) ) <EOL> return response . status , headers , response . body or '<STR_LIT>' </s>
