<s> from __future__ import absolute_import , unicode_literals <EOL> from io import BytesIO <EOL> from vine import promise , transform <EOL> from kombu . async . http import Headers , Request , get_client <EOL> from kombu . five import items , python_2_unicode_compatible <EOL> from . ext import ( <EOL> boto , AWSAuthConnection , AWSQueryConnection , XmlHandler , ResultSet , <EOL> ) <EOL> try : <EOL> from urllib . parse import urlunsplit <EOL> except ImportError : <EOL> from urlparse import urlunsplit <EOL> from xml . sax import parseString as sax_parse <EOL> try : <EOL> from email import message_from_file <EOL> from email . mime . message import MIMEMessage <EOL> except ImportError : <EOL> from mimetools import Message as MIMEMessage <EOL> def message_from_file ( m ) : <EOL> return m <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> @ python_2_unicode_compatible <EOL> class AsyncHTTPResponse ( object ) : <EOL> def __init__ ( self , response ) : <EOL> self . response = response <EOL> self . _msg = None <EOL> self . version = <NUM_LIT:10> <EOL> def read ( self , * args , ** kwargs ) : <EOL> return self . response . body <EOL> def getheader ( self , name , default = None ) : <EOL> return self . response . headers . get ( name , default ) <EOL> def getheaders ( self ) : <EOL> return list ( items ( self . response . headers ) ) <EOL> @ property <EOL> def msg ( self ) : <EOL> if self . _msg is None : <EOL> self . _msg = MIMEMessage ( message_from_file ( <EOL> BytesIO ( b'<STR_LIT:\r\n>' . join ( <EOL> b'<STR_LIT>' . format ( * h ) for h in self . getheaders ( ) ) <EOL> ) <EOL> ) ) <EOL> return self . _msg <EOL> @ property <EOL> def status ( self ) : <EOL> return self . response . code <EOL> @ property <EOL> def reason ( self ) : <EOL> if self . response . error : <EOL> return self . response . error . message <EOL> return '<STR_LIT>' <EOL> def __repr__ ( self ) : <EOL> return repr ( self . response ) <EOL> @ python_2_unicode_compatible <EOL> class AsyncHTTPConnection ( object ) : <EOL> Request = Request <EOL> Response = AsyncHTTPResponse <EOL> method = '<STR_LIT:GET>' <EOL> path = '<STR_LIT:/>' <EOL> body = None <EOL> scheme = '<STR_LIT:http>' <EOL> default_ports = { '<STR_LIT:http>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } <EOL> def __init__ ( self , host , port = None , <EOL> strict = None , timeout = <NUM_LIT> , http_client = None , ** kwargs ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . headers = [ ] <EOL> self . timeout = timeout <EOL> self . strict = strict <EOL> self . http_client = http_client or get_client ( ) <EOL> def request ( self , method , path , body = None , headers = None ) : <EOL> self . path = path <EOL> self . method = method <EOL> if body is not None : <EOL> try : <EOL> read = body . read <EOL> except AttributeError : <EOL> self . body = body <EOL> else : <EOL> self . body = read ( ) <EOL> if headers is not None : <EOL> self . headers . extend ( list ( items ( headers ) ) ) <EOL> def getrequest ( self , scheme = None ) : <EOL> scheme = scheme if scheme else self . scheme <EOL> host = self . host <EOL> if self . port and self . port != self . default_ports [ scheme ] : <EOL> host = '<STR_LIT>' . format ( host , self . port ) <EOL> url = urlunsplit ( ( scheme , host , self . path , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> headers = Headers ( self . headers ) <EOL> return self . Request ( url , method = self . method , headers = headers , <EOL> body = self . body , connect_timeout = self . timeout , <EOL> request_timeout = self . timeout , validate_cert = False ) <EOL> def getresponse ( self , callback = None ) : <EOL> request = self . getrequest ( ) <EOL> request . then ( transform ( self . Response , callback ) ) <EOL> return self . http_client . add_request ( request ) <EOL> def set_debuglevel ( self , level ) : <EOL> pass <EOL> def connect ( self ) : <EOL> pass <EOL> def close ( self ) : <EOL> pass <EOL> def putrequest ( self , method , path , ** kwargs ) : <EOL> self . method = method <EOL> self . path = path <EOL> def putheader ( self , header , value ) : <EOL> self . headers . append ( ( header , value ) ) <EOL> def endheaders ( self ) : <EOL> pass <EOL> def send ( self , data ) : <EOL> if self . body : <EOL> self . body += data <EOL> else : <EOL> self . body = data <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . getrequest ( ) ) <EOL> class AsyncHTTPSConnection ( AsyncHTTPConnection ) : <EOL> scheme = '<STR_LIT>' <EOL> class AsyncConnection ( object ) : <EOL> def __init__ ( self , http_client = None , ** kwargs ) : <EOL> if boto is None : <EOL> raise ImportError ( '<STR_LIT>' ) <EOL> self . _httpclient = http_client or get_client ( ) <EOL> def get_http_connection ( self , host , port , is_secure ) : <EOL> return ( AsyncHTTPSConnection if is_secure else AsyncHTTPConnection ) ( <EOL> host , port , http_client = self . _httpclient , <EOL> ) <EOL> def _mexe ( self , request , sender = None , callback = None ) : <EOL> callback = callback or promise ( ) <EOL> boto . log . debug ( <EOL> '<STR_LIT>' , <EOL> request . host , request . path , <EOL> request . headers , request . body , <EOL> ) <EOL> conn = self . get_http_connection ( <EOL> request . host , request . port , self . is_secure , <EOL> ) <EOL> request . authorize ( connection = self ) <EOL> if callable ( sender ) : <EOL> sender ( conn , request . method , request . path , request . body , <EOL> request . headers , callback ) <EOL> else : <EOL> conn . request ( request . method , request . path , <EOL> request . body , request . headers ) <EOL> conn . getresponse ( callback = callback ) <EOL> return callback <EOL> class AsyncAWSAuthConnection ( AsyncConnection , AWSAuthConnection ) : <EOL> def __init__ ( self , host , <EOL> http_client = None , http_client_params = { } , ** kwargs ) : <EOL> AsyncConnection . __init__ ( self , http_client , ** http_client_params ) <EOL> AWSAuthConnection . __init__ ( self , host , ** kwargs ) <EOL> def make_request ( self , method , path , headers = None , data = '<STR_LIT>' , host = None , <EOL> auth_path = None , sender = None , callback = None , ** kwargs ) : <EOL> req = self . build_base_http_request ( <EOL> method , path , auth_path , { } , headers , data , host , <EOL> ) <EOL> return self . _mexe ( req , sender = sender , callback = callback ) <EOL> class AsyncAWSQueryConnection ( AsyncConnection , AWSQueryConnection ) : <EOL> def __init__ ( self , host , <EOL> http_client = None , http_client_params = { } , ** kwargs ) : <EOL> AsyncConnection . __init__ ( self , http_client , ** http_client_params ) <EOL> AWSAuthConnection . __init__ ( self , host , ** kwargs ) <EOL> def make_request ( self , action , params , path , verb , callback = None ) : <EOL> request = self . build_base_http_request ( <EOL> verb , path , None , params , { } , '<STR_LIT>' , self . server_name ( ) ) <EOL> if action : <EOL> request . params [ '<STR_LIT>' ] = action <EOL> request . params [ '<STR_LIT>' ] = self . APIVersion <EOL> return self . _mexe ( request , callback = callback ) <EOL> def get_list ( self , action , params , markers , <EOL> path = '<STR_LIT:/>' , parent = None , verb = '<STR_LIT:GET>' , callback = None ) : <EOL> return self . make_request ( <EOL> action , params , path , verb , <EOL> callback = transform ( <EOL> self . _on_list_ready , callback , parent or self , markers , <EOL> ) , <EOL> ) <EOL> def get_object ( self , action , params , cls , <EOL> path = '<STR_LIT:/>' , parent = None , verb = '<STR_LIT:GET>' , callback = None ) : <EOL> return self . make_request ( <EOL> action , params , path , verb , <EOL> callback = transform ( <EOL> self . _on_obj_ready , callback , parent or self , cls , <EOL> ) , <EOL> ) <EOL> def get_status ( self , action , params , <EOL> path = '<STR_LIT:/>' , parent = None , verb = '<STR_LIT:GET>' , callback = None ) : <EOL> return self . make_request ( <EOL> action , params , path , verb , <EOL> callback = transform ( <EOL> self . _on_status_ready , callback , parent or self , <EOL> ) , <EOL> ) <EOL> def _on_list_ready ( self , parent , markers , response ) : <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> and body : <EOL> rs = ResultSet ( markers ) <EOL> h = XmlHandler ( rs , parent ) <EOL> sax_parse ( body , h ) <EOL> return rs <EOL> else : <EOL> raise self . _for_status ( response , body ) <EOL> def _on_obj_ready ( self , parent , cls , response ) : <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> and body : <EOL> obj = cls ( parent ) <EOL> h = XmlHandler ( obj , parent ) <EOL> sax_parse ( body , h ) <EOL> return obj <EOL> else : <EOL> raise self . _for_status ( response , body ) <EOL> def _on_status_ready ( self , parent , response ) : <EOL> body = response . read ( ) <EOL> if response . status == <NUM_LIT:200> and body : <EOL> rs = ResultSet ( ) <EOL> h = XmlHandler ( rs , parent ) <EOL> sax_parse ( body , h ) <EOL> return rs . status <EOL> else : <EOL> raise self . _for_status ( response , body ) <EOL> def _for_status ( self , response , body ) : <EOL> context = '<STR_LIT>' if not body else '<STR_LIT>' <EOL> exc = self . ResponseError ( response . status , response . reason , body ) <EOL> boto . log . error ( '<STR_LIT>' . format ( context ) , exc ) <EOL> return exc </s>
