<s> """<STR_LIT>""" <EOL> import httplib2 <EOL> import warnings <EOL> import oauth2 as oauth <EOL> import requests <EOL> from requests_oauthlib import OAuth1Session <EOL> try : <EOL> from urllib . parse import urlparse , parse_qsl , urlencode <EOL> except ImportError : <EOL> from urlparse import urlparse , parse_qsl <EOL> from urllib import urlencode <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> ACCESS_TOKEN_URL = '<STR_LIT>' <EOL> RESOURCE_URL = '<STR_LIT>' <EOL> IMPORTS_URL = '<STR_LIT>' <EOL> def proxyinfo2proxies ( proxy_info ) : <EOL> """<STR_LIT>""" <EOL> proxies = { } <EOL> if proxy_info . proxy_user and proxy_info . proxy_pass : <EOL> credentials = "<STR_LIT>" . format ( user = proxy_info . proxy_user , password = proxy_info . proxy_pass ) <EOL> elif proxy_info . proxy_user : <EOL> credentials = "<STR_LIT>" . format ( user = proxy_info . proxy_user ) <EOL> else : <EOL> credentials = '<STR_LIT>' <EOL> port = "<STR_LIT>" . format ( port = proxy_info . proxy_port ) if proxy_info . proxy_port else '<STR_LIT>' <EOL> proxy_url = "<STR_LIT>" . format ( credentials = credentials , host = proxy_info . proxy_host , port = port ) <EOL> if proxy_info . applies_to ( "<STR_LIT:http>" ) : <EOL> proxies [ "<STR_LIT:http>" ] = proxy_url <EOL> if proxy_info . applies_to ( "<STR_LIT>" ) : <EOL> proxies [ "<STR_LIT>" ] = proxy_url <EOL> return proxies <EOL> def proxies2proxyinfo ( proxies ) : <EOL> """<STR_LIT>""" <EOL> url_components = urlparse ( proxies [ "<STR_LIT:http>" ] ) if "<STR_LIT:http>" in proxies else urlparse ( proxies [ "<STR_LIT>" ] ) <EOL> return httplib2 . ProxyInfo ( httplib2 . socks . PROXY_TYPE_HTTP_NO_TUNNEL , url_components . hostname , url_components . port , <EOL> proxy_user = url_components . username , proxy_pass = url_components . password ) <EOL> class CartoDBException ( Exception ) : <EOL> pass <EOL> class CartoDBBase ( object ) : <EOL> """<STR_LIT>""" <EOL> MAX_GET_QUERY_LEN = <NUM_LIT> <EOL> def __init__ ( self , cartodb_domain , host = '<STR_LIT>' , protocol = '<STR_LIT>' , api_version = None , proxy_info = None , sql_api_version = '<STR_LIT>' , import_api_version = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if api_version is None : <EOL> api_version = sql_api_version <EOL> self . resource_url = RESOURCE_URL % { '<STR_LIT:user>' : cartodb_domain , '<STR_LIT>' : host , '<STR_LIT>' : protocol , '<STR_LIT>' : api_version } <EOL> self . imports_url = IMPORTS_URL % { '<STR_LIT:user>' : cartodb_domain , '<STR_LIT>' : host , '<STR_LIT>' : protocol , '<STR_LIT>' : import_api_version } <EOL> self . host = host <EOL> self . protocol = protocol <EOL> self . api_version = api_version <EOL> if type ( proxy_info ) == httplib2 . ProxyInfo : <EOL> self . proxy_info = proxy_info <EOL> self . proxies = proxyinfo2proxies ( self . proxy_info ) <EOL> if type ( proxy_info ) == dict : <EOL> self . proxies = proxy_info <EOL> self . proxy_info = proxies2proxyinfo ( self . proxies ) <EOL> else : <EOL> self . proxy_info = None <EOL> self . proxies = None <EOL> def req ( self , url , http_method = "<STR_LIT:GET>" , http_headers = None , body = None , params = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_response_data ( self , resp , parse_json = True ) : <EOL> """<STR_LIT>""" <EOL> if resp . status_code == requests . codes . ok : <EOL> if parse_json : <EOL> return resp . json ( ) <EOL> return resp . content <EOL> elif resp . status_code == requests . codes . bad_request : <EOL> r = resp . json ( ) <EOL> raise CartoDBException ( r . get ( '<STR_LIT:error>' , False ) or r . get ( '<STR_LIT>' , '<STR_LIT>' + resp . text ) ) <EOL> elif resp . status_code == requests . codes . not_found : <EOL> raise CartoDBException ( '<STR_LIT>' + resp . url ) <EOL> elif resp . status_code == requests . codes . internal_server_error : <EOL> raise CartoDBException ( '<STR_LIT>' ) <EOL> elif resp . status_code == requests . codes . unauthorized or resp . status_code == requests . codes . forbidden : <EOL> raise CartoDBException ( '<STR_LIT>' ) <EOL> else : <EOL> raise CartoDBException ( '<STR_LIT>' ) <EOL> def sql ( self , sql , parse_json = True , do_post = True , format = None ) : <EOL> """<STR_LIT>""" <EOL> params = { '<STR_LIT:q>' : sql } <EOL> if format : <EOL> params [ '<STR_LIT>' ] = format <EOL> if format not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> parse_json = False <EOL> url = self . resource_url <EOL> if len ( sql ) < self . MAX_GET_QUERY_LEN and not do_post : <EOL> resp = self . req ( url , '<STR_LIT:GET>' , params = params ) <EOL> else : <EOL> resp = self . req ( url , '<STR_LIT:POST>' , body = params ) <EOL> return self . get_response_data ( resp , parse_json ) <EOL> class CartoDBOAuth ( CartoDBBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , secret , email , password , cartodb_domain , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( CartoDBOAuth , self ) . __init__ ( cartodb_domain , ** kwargs ) <EOL> self . consumer_key = key <EOL> self . consumer_secret = secret <EOL> consumer = oauth . Consumer ( self . consumer_key , self . consumer_secret ) <EOL> client = oauth . Client ( consumer , proxy_info = self . proxy_info ) <EOL> client . set_signature_method = oauth . SignatureMethod_HMAC_SHA1 ( ) <EOL> params = { } <EOL> params [ "<STR_LIT>" ] = email <EOL> params [ "<STR_LIT>" ] = password <EOL> params [ "<STR_LIT>" ] = '<STR_LIT>' <EOL> access_token_url = ACCESS_TOKEN_URL % { '<STR_LIT:user>' : cartodb_domain , '<STR_LIT>' : self . host , '<STR_LIT>' : self . protocol } <EOL> resp , token = client . request ( access_token_url , method = "<STR_LIT:POST>" , body = urlencode ( params ) ) <EOL> if resp [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> raise CartoDBException ( "<STR_LIT>" % ( resp [ '<STR_LIT:status>' ] , token ) ) <EOL> access_token = dict ( parse_qsl ( token . decode ( ) ) ) <EOL> try : <EOL> self . client = OAuth1Session ( self . consumer_key , client_secret = self . consumer_secret , resource_owner_key = access_token [ '<STR_LIT>' ] , <EOL> resource_owner_secret = access_token [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> raise CartoDBException ( '<STR_LIT>' ) <EOL> def req ( self , url , http_method = "<STR_LIT:GET>" , http_headers = None , body = None , params = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> return self . client . request ( http_method . lower ( ) , url , params = params , data = body , headers = http_headers , proxies = self . proxies , files = files ) <EOL> class CartoDBAPIKey ( CartoDBBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , api_key , cartodb_domain , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( CartoDBAPIKey , self ) . __init__ ( cartodb_domain , ** kwargs ) <EOL> self . api_key = api_key <EOL> self . client = requests <EOL> if self . protocol != '<STR_LIT>' : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> def req ( self , url , http_method = "<STR_LIT:GET>" , http_headers = None , body = None , params = None , files = None ) : <EOL> """<STR_LIT>""" <EOL> params = params or { } <EOL> params . update ( { "<STR_LIT>" : self . api_key } ) <EOL> return self . client . request ( http_method . lower ( ) , url , params = params , data = body , headers = http_headers , proxies = self . proxies , files = files ) </s>
