U Ec_w,@sddlZddlZddlZddlZddlZddlmZddlZz ddlZ Wne k rdddl Z YnXddl m Z m Z mZmZddlmZedZdejjjkZedZesde_ed ZGd d d eZdS) N)python_version)TransportErrorImproperlyConfiguredElasticsearchDeprecationWarningHTTP_EXCEPTIONS)__versionstr__Z elasticsearchzelasticsearch.traceFz \"([^\"]*)\"c @s|eZdZdZd!ddZd d Zd d Zd dZddZddZ ddZ ddZ ddZ d"ddZ ddZddZdd ZdS)# Connectiona Class responsible for maintaining a connection to an Elasticsearch node. It holds persistent connection pool to it and it's main interface (`perform_request`) is thread-safe. Also responsible for logging. :arg host: hostname of the node (default: localhost) :arg port: port to use (integer, default: 9200) :arg use_ssl: use ssl for the connection if `True` :arg url_prefix: optional url prefix for elasticsearch :arg timeout: default timeout in seconds (float, default: 10) :arg http_compress: Use gzip compression :arg cloud_id: The Cloud ID from ElasticCloud. Convenient way to connect to cloud instances. :arg opaque_id: Send this value in the 'X-Opaque-Id' HTTP header For tracing all requests made by this transport. localhostNF c  Ks|rzj|d\} }t|ddddd\} }d| krl| d\} } }|dkrl|dkrlt|}Wn ttfk rt dYnXd|| f}d}|dkrd}n |dkrd }t |d si|_ |pi}|D]}|||j | <q| r| |j d <|j d d |j d|| dk r6|| |j d<|rFd|j d<| dd}|sb|dkrjd}d}||_|pxd|_||_||_||_d||f|_|jdk r|jd|j7_|rd|d}||_||_dS)N:utf-8$r443z$'cloud_id' is not properly formattedz%s.%sTi#headersz x-opaque-idz content-typezapplication/jsonz user-agent authorizationz gzip,deflatezaccept-encodingschemehttphttpsFz%s://%sz:%s/)splitbinascii a2b_base64encodedecode rpartitionint ValueError IndexErrorrhasattrrlower setdefault_get_default_user_agent_get_api_key_header_valgetuse_ssl http_compressrhostnameporthoststrip url_prefixtimeout)selfr*r)r&r,r-rr'Zcloud_idapi_keyZ opaque_idkwargs_Z parent_dnZes_uuidZ parent_portkeyrr3D/tmp/pip-target-m1bursmm/lib/python/elasticsearch/connection/base.py__init__Fsh         zConnection.__init__cCsd|jj|jfS)Nz<%s: %s>) __class____name__r*r.r3r3r4__repr__szConnection.__repr__cCs*t|tstd||f||kS)Nz(Unsupported equality check for %s and %s) isinstancer TypeError__hash__)r.otherr3r3r4__eq__s zConnection.__eq__cCst|S)N)idr8r3r3r4r<szConnection.__hash__c Cs4t}tj|dd}||W5QRX|S)Nwb)fileobjmode)ioBytesIOgzipGzipFilewritegetvalue)r.bodybuffr3r3r4_gzip_compressszConnection._gzip_compresscCsV|sdSg}|D](}t|}|r.||q||q|D]}tj|tdq>dS)zIf 'headers' contains a 'Warning' header raise the warnings to be seen by the user. Takes an iterable of string values from any number of 'Warning' headers. N)category) _WARNING_REfindallextendappendwarningswarnr)r.Zwarning_headersZwarning_messagesheadermatchesmessager3r3r4_raise_warningss   zConnection._raise_warningsc CsDz"tjt|ddddddWSttfk r>|YSXdS)NTr),z: ) sort_keysindent separators'z\u0027)jsondumpsloadsreplacerr;)r.datar3r3r4 _pretty_jsonszConnection._pretty_jsonc CsttjrtjsdSd|kr,|dddn|d}|jrJ||jdd}td|rXdnd|||rl||ndttj rt d|||r||d d nddS) N?z?pretty&z?prettyr z-curl %s-X%s 'http://localhost:9200%s' -d '%s'z$-H 'Content-Type: application/json' z#[%s] (%.3fs) #%s z #) tracer isEnabledForloggingINFOhandlersr`r,inforbDEBUGdebug)r.methodpathrI status_coderesponsedurationr3r3r4 _log_traces&  zConnection._log_tracecCsl|r*z|dd}Wntk r(YnXtd||||td|td||||||||dS)z Log a successful API call. rignore%s %s [status:%s request:%.3fs]> %s< %sN)rAttributeErrorloggerrkrmrs)r.rnfull_urlrorIrprqrrr3r3r4log_request_successs  zConnection.log_request_successc Cs|dkr|dkrdStjd|||p$d||dk d|r\z|dd}Wntk rZYnXtd |||||||||dk rtd |dS) z Log an unsuccessful API call. HEADiNruzN/A)exc_inforrtrvrw)rywarningrrxrmrs) r.rnrzrorIrrrprq exceptionr3r3r4log_request_fails&   zConnection.log_request_failc Cs|}d}z8|r>t|}|d|}t|tr>d|kr>|d}Wn2ttfk rr}ztd|W5d}~XYnXt |t |||dS)z, Locate appropriate exception and raise it. Nerrortypez.Undecodable raw error response from server: %s) r]r_r%r:dictrr;ryr~rr)r.rpraw_data error_messageZadditional_infoerrr3r3r4 _raise_errors    zConnection._raise_errorcCsdttfS)Nzelasticsearch-py/%s (Python %s))rrr8r3r3r4r#0sz"Connection._get_default_user_agentcCsJt|ttfrBd|d|dd}dt|ddSd|S)a& Check the type of the passed api_key and return the correct header value for the `API Key authentication ` :arg api_key, either a tuple or a base64 encoded string z{0}:{1}rrdrzApiKey s ) r:tuplelistformatrr b2a_base64rstripr)r.r/sr3r3r4r$3sz"Connection._get_api_key_header_val) r NFr r NNNNN)NNN)r7 __module__ __qualname____doc__r5r9r>r<rLrWrbrsr{rrr#r$r3r3r3r4r 3s6 N  'r )rhrrErCreplatformrrRZ simplejsonr] ImportError exceptionsrrrrr r getLoggerryLoggermanager loggerDictZ_tracer_already_configuredrf propagatecompilerNobjectr r3r3r3r4s&