<s> """<STR_LIT>""" <EOL> import os <EOL> import json <EOL> import hmac <EOL> import hashlib <EOL> import base64 <EOL> import sys <EOL> APPENGINE = '<STR_LIT>' in os . environ <EOL> SSL_CERTIFICATE_DOMAIN = '<STR_LIT>' <EOL> try : <EOL> from urllib import urlencode <EOL> except ImportError : <EOL> from urllib . parse import urlencode <EOL> if APPENGINE : <EOL> from google . appengine . api import urlfetch <EOL> APPENGINE_METHODS = { <EOL> '<STR_LIT:GET>' : urlfetch . GET , <EOL> '<STR_LIT:POST>' : urlfetch . POST , <EOL> '<STR_LIT>' : urlfetch . PUT , <EOL> '<STR_LIT>' : urlfetch . DELETE <EOL> } <EOL> from requests import Session <EOL> from requests import exceptions <EOL> from . version import VERSION <EOL> from . index import Index <EOL> from . helpers import AlgoliaException <EOL> from . helpers import CustomJSONEncoder <EOL> from . helpers import deprecated <EOL> from . helpers import safe <EOL> from . helpers import urlify <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_id , api_key , hosts_array = None ) : <EOL> """<STR_LIT>""" <EOL> if not hosts_array : <EOL> self . read_hosts = [ '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id ] <EOL> self . write_hosts = [ '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id , <EOL> '<STR_LIT>' % app_id <EOL> ] <EOL> else : <EOL> self . read_hosts = hosts_array <EOL> self . write_hosts = hosts_array <EOL> self . _app_id = app_id <EOL> self . _api_key = api_key <EOL> self . timeout = ( <NUM_LIT:1> , <NUM_LIT:30> ) <EOL> self . search_timeout = ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> self . _session = Session ( ) <EOL> self . _session . verify = os . path . join ( os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' ) <EOL> self . _session . headers = { <EOL> '<STR_LIT>' : self . api_key , <EOL> '<STR_LIT>' : self . app_id , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % VERSION <EOL> } <EOL> if '<STR_LIT>' in sys . modules . keys ( ) : <EOL> self . _session . headers . pop ( '<STR_LIT>' , None ) <EOL> @ property <EOL> def app_id ( self ) : <EOL> return self . _app_id <EOL> @ property <EOL> def api_key ( self ) : <EOL> return self . _api_key <EOL> @ api_key . setter <EOL> def api_key ( self , value ) : <EOL> self . _api_key = value <EOL> self . set_extra_headers ( ** { '<STR_LIT>' : value } ) <EOL> @ deprecated <EOL> def enableRateLimitForward ( self , admin_api_key , end_user_ip , <EOL> rate_limit_api_key ) : <EOL> return self . enable_rate_limit_forward ( end_user_ip , rate_limit_api_key ) <EOL> def enable_rate_limit_forward ( self , end_user_ip , rate_limit_api_key ) : <EOL> """<STR_LIT>""" <EOL> self . headers . update ( { <EOL> '<STR_LIT>' : end_user_ip , <EOL> '<STR_LIT>' : rate_limit_api_key , <EOL> } ) <EOL> def set_end_user_ip ( self , end_user_ip ) : <EOL> """<STR_LIT>""" <EOL> self . headers [ '<STR_LIT>' ] = end_user_ip <EOL> @ deprecated <EOL> def disableRateLimitForward ( self ) : <EOL> return self . disable_rate_limit_forward ( ) <EOL> def disable_rate_limit_forward ( self ) : <EOL> """<STR_LIT>""" <EOL> self . headers . pop ( '<STR_LIT>' , None ) <EOL> self . headers . pop ( '<STR_LIT>' , None ) <EOL> @ deprecated <EOL> def set_extra_header ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> self . set_extra_headers ( ** { key : value } ) <EOL> def set_extra_headers ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . headers . update ( kwargs ) <EOL> @ property <EOL> def headers ( self ) : <EOL> return self . _session . headers <EOL> @ deprecated <EOL> def set_timeout ( self , connect_timeout , read_timeout , search_timeout = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> self . timeout = ( connect_timeout , read_timeout ) <EOL> self . search_timeout = ( connect_timeout , search_timeout ) <EOL> @ deprecated <EOL> def multipleQueries ( self , queries , index_name_key = '<STR_LIT>' ) : <EOL> return self . multiple_queries ( queries , index_name_key ) <EOL> def multiple_queries ( self , queries , <EOL> index_name_key = '<STR_LIT>' , <EOL> strategy = '<STR_LIT:none>' ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' <EOL> params = { '<STR_LIT>' : strategy } <EOL> requests = [ ] <EOL> for query in queries : <EOL> index_name = query . pop ( index_name_key ) <EOL> requests . append ( { <EOL> '<STR_LIT>' : index_name , <EOL> '<STR_LIT>' : urlencode ( urlify ( query ) ) <EOL> } ) <EOL> return self . _perform_request ( <EOL> self . read_hosts , path , '<STR_LIT:POST>' , params = params , <EOL> body = { '<STR_LIT>' : requests } , is_search = True ) <EOL> def batch ( self , requests ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( requests , ( list , tuple ) ) : <EOL> requests = { '<STR_LIT>' : requests } <EOL> return self . _perform_request ( self . write_hosts , '<STR_LIT>' , <EOL> '<STR_LIT:POST>' , body = requests ) <EOL> @ deprecated <EOL> def listIndexes ( self ) : <EOL> return self . list_indexes ( ) <EOL> def list_indexes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _perform_request ( self . read_hosts , '<STR_LIT>' , '<STR_LIT:GET>' ) <EOL> @ deprecated <EOL> def deleteIndex ( self , index_name ) : <EOL> return self . delete_index ( index_name ) <EOL> def delete_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % safe ( index_name ) <EOL> return self . _perform_request ( self . write_hosts , path , '<STR_LIT>' ) <EOL> @ deprecated <EOL> def moveIndex ( self , src_index_name , dst_index_name ) : <EOL> return self . move_index ( src_index_name , dst_index_name ) <EOL> def move_index ( self , src_index_name , dst_index_name ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % safe ( src_index_name ) <EOL> request = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dst_index_name } <EOL> return self . _perform_request ( self . write_hosts , path , '<STR_LIT:POST>' , <EOL> body = request ) <EOL> @ deprecated <EOL> def copyIndex ( self , src_index_name , dst_index_name ) : <EOL> return self . copy_index ( src_index_name , dst_index_name ) <EOL> def copy_index ( self , src_index_name , dst_index_name ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % safe ( src_index_name ) <EOL> request = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : dst_index_name } <EOL> return self . _perform_request ( self . write_hosts , path , '<STR_LIT:POST>' , <EOL> body = request ) <EOL> @ deprecated <EOL> def getLogs ( self , offset = <NUM_LIT:0> , length = <NUM_LIT:10> , type = '<STR_LIT:all>' ) : <EOL> return self . get_logs ( offset , length , type ) <EOL> def get_logs ( self , offset = <NUM_LIT:0> , length = <NUM_LIT:10> , type = '<STR_LIT:all>' ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> '<STR_LIT>' : offset , <EOL> '<STR_LIT>' : length , <EOL> '<STR_LIT:type>' : type <EOL> } <EOL> return self . _perform_request ( self . write_hosts , '<STR_LIT>' , '<STR_LIT:GET>' , <EOL> params = params ) <EOL> @ deprecated <EOL> def initIndex ( self , index_name ) : <EOL> return self . init_index ( index_name ) <EOL> def init_index ( self , index_name ) : <EOL> """<STR_LIT>""" <EOL> return Index ( self , index_name ) <EOL> @ deprecated <EOL> def listUserKeys ( self ) : <EOL> return self . list_user_keys ( ) <EOL> def list_user_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _perform_request ( self . read_hosts , '<STR_LIT>' , '<STR_LIT:GET>' ) <EOL> @ deprecated <EOL> def getUserKeyACL ( self , key ) : <EOL> return self . get_user_key_acl ( key ) <EOL> def get_user_key_acl ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % key <EOL> return self . _perform_request ( self . read_hosts , path , '<STR_LIT:GET>' ) <EOL> @ deprecated <EOL> def deleteUserKey ( self , key ) : <EOL> return self . delete_user_key ( key ) <EOL> def delete_user_key ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = '<STR_LIT>' % key <EOL> return self . _perform_request ( self . write_hosts , path , '<STR_LIT>' ) <EOL> @ deprecated <EOL> def addUserKey ( self , obj , <EOL> validity = <NUM_LIT:0> , <EOL> max_queries_per_ip_per_hour = <NUM_LIT:0> , <EOL> max_hits_per_query = <NUM_LIT:0> , <EOL> indexes = None ) : <EOL> return self . add_user_key ( obj , validity , max_queries_per_ip_per_hour , <EOL> max_hits_per_query , indexes ) <EOL> def add_user_key ( self , obj , <EOL> validity = <NUM_LIT:0> , <EOL> max_queries_per_ip_per_hour = <NUM_LIT:0> , <EOL> max_hits_per_query = <NUM_LIT:0> , <EOL> indexes = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , dict ) : <EOL> obj = { '<STR_LIT>' : obj } <EOL> obj . update ( { <EOL> '<STR_LIT>' : validity , <EOL> '<STR_LIT>' : max_queries_per_ip_per_hour , <EOL> '<STR_LIT>' : max_hits_per_query <EOL> } ) <EOL> if indexes : <EOL> obj [ '<STR_LIT>' ] = indexes <EOL> return self . _perform_request ( self . write_hosts , '<STR_LIT>' , '<STR_LIT:POST>' , <EOL> body = obj ) <EOL> def update_user_key ( self , key , obj , <EOL> validity = None , <EOL> max_queries_per_ip_per_hour = None , <EOL> max_hits_per_query = None , <EOL> indexes = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( obj , dict ) : <EOL> obj = { '<STR_LIT>' : obj } <EOL> if validity is not None : <EOL> obj [ '<STR_LIT>' ] = validity <EOL> if max_queries_per_ip_per_hour is not None : <EOL> obj [ '<STR_LIT>' ] = max_queries_per_ip_per_hour <EOL> if max_hits_per_query is not None : <EOL> obj [ '<STR_LIT>' ] = max_hits_per_query <EOL> if indexes : <EOL> obj [ '<STR_LIT>' ] = indexes <EOL> path = '<STR_LIT>' % key <EOL> return self . _perform_request ( self . write_hosts , path , '<STR_LIT>' , <EOL> body = obj ) <EOL> @ deprecated <EOL> def generateSecuredApiKey ( self , private_api_key , tag_filters , <EOL> user_token = '<STR_LIT>' ) : <EOL> return self . generate_secured_api_key ( private_api_key , tag_filters , <EOL> user_token ) <EOL> def generate_secured_api_key ( self , private_api_key , queryParameters , <EOL> user_token = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( queryParameters , ( list , tuple ) ) : <EOL> queryParameters = '<STR_LIT:U+002C>' . join ( <EOL> map ( lambda t : '<STR_LIT>' . join ( [ '<STR_LIT:(>' , '<STR_LIT:U+002C>' . join ( t ) , '<STR_LIT:)>' ] ) if <EOL> isinstance ( t , ( list , tuple ) ) else t , queryParameters ) ) <EOL> queryParameters = { '<STR_LIT>' : queryParameters } <EOL> elif not isinstance ( queryParameters , dict ) and not '<STR_LIT:=>' in queryParameters : <EOL> queryParameters = { '<STR_LIT>' : queryParameters } <EOL> if isinstance ( queryParameters , dict ) : <EOL> if user_token != None and user_token != '<STR_LIT>' : <EOL> queryParameters [ '<STR_LIT>' ] = user_token <EOL> queryParameters = urlencode ( urlify ( queryParameters ) ) <EOL> securedKey = hmac . new ( private_api_key . encode ( '<STR_LIT:utf-8>' ) , queryParameters . encode ( '<STR_LIT:utf-8>' ) , hashlib . sha256 ) . hexdigest ( ) <EOL> return str ( base64 . b64encode ( ( "<STR_LIT>" % ( securedKey , queryParameters ) ) . encode ( '<STR_LIT:utf-8>' ) ) . decode ( '<STR_LIT:utf-8>' ) ) <EOL> def _perform_appengine_request ( self , host , path , method , timeout , params = None , data = None ) : <EOL> """<STR_LIT>""" <EOL> method = APPENGINE_METHODS . get ( method ) <EOL> if isinstance ( timeout , tuple ) : <EOL> timeout = timeout [ <NUM_LIT:1> ] <EOL> url = '<STR_LIT>' % ( host , path ) <EOL> url = params and '<STR_LIT>' % ( url , urlencode ( urlify ( params ) ) ) or url <EOL> res = urlfetch . fetch ( url = url , method = method , payload = data , <EOL> headers = self . headers , deadline = timeout , <EOL> validate_certificate = host . endswith ( SSL_CERTIFICATE_DOMAIN ) ) <EOL> content = res . content != None and json . loads ( res . content ) or None <EOL> if ( int ( res . status_code / <NUM_LIT:100> ) == <NUM_LIT:2> and content ) : <EOL> return content <EOL> elif ( int ( res . status_code / <NUM_LIT:100> ) == <NUM_LIT:4> ) : <EOL> message = "<STR_LIT>" % res . status_code <EOL> if content and content . get ( '<STR_LIT:message>' ) : <EOL> message = content [ '<STR_LIT:message>' ] <EOL> raise AlgoliaException ( message ) <EOL> else : <EOL> mesage = '<STR_LIT>' % ( res . status_code , res . content ) <EOL> raise Exception ( http_error_msg , response = res ) <EOL> def _perform_session_request ( self , host , path , method , timeout , params = None , data = None ) : <EOL> """<STR_LIT>""" <EOL> res = self . _session . request ( <EOL> method , '<STR_LIT>' % ( host , path ) , <EOL> params = params , data = data , timeout = timeout ) <EOL> if ( int ( res . status_code / <NUM_LIT:100> ) == <NUM_LIT:2> and res . json != None ) : <EOL> return res . json ( ) <EOL> elif ( int ( res . status_code / <NUM_LIT:100> ) == <NUM_LIT:4> ) : <EOL> message = "<STR_LIT>" % res . status_code <EOL> if res . json != None and '<STR_LIT:message>' in res . json ( ) : <EOL> message = res . json ( ) [ '<STR_LIT:message>' ] <EOL> raise AlgoliaException ( message ) <EOL> res . raise_for_status ( ) <EOL> def _perform_request ( self , hosts , path , method , params = None , body = None , <EOL> is_search = False ) : <EOL> """<STR_LIT>""" <EOL> if params : <EOL> params = urlify ( params ) <EOL> if body : <EOL> body = json . dumps ( body , cls = CustomJSONEncoder ) <EOL> timeout = self . search_timeout if is_search else self . timeout <EOL> exceptions_hosts = { } <EOL> for i , host in enumerate ( hosts ) : <EOL> if i > <NUM_LIT:1> : <EOL> if isinstance ( timeout , tuple ) : <EOL> timeout = ( timeout [ <NUM_LIT:0> ] + <NUM_LIT:2> , timeout [ <NUM_LIT:1> ] + <NUM_LIT:10> ) <EOL> else : <EOL> timeout += <NUM_LIT:10> <EOL> try : <EOL> _request = APPENGINE and self . _perform_appengine_request or self . _perform_session_request <EOL> return _request ( host , path , method , timeout , params = params , data = body ) <EOL> except AlgoliaException as e : <EOL> raise e <EOL> except Exception as e : <EOL> exceptions_hosts [ host ] = "<STR_LIT>" % ( e . __class__ . __name__ , str ( e ) ) <EOL> pass <EOL> raise AlgoliaException ( '<STR_LIT>' % ( '<STR_LIT>' , <EOL> exceptions_hosts ) ) </s>
