<s> """<STR_LIT>""" <EOL> from zope import interface <EOL> from object_storage . transport import requote_path <EOL> from object_storage . errors import NotFound <EOL> from object_storage . transport import Response , BaseAuthenticatedConnection , BaseAuthentication <EOL> from object_storage import errors <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import Deferred <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . internet . ssl import ClientContextFactory <EOL> from twisted . web . client import Agent <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . web . iweb import IBodyProducer , UNKNOWN_LENGTH <EOL> import urlparse <EOL> import urllib <EOL> from object_storage . utils import json <EOL> def complete_request ( resp , callback = None , load_body = True ) : <EOL> r = Response ( ) <EOL> r . status_code = resp . code <EOL> r . version = resp . version <EOL> r . phrase = resp . phrase <EOL> for k , v in resp . headers . getAllRawHeaders ( ) : <EOL> r . headers [ k . lower ( ) ] = v . pop ( ) <EOL> if r . status_code == <NUM_LIT> : <EOL> raise NotFound ( '<STR_LIT>' ) <EOL> r . raise_for_status ( ) <EOL> if not load_body : <EOL> if callback : <EOL> return callback ( r ) <EOL> return r <EOL> def build_response ( body ) : <EOL> r . content = body <EOL> if callback : <EOL> return callback ( r ) <EOL> return r <EOL> finished = Deferred ( ) <EOL> resp . deliverBody ( FullBodyReader ( finished ) ) <EOL> finished . addCallback ( build_response ) <EOL> return finished <EOL> def print_error ( failure ) : <EOL> from twisted . web import _newclient <EOL> if failure . check ( _newclient . RequestGenerationFailed ) : <EOL> for f in failure . value . reasons : <EOL> print f . getTraceback ( ) <EOL> return failure <EOL> class AuthenticatedConnection ( BaseAuthenticatedConnection ) : <EOL> def __init__ ( self , auth , ** kwargs ) : <EOL> self . token = None <EOL> self . storage_url = None <EOL> self . auth = auth <EOL> def authenticate ( self ) : <EOL> d = self . auth . authenticate ( ) <EOL> d . addCallback ( lambda r : self . _authenticate ( ) ) <EOL> return d <EOL> def make_request ( self , method , url = None , headers = None , * args , ** kwargs ) : <EOL> headers = headers or { } <EOL> headers . update ( self . get_headers ( ) ) <EOL> return make_request ( method , url = url , headers = headers , * args , ** kwargs ) <EOL> def make_request ( method , url = None , headers = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> headers = Headers ( dict ( [ ( k , [ v ] ) for k , v in headers . items ( ) ] ) ) <EOL> formatter = None <EOL> if '<STR_LIT>' in kwargs : <EOL> formatter = kwargs . get ( '<STR_LIT>' ) <EOL> del kwargs [ '<STR_LIT>' ] <EOL> if not formatter : <EOL> def _nothing ( result ) : <EOL> return result <EOL> formatter = _nothing <EOL> params = kwargs . get ( '<STR_LIT>' , None ) <EOL> if params : <EOL> params = urllib . urlencode ( params ) <EOL> url = _full_url ( url , params ) <EOL> body = kwargs . get ( '<STR_LIT:data>' ) <EOL> contextFactory = WebClientContextFactory ( ) <EOL> agent = Agent ( reactor , contextFactory ) <EOL> d = agent . request ( <EOL> method , <EOL> url , <EOL> headers , <EOL> body ) <EOL> load_body = True <EOL> if method . upper ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> load_body = False <EOL> d . addCallback ( complete_request , formatter , load_body = load_body ) <EOL> d . addErrback ( print_error ) <EOL> return d <EOL> def _full_url ( url , _params = { } ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , path , params , query , fragment = urlparse . urlparse ( url ) <EOL> if not scheme : <EOL> raise ValueError ( "<STR_LIT>" % url ) <EOL> netloc = netloc . encode ( '<STR_LIT>' ) <EOL> if isinstance ( path , unicode ) : <EOL> path = path . encode ( '<STR_LIT:utf-8>' ) <EOL> path = requote_path ( path ) <EOL> url = str ( urlparse . urlunparse ( [ scheme , netloc , path , params , query , <EOL> fragment ] ) ) <EOL> if _params : <EOL> if urlparse . urlparse ( url ) . query : <EOL> return '<STR_LIT>' % ( url , _params ) <EOL> else : <EOL> return '<STR_LIT>' % ( url , _params ) <EOL> else : <EOL> return url <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 , response ) : <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 . get ( '<STR_LIT>' , '<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> raise errors . StorageURLNotFound ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not self . auth_token or not self . storage_url : <EOL> raise errors . AuthenticationError ( '<STR_LIT>' ) <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> d = make_request ( '<STR_LIT:GET>' , self . auth_url , headers = headers ) <EOL> d . addCallback ( self . _authenticate ) <EOL> return d <EOL> class WebClientContextFactory ( ClientContextFactory ) : <EOL> def getContext ( self , hostname , port ) : <EOL> return ClientContextFactory . getContext ( self ) <EOL> class FullBodyReader ( Protocol ) : <EOL> def __init__ ( self , finished ) : <EOL> self . finished = finished <EOL> self . body = '<STR_LIT>' <EOL> def dataReceived ( self , data ) : <EOL> self . body += data <EOL> def connectionLost ( self , reason ) : <EOL> self . finished . callback ( self . body ) <EOL> class ChunkedConnection : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conn , url , headers = None , size = None ) : <EOL> self . conn = conn <EOL> self . url = url <EOL> self . req = None <EOL> self . headers = headers <EOL> self . started = Deferred ( ) <EOL> self . size = size <EOL> self . body = ChunkedStreamProducer ( self . started , self . size ) <EOL> def setup ( self , size = None ) : <EOL> """<STR_LIT>""" <EOL> if size : <EOL> self . size = size <EOL> if not self . size : <EOL> self . size = UNKNOWN_LENGTH <EOL> self . body . length = self . size <EOL> req = self . conn . make_request ( '<STR_LIT>' , self . url , <EOL> headers = self . headers , <EOL> data = self . body ) <EOL> self . req = req <EOL> print "<STR_LIT>" <EOL> def send_chunk ( self , chunk ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> return self . body . send ( chunk ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> self . body . finish ( ) <EOL> class ChunkedStreamProducer ( object ) : <EOL> interface . implements ( IBodyProducer ) <EOL> def __init__ ( self , started , length = UNKNOWN_LENGTH ) : <EOL> self . length = length <EOL> self . consumer = None <EOL> self . started = Deferred ( ) <EOL> self . finished = Deferred ( ) <EOL> def startProducing ( self , consumer ) : <EOL> print "<STR_LIT>" <EOL> self . consumer = consumer <EOL> self . started . callback ( None ) <EOL> return self . finished <EOL> def _send ( self , result , data ) : <EOL> print "<STR_LIT>" <EOL> return self . consumer . write ( data ) <EOL> def send ( self , data ) : <EOL> print "<STR_LIT>" <EOL> d = Deferred ( ) <EOL> self . started . chainDeferred ( d ) <EOL> d . addCallback ( self . _send , data ) <EOL> return d <EOL> def finish ( self ) : <EOL> def _finish ( result ) : <EOL> self . finished . callback ( None ) <EOL> return None <EOL> d = Deferred ( ) <EOL> self . started . chainDeferred ( d ) <EOL> d . addCallback ( _finish ) <EOL> return d <EOL> def pauseProducing ( self ) : <EOL> print "<STR_LIT>" <EOL> pass <EOL> def stopProducing ( self ) : <EOL> print "<STR_LIT>" <EOL> pass </s>
