<s> """<STR_LIT>""" <EOL> import urllib <EOL> from object_storage import errors <EOL> from object_storage . transport import BaseAuthentication , BaseAuthenticatedConnection , Response <EOL> import httplib2 <EOL> from object_storage . utils import json <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class AuthenticatedConnection ( BaseAuthenticatedConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , auth , debug = False , ** kwargs ) : <EOL> if debug : <EOL> httplib2 . debuglevel = <NUM_LIT:4> <EOL> self . token = None <EOL> self . storage_url = None <EOL> self . http = httplib2 . Http ( ) <EOL> self . http . disable_ssl_certificate_validation = True <EOL> self . auth = auth <EOL> if not self . auth . authenticated : <EOL> self . auth . authenticate ( ) <EOL> self . _authenticate ( ) <EOL> def make_request ( self , method , url = None , headers = None , formatter = None , <EOL> params = None , data = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = headers or { } <EOL> headers . update ( self . get_headers ( ) ) <EOL> if params : <EOL> url = "<STR_LIT>" % ( url , urllib . urlencode ( params ) ) <EOL> def _make_request ( headers ) : <EOL> logger . debug ( "<STR_LIT>" % ( method , url , headers ) ) <EOL> res , content = self . http . request ( url , method , <EOL> headers = headers , <EOL> body = data ) <EOL> response = Response ( ) <EOL> response . headers = res <EOL> response . status_code = int ( res . status ) <EOL> response . content = content <EOL> return response <EOL> response = _make_request ( headers ) <EOL> if response . status_code == <NUM_LIT> : <EOL> self . auth . authenticate ( ) <EOL> self . _authenticate ( ) <EOL> headers . update ( self . auth_headers ) <EOL> response = _make_request ( headers ) <EOL> response . raise_for_status ( ) <EOL> if formatter : <EOL> return formatter ( response ) <EOL> return response <EOL> class Authentication ( BaseAuthentication ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , username , api_key , auth_token = None , * args , ** kwargs ) : <EOL> super ( Authentication , self ) . __init__ ( * args , ** kwargs ) <EOL> self . username = username <EOL> self . api_key = api_key <EOL> self . auth_token = auth_token <EOL> if self . auth_token : <EOL> self . authenticated = True <EOL> @ property <EOL> def auth_headers ( self ) : <EOL> return { '<STR_LIT>' : self . auth_token } <EOL> def authenticate ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = { '<STR_LIT>' : self . username , <EOL> '<STR_LIT>' : self . api_key , <EOL> '<STR_LIT>' : '<STR_LIT:0>' } <EOL> http = httplib2 . Http ( ) <EOL> http . disable_ssl_certificate_validation = True <EOL> res , content = http . request ( self . auth_url , '<STR_LIT:GET>' , headers = headers ) <EOL> response = Response ( ) <EOL> response . headers = res <EOL> response . status_code = int ( res . status ) <EOL> response . content = content <EOL> if response . status_code == <NUM_LIT> : <EOL> raise errors . AuthenticationError ( '<STR_LIT>' ) <EOL> response . raise_for_status ( ) <EOL> try : <EOL> storage_options = json . loads ( response . content ) [ '<STR_LIT>' ] <EOL> except ValueError : <EOL> raise errors . StorageURLNotFound ( "<STR_LIT>" ) <EOL> self . auth_token = response . headers [ '<STR_LIT>' ] <EOL> self . storage_url = self . get_storage_url ( storage_options ) <EOL> if not self . storage_url : <EOL> self . storage_url = response . headers [ '<STR_LIT>' ] <EOL> if not self . auth_token or not self . storage_url : <EOL> raise errors . AuthenticationError ( '<STR_LIT>' ) </s>
