<s> """<STR_LIT>""" <EOL> import logging <EOL> import time <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> import requests <EOL> from solumclient . openstack . common . apiclient import exceptions <EOL> from solumclient . openstack . common . gettextutils import _ <EOL> from solumclient . openstack . common import importutils <EOL> _logger = logging . getLogger ( __name__ ) <EOL> class HTTPClient ( object ) : <EOL> """<STR_LIT>""" <EOL> user_agent = "<STR_LIT>" <EOL> def __init__ ( self , <EOL> auth_plugin , <EOL> region_name = None , <EOL> endpoint_type = "<STR_LIT>" , <EOL> original_ip = None , <EOL> verify = True , <EOL> cert = None , <EOL> timeout = None , <EOL> timings = False , <EOL> keyring_saver = None , <EOL> debug = False , <EOL> user_agent = None , <EOL> http = None ) : <EOL> self . auth_plugin = auth_plugin <EOL> self . endpoint_type = endpoint_type <EOL> self . region_name = region_name <EOL> self . original_ip = original_ip <EOL> self . timeout = timeout <EOL> self . verify = verify <EOL> self . cert = cert <EOL> self . keyring_saver = keyring_saver <EOL> self . debug = debug <EOL> self . user_agent = user_agent or self . user_agent <EOL> self . times = [ ] <EOL> self . timings = timings <EOL> self . http = http or requests . Session ( ) <EOL> self . cached_token = None <EOL> def _http_log_req ( self , method , url , kwargs ) : <EOL> if not self . debug : <EOL> return <EOL> string_parts = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % method , <EOL> "<STR_LIT>" % url , <EOL> ] <EOL> for element in kwargs [ '<STR_LIT>' ] : <EOL> header = "<STR_LIT>" % ( element , kwargs [ '<STR_LIT>' ] [ element ] ) <EOL> string_parts . append ( header ) <EOL> _logger . debug ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( string_parts ) ) <EOL> if '<STR_LIT:data>' in kwargs : <EOL> _logger . debug ( "<STR_LIT>" % ( kwargs [ '<STR_LIT:data>' ] ) ) <EOL> def _http_log_resp ( self , resp ) : <EOL> if not self . debug : <EOL> return <EOL> _logger . debug ( <EOL> "<STR_LIT>" , <EOL> resp . status_code , <EOL> resp . headers ) <EOL> if resp . _content_consumed : <EOL> _logger . debug ( <EOL> "<STR_LIT>" , <EOL> resp . text ) <EOL> def serialize ( self , kwargs ) : <EOL> if kwargs . get ( '<STR_LIT>' ) is not None : <EOL> kwargs [ '<STR_LIT>' ] [ '<STR_LIT:Content-Type>' ] = '<STR_LIT:application/json>' <EOL> kwargs [ '<STR_LIT:data>' ] = json . dumps ( kwargs [ '<STR_LIT>' ] ) <EOL> try : <EOL> del kwargs [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> def get_timings ( self ) : <EOL> return self . times <EOL> def reset_timings ( self ) : <EOL> self . times = [ ] <EOL> def request ( self , method , url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs . setdefault ( "<STR_LIT>" , kwargs . get ( "<STR_LIT>" , { } ) ) <EOL> kwargs [ "<STR_LIT>" ] [ "<STR_LIT>" ] = self . user_agent <EOL> if self . original_ip : <EOL> kwargs [ "<STR_LIT>" ] [ "<STR_LIT>" ] = "<STR_LIT>" % ( <EOL> self . original_ip , self . user_agent ) <EOL> if self . timeout is not None : <EOL> kwargs . setdefault ( "<STR_LIT>" , self . timeout ) <EOL> kwargs . setdefault ( "<STR_LIT>" , self . verify ) <EOL> if self . cert is not None : <EOL> kwargs . setdefault ( "<STR_LIT>" , self . cert ) <EOL> self . serialize ( kwargs ) <EOL> self . _http_log_req ( method , url , kwargs ) <EOL> if self . timings : <EOL> start_time = time . time ( ) <EOL> resp = self . http . request ( method , url , ** kwargs ) <EOL> if self . timings : <EOL> self . times . append ( ( "<STR_LIT>" % ( method , url ) , <EOL> start_time , time . time ( ) ) ) <EOL> self . _http_log_resp ( resp ) <EOL> if resp . status_code >= <NUM_LIT> : <EOL> _logger . debug ( <EOL> "<STR_LIT>" , <EOL> resp . status_code ) <EOL> raise exceptions . from_response ( resp , method , url ) <EOL> return resp <EOL> @ staticmethod <EOL> def concat_url ( endpoint , url ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( endpoint . rstrip ( "<STR_LIT:/>" ) , url . strip ( "<STR_LIT:/>" ) ) <EOL> def client_request ( self , client , method , url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> filter_args = { <EOL> "<STR_LIT>" : client . endpoint_type or self . endpoint_type , <EOL> "<STR_LIT>" : client . service_type , <EOL> } <EOL> token , endpoint = ( self . cached_token , client . cached_endpoint ) <EOL> just_authenticated = False <EOL> if not ( token and endpoint ) : <EOL> try : <EOL> token , endpoint = self . auth_plugin . token_and_endpoint ( <EOL> ** filter_args ) <EOL> except exceptions . EndpointException : <EOL> pass <EOL> if not ( token and endpoint ) : <EOL> self . authenticate ( ) <EOL> just_authenticated = True <EOL> token , endpoint = self . auth_plugin . token_and_endpoint ( <EOL> ** filter_args ) <EOL> if not ( token and endpoint ) : <EOL> raise exceptions . AuthorizationFailure ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> old_token_endpoint = ( token , endpoint ) <EOL> kwargs . setdefault ( "<STR_LIT>" , { } ) [ "<STR_LIT>" ] = token <EOL> self . cached_token = token <EOL> client . cached_endpoint = endpoint <EOL> try : <EOL> return self . request ( <EOL> method , self . concat_url ( endpoint , url ) , ** kwargs ) <EOL> except exceptions . ConnectionRefused as refused_ex : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' % endpoint ) <EOL> raise refused_ex <EOL> except exceptions . EndpointException as endpoint_ex : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' % endpoint ) <EOL> raise endpoint_ex <EOL> except requests . ConnectionError as conn_err : <EOL> print ( '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' % endpoint ) <EOL> raise conn_err <EOL> except exceptions . Unauthorized as unauth_ex : <EOL> if just_authenticated : <EOL> raise <EOL> self . cached_token = None <EOL> client . cached_endpoint = None <EOL> self . authenticate ( ) <EOL> try : <EOL> token , endpoint = self . auth_plugin . token_and_endpoint ( <EOL> ** filter_args ) <EOL> except exceptions . EndpointException : <EOL> raise unauth_ex <EOL> if ( not ( token and endpoint ) or <EOL> old_token_endpoint == ( token , endpoint ) ) : <EOL> raise unauth_ex <EOL> self . cached_token = token <EOL> client . cached_endpoint = endpoint <EOL> kwargs [ "<STR_LIT>" ] [ "<STR_LIT>" ] = token <EOL> return self . request ( <EOL> method , self . concat_url ( endpoint , url ) , ** kwargs ) <EOL> def add_client ( self , base_client_instance ) : <EOL> """<STR_LIT>""" <EOL> service_type = base_client_instance . service_type <EOL> if service_type and not hasattr ( self , service_type ) : <EOL> setattr ( self , service_type , base_client_instance ) <EOL> def authenticate ( self ) : <EOL> self . auth_plugin . authenticate ( self ) <EOL> if self . keyring_saver : <EOL> self . keyring_saver . save ( self ) <EOL> class BaseClient ( object ) : <EOL> """<STR_LIT>""" <EOL> service_type = None <EOL> endpoint_type = None <EOL> cached_endpoint = None <EOL> def __init__ ( self , http_client , extensions = None ) : <EOL> self . http_client = http_client <EOL> http_client . add_client ( self ) <EOL> if extensions : <EOL> for extension in extensions : <EOL> if extension . manager_class : <EOL> setattr ( self , extension . name , <EOL> extension . manager_class ( self ) ) <EOL> def client_request ( self , method , url , ** kwargs ) : <EOL> return self . http_client . client_request ( <EOL> self , method , url , ** kwargs ) <EOL> def head ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT>" , url , ** kwargs ) <EOL> def get ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT:GET>" , url , ** kwargs ) <EOL> def post ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT:POST>" , url , ** kwargs ) <EOL> def put ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT>" , url , ** kwargs ) <EOL> def delete ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT>" , url , ** kwargs ) <EOL> def patch ( self , url , ** kwargs ) : <EOL> return self . client_request ( "<STR_LIT>" , url , ** kwargs ) <EOL> @ staticmethod <EOL> def get_class ( api_name , version , version_map ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> client_path = version_map [ str ( version ) ] <EOL> except ( KeyError , ValueError ) : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : api_name , <EOL> '<STR_LIT:version>' : version , <EOL> '<STR_LIT>' : '<STR_LIT:U+002CU+0020>' . join ( version_map . keys ( ) ) } <EOL> raise exceptions . UnsupportedVersion ( msg ) <EOL> return importutils . import_class ( client_path ) </s>
