<s> import logging <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> from . . exceptions import TransportError , HTTP_EXCEPTIONS <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> _tracer_already_configured = '<STR_LIT>' in logging . Logger . manager . loggerDict <EOL> tracer = logging . getLogger ( '<STR_LIT>' ) <EOL> if not _tracer_already_configured : <EOL> tracer . propagate = False <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> transport_schema = '<STR_LIT:http>' <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = <NUM_LIT> , url_prefix = '<STR_LIT>' , timeout = <NUM_LIT:10> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . host = '<STR_LIT>' % ( self . transport_schema , host , port ) <EOL> if url_prefix : <EOL> url_prefix = '<STR_LIT:/>' + url_prefix . strip ( '<STR_LIT:/>' ) <EOL> self . url_prefix = url_prefix <EOL> self . timeout = timeout <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . host ) <EOL> def log_request_success ( self , method , full_url , path , body , status_code , response , duration ) : <EOL> """<STR_LIT>""" <EOL> def _pretty_json ( data ) : <EOL> try : <EOL> return json . dumps ( json . loads ( data ) , sort_keys = True , indent = <NUM_LIT:2> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) . replace ( "<STR_LIT:'>" , r'<STR_LIT>' ) <EOL> except ( ValueError , TypeError ) : <EOL> return data <EOL> if body : <EOL> body = body . decode ( '<STR_LIT:utf-8>' ) <EOL> logger . info ( <EOL> '<STR_LIT>' , method , full_url , <EOL> status_code , duration <EOL> ) <EOL> logger . debug ( '<STR_LIT>' , body ) <EOL> logger . debug ( '<STR_LIT>' , response ) <EOL> if tracer . isEnabledFor ( logging . INFO ) : <EOL> path = path . replace ( '<STR_LIT:?>' , '<STR_LIT>' , <NUM_LIT:1> ) if '<STR_LIT:?>' in path else path + '<STR_LIT>' <EOL> if self . url_prefix : <EOL> path = path . replace ( self . url_prefix , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> tracer . info ( "<STR_LIT>" , method , path , _pretty_json ( body ) if body else '<STR_LIT>' ) <EOL> if tracer . isEnabledFor ( logging . DEBUG ) : <EOL> tracer . debug ( '<STR_LIT>' , status_code , duration , _pretty_json ( response ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) if response else '<STR_LIT>' ) <EOL> def log_request_fail ( self , method , full_url , body , duration , status_code = None , exception = None ) : <EOL> """<STR_LIT>""" <EOL> logger . warning ( <EOL> '<STR_LIT>' , method , full_url , <EOL> status_code or '<STR_LIT>' , duration , exc_info = exception is not None <EOL> ) <EOL> if body : <EOL> body = body . decode ( '<STR_LIT:utf-8>' ) <EOL> logger . debug ( '<STR_LIT>' , body ) <EOL> def _raise_error ( self , status_code , raw_data ) : <EOL> """<STR_LIT>""" <EOL> error_message = raw_data <EOL> additional_info = None <EOL> try : <EOL> additional_info = json . loads ( raw_data ) <EOL> error_message = additional_info . get ( '<STR_LIT:error>' , error_message ) <EOL> if isinstance ( error_message , dict ) and '<STR_LIT:type>' in error_message : <EOL> error_message = error_message [ '<STR_LIT:type>' ] <EOL> except : <EOL> pass <EOL> raise HTTP_EXCEPTIONS . get ( status_code , TransportError ) ( status_code , error_message , additional_info ) </s>
