a _b/@sddlZddlZddlZddlZddlZddlZddlZddlmZz ddl Z Wne yjddl Z Yn0ddl m Z ddlmZmZmZmZedZdejjjvZedZesde_ed ZGd d d eZdS) N)python_version)__versionstr__)HTTP_EXCEPTIONSImproperlyConfiguredOpenSearchWarningTransportErrorZ opensearchzopensearchpy.traceFz \"([^\"]*)\"c @seZdZdZd$ddZd d Zd d Zd dZddZddZ ddZ ddZ d%ddZ ddZ d&ddZddZd d!Zd"d#ZdS)' Connectiona  Class responsible for maintaining a connection to an OpenSearch 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 opensearch :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| vrl| d\} } }|durl|dkrlt|}Wnttfyt dYn0d|| f}d}|durd}n |durd }t |d si|_ |pi}|D]}|||j | <q| r| |j d <t d d kr|j dd|j dd|j d|| durR|| |j d<|rbd|j d<| dd}|s~|dkrd}d}||_|pd|_||_||_||_d|vrd||f|_nd||f|_|jdur|jd|j7_|rd|d}||_||_dS)N:utf-8$r443z$'cloud_id' is not properly formattedz%s.%sTi#headersz x-opaque-idZELASTIC_CLIENT_APIVERSIONING1acceptz4application/vnd.elasticsearch+json;compatible-with=7z content-typezapplication/jsonz user-agent authorizationz gzip,deflatezaccept-encodingschemehttphttpsFz %s://[%s]z%s://%sz:%s/)splitbinascii a2b_base64encodedecode rpartitionint ValueError IndexErrorrhasattrrlowerosgetenv setdefault_get_default_user_agent_get_api_key_header_valgetuse_ssl http_compressrhostnameporthoststrip url_prefixtimeout)selfr.r-r*r0r1rr+Zcloud_idapi_keyZ opaque_idkwargs_Z parent_dnZopensearch_uuidZ parent_portkeyrr7C/tmp/pip-target-4jja1joz/lib/python/opensearchpy/connection/base.py__init__Pst          zConnection.__init__cCsd|jj|jfS)Nz<%s: %s>) __class____name__r.r2r7r7r8__repr__szConnection.__repr__cCs*t|tstd||f||kS)Nz(Unsupported equality check for %s and %s) isinstancer TypeError__hash__)r2otherr7r7r8__eq__s zConnection.__eq__cCst|SN)idr<r7r7r8r@szConnection.__hash__cCsHt}tj|dd}||Wdn1s60Y|S)Nwb)fileobjmode)ioBytesIOgzipGzipFilewritegetvalue)r2bodybuffr7r7r8_gzip_compresss(zConnection._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)r2Zwarning_headersZwarning_messagesheadermatchesmessager7r7r8_raise_warningss   zConnection._raise_warningsc CsBz"tjt|ddddddWSttfy<|YS0dS)NTr),z: ) sort_keysindent separators'z\u0027)jsondumpsloadsreplacer r?)r2datar7r7r8 _pretty_jsonszConnection._pretty_jsonc CsttjrtjsdSd|vr,|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 isEnabledForloggingINFOhandlersrer0inforgDEBUGdebug)r2methodpathrN status_coderesponsedurationr7r7r8 _log_traces&  zConnection._log_tracer7cCs tdSrC)NotImplementedError)r2rsurlparamsrNr1ignorerr7r7r8perform_requests zConnection.perform_requestcCsj|r(z|dd}Wnty&Yn0td||||td|td||||||||dS)zLog a successful API call.rr|%s %s [status:%s request:%.3fs]> %s< %sN)rAttributeErrorloggerrprrrx)r2rsfull_urlrtrNrurvrwr7r7r8log_request_successs    zConnection.log_request_successc Cs|dkr|dkrdStjd|||p$d||dud|rZz|dd}WntyXYn0td |||||||||durtd |dS) zLog an unsuccessful API call.HEADiNr~zN/A)exc_inforr|rr)rwarningrrrrrx) r2rsrrtrNrwrurv exceptionr7r7r8log_request_fails&    zConnection.log_request_failc Cs|}d}z8|r>t|}|d|}t|tr>d|vr>|d}Wn4ttfyt}ztd|WYd}~n d}~00t |t |||dS)z*Locate appropriate exception and raise it.Nerrortypez.Undecodable raw error response from server: %s) rbrdr)r>dictr r?rrrr)r2ruraw_data error_messageZadditional_infoerrr7r7r8 _raise_error=s   " zConnection._raise_errorcCsdttfS)Nzopensearch-py/%s (Python %s))rrr<r7r7r8r'Nsz"Connection._get_default_user_agentcCsJt|ttfrBd|d|dd}dt|ddSd|S)z 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}rrirzApiKey s ) r>tuplelistformatrr b2a_base64rstripr)r2r3sr7r7r8r(Qsz"Connection._get_api_key_header_val) r NFr r NNNNN)NNNr7N)NNN)r; __module__ __qualname____doc__r9r=rBr@rQr\rgrxr}rrrr'r(r7r7r7r8r =sB V   'r )rrJrHrmr$rerWplatformrZ simplejsonrb ImportErrorr r exceptionsrrrr getLoggerrLoggermanager loggerDictZ_tracer_already_configuredrk propagatecompilerSobjectr r7r7r7r8s(