<s> """<STR_LIT>""" <EOL> import cStringIO <EOL> import logging <EOL> import os <EOL> import re <EOL> import urllib <EOL> import urllib2 <EOL> import httplib2 <EOL> from oauth2client import client <EOL> from oauth2client import file as oauth2client_file <EOL> from oauth2client import tools <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class Error ( Exception ) : <EOL> pass <EOL> class AuthPermanentFail ( Error ) : <EOL> """<STR_LIT>""" <EOL> class MemoryCache ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . cache = { } <EOL> def get ( self , key ) : <EOL> return self . cache . get ( key ) <EOL> def set ( self , key , value ) : <EOL> self . cache [ key ] = value <EOL> def delete ( self , key ) : <EOL> self . cache . pop ( key , None ) <EOL> def RaiseHttpError ( url , response_info , response_body , extra_msg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if response_body is not None : <EOL> stream = cStringIO . StringIO ( ) <EOL> stream . write ( response_body ) <EOL> stream . seek ( <NUM_LIT:0> ) <EOL> else : <EOL> stream = None <EOL> if not extra_msg : <EOL> msg = response_info . reason <EOL> else : <EOL> msg = response_info . reason + '<STR_LIT:U+0020>' + extra_msg <EOL> raise urllib2 . HTTPError ( url , response_info . status , msg , response_info , stream ) <EOL> class HttpRpcServerHttpLib2 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , auth_function , user_agent , source , <EOL> host_override = None , extra_headers = None , save_cookies = False , <EOL> auth_tries = None , account_type = None , debug_data = True , secure = True , <EOL> ignore_certs = False , rpc_tries = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . auth_function = auth_function <EOL> self . user_agent = user_agent <EOL> self . source = source <EOL> self . host_override = host_override <EOL> self . extra_headers = extra_headers or { } <EOL> self . save_cookies = save_cookies <EOL> self . auth_tries = auth_tries <EOL> self . account_type = account_type <EOL> self . debug_data = debug_data <EOL> self . secure = secure <EOL> self . ignore_certs = ignore_certs <EOL> self . rpc_tries = rpc_tries <EOL> self . scheme = secure and '<STR_LIT>' or '<STR_LIT:http>' <EOL> self . certpath = None <EOL> self . cert_file_available = False <EOL> if not self . ignore_certs : <EOL> self . certpath = os . path . normpath ( os . path . join ( <EOL> os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT:..>' , '<STR_LIT:..>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> self . cert_file_available = os . path . exists ( self . certpath ) <EOL> self . memory_cache = MemoryCache ( ) <EOL> def _Authenticate ( self , http , saw_error ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def Send ( self , request_path , payload = '<STR_LIT>' , <EOL> content_type = '<STR_LIT>' , <EOL> timeout = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . http = httplib2 . Http ( <EOL> cache = self . memory_cache , ca_certs = self . certpath , <EOL> disable_ssl_certificate_validation = ( not self . cert_file_available ) ) <EOL> self . http . follow_redirects = False <EOL> self . http . timeout = timeout <EOL> url = '<STR_LIT>' % ( self . scheme , self . host , request_path ) <EOL> if kwargs : <EOL> url += '<STR_LIT:?>' + urllib . urlencode ( sorted ( kwargs . items ( ) ) ) <EOL> headers = { } <EOL> if self . extra_headers : <EOL> headers . update ( self . extra_headers ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> if payload is not None : <EOL> method = '<STR_LIT:POST>' <EOL> headers [ '<STR_LIT>' ] = str ( len ( payload ) ) <EOL> headers [ '<STR_LIT:Content-Type>' ] = content_type <EOL> else : <EOL> method = '<STR_LIT:GET>' <EOL> if self . host_override : <EOL> headers [ '<STR_LIT>' ] = self . host_override <EOL> tries = <NUM_LIT:0> <EOL> auth_tries = [ <NUM_LIT:0> ] <EOL> def NeedAuth ( ) : <EOL> """<STR_LIT>""" <EOL> auth_tries [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> if auth_tries [ <NUM_LIT:0> ] > self . auth_tries : <EOL> RaiseHttpError ( url , response_info , response , '<STR_LIT>' ) <EOL> while tries < self . rpc_tries : <EOL> tries += <NUM_LIT:1> <EOL> self . _Authenticate ( self . http , auth_tries [ <NUM_LIT:0> ] > <NUM_LIT:0> ) <EOL> logger . debug ( '<STR_LIT>' , <EOL> url , headers , <EOL> self . debug_data and payload or payload and '<STR_LIT>' or '<STR_LIT>' ) <EOL> try : <EOL> response_info , response = self . http . request ( url , method , body = payload , <EOL> headers = headers ) <EOL> except client . AccessTokenRefreshError , e : <EOL> logger . info ( '<STR_LIT>' , exc_info = <NUM_LIT:1> ) <EOL> response_info = httplib2 . Response ( { '<STR_LIT:status>' : <NUM_LIT> } ) <EOL> response_info . reason = str ( e ) <EOL> response = '<STR_LIT>' <EOL> status = response_info . status <EOL> if status == <NUM_LIT:200> : <EOL> return response <EOL> logger . debug ( '<STR_LIT>' , <EOL> response_info . status , tries ) <EOL> if status == <NUM_LIT> : <EOL> NeedAuth ( ) <EOL> continue <EOL> elif status >= <NUM_LIT> and status < <NUM_LIT> : <EOL> continue <EOL> elif status == <NUM_LIT> : <EOL> loc = response_info . get ( '<STR_LIT:location>' ) <EOL> logger . debug ( '<STR_LIT>' , loc ) <EOL> if ( loc . startswith ( '<STR_LIT>' ) or <EOL> re . match ( r'<STR_LIT>' , <EOL> loc ) ) : <EOL> NeedAuth ( ) <EOL> continue <EOL> elif loc . startswith ( '<STR_LIT>' % ( self . host , ) ) : <EOL> RaiseHttpError ( url , response_info , response , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> RaiseHttpError ( url , response_info , response , <EOL> '<STR_LIT>' % loc ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' , response_info ) <EOL> RaiseHttpError ( url , response_info , response , <EOL> '<STR_LIT>' % status ) <EOL> logging . info ( '<STR_LIT>' , url ) <EOL> RaiseHttpError ( url , response_info , response ) <EOL> class NoStorage ( client . Storage ) : <EOL> """<STR_LIT>""" <EOL> def locked_get ( self ) : <EOL> return None <EOL> def locked_put ( self , credentials ) : <EOL> pass <EOL> class HttpRpcServerOauth2 ( HttpRpcServerHttpLib2 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , refresh_token , user_agent , source , <EOL> host_override = None , extra_headers = None , save_cookies = False , <EOL> auth_tries = None , account_type = None , debug_data = True , secure = True , <EOL> ignore_certs = False , rpc_tries = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> super ( HttpRpcServerOauth2 , self ) . __init__ ( <EOL> host , None , user_agent , None , host_override = host_override , <EOL> extra_headers = extra_headers , auth_tries = auth_tries , <EOL> debug_data = debug_data , secure = secure , ignore_certs = ignore_certs , <EOL> rpc_tries = rpc_tries ) <EOL> if not isinstance ( source , tuple ) or len ( source ) not in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . client_id = source [ <NUM_LIT:0> ] <EOL> self . client_secret = source [ <NUM_LIT:1> ] <EOL> self . scope = source [ <NUM_LIT:2> ] <EOL> oauth2_credential_file = ( len ( source ) > <NUM_LIT:3> and source [ <NUM_LIT:3> ] <EOL> or '<STR_LIT>' ) <EOL> if save_cookies : <EOL> self . storage = oauth2client_file . Storage ( <EOL> os . path . expanduser ( oauth2_credential_file ) ) <EOL> else : <EOL> self . storage = NoStorage ( ) <EOL> self . refresh_token = refresh_token <EOL> if refresh_token : <EOL> self . credentials = client . OAuth2Credentials ( <EOL> None , <EOL> self . client_id , <EOL> self . client_secret , <EOL> refresh_token , <EOL> None , <EOL> ( '<STR_LIT>' % <EOL> os . getenv ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> self . user_agent ) <EOL> else : <EOL> self . credentials = self . storage . get ( ) <EOL> def _Authenticate ( self , http , needs_auth ) : <EOL> """<STR_LIT>""" <EOL> if needs_auth and ( not self . credentials or self . credentials . invalid ) : <EOL> if self . refresh_token : <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise AuthPermanentFail ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> flow = client . OAuth2WebServerFlow ( <EOL> client_id = self . client_id , <EOL> client_secret = self . client_secret , <EOL> scope = self . scope , <EOL> user_agent = self . user_agent ) <EOL> self . credentials = tools . run ( flow , self . storage ) <EOL> if self . credentials and not self . credentials . invalid : <EOL> if not self . credentials . access_token_expired or needs_auth : <EOL> logger . debug ( '<STR_LIT>' , <EOL> needs_auth ) <EOL> self . credentials . authorize ( http ) <EOL> return <EOL> logger . debug ( '<STR_LIT>' , needs_auth ) </s>
