<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import StringIO <EOL> import atom . service <EOL> import atom . http_interface <EOL> from google . appengine . api import urlfetch <EOL> def run_on_appengine ( gdata_service ) : <EOL> """<STR_LIT>""" <EOL> gdata_service . http_client = AppEngineHttpClient ( ) <EOL> class AppEngineHttpClient ( atom . http_interface . GenericHttpClient ) : <EOL> def __init__ ( self , headers = None ) : <EOL> self . debug = False <EOL> self . headers = headers or { } <EOL> def request ( self , operation , url , data = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> all_headers = self . headers . copy ( ) <EOL> if headers : <EOL> all_headers . update ( headers ) <EOL> data_str = data <EOL> if data : <EOL> if isinstance ( data , list ) : <EOL> converted_parts = [ __ConvertDataPart ( x ) for x in data ] <EOL> data_str = '<STR_LIT>' . join ( converted_parts ) <EOL> else : <EOL> data_str = __ConvertDataPart ( data ) <EOL> if data and '<STR_LIT>' not in all_headers : <EOL> all_headers [ '<STR_LIT>' ] = len ( data_str ) <EOL> if '<STR_LIT:Content-Type>' not in all_headers : <EOL> all_headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> if operation == '<STR_LIT:GET>' : <EOL> method = urlfetch . GET <EOL> elif operation == '<STR_LIT:POST>' : <EOL> method = urlfetch . POST <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . PUT <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . DELETE <EOL> else : <EOL> method = None <EOL> return HttpResponse ( urlfetch . Fetch ( url = str ( url ) , payload = data_str , <EOL> method = method , headers = all_headers ) ) <EOL> def HttpRequest ( service , operation , data , uri , extra_headers = None , <EOL> url_params = None , escape_params = True , content_type = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> full_uri = atom . service . BuildUri ( uri , url_params , escape_params ) <EOL> ( server , port , ssl , partial_uri ) = atom . service . ProcessUrl ( service , full_uri ) <EOL> if ssl : <EOL> full_url = '<STR_LIT>' % ( server , partial_uri ) <EOL> else : <EOL> full_url = '<STR_LIT>' % ( server , partial_uri ) <EOL> data_str = data <EOL> if data : <EOL> if isinstance ( data , list ) : <EOL> converted_parts = [ __ConvertDataPart ( x ) for x in data ] <EOL> data_str = '<STR_LIT>' . join ( converted_parts ) <EOL> else : <EOL> data_str = __ConvertDataPart ( data ) <EOL> headers = { } <EOL> if isinstance ( service . additional_headers , dict ) : <EOL> headers = service . additional_headers . copy ( ) <EOL> if isinstance ( extra_headers , dict ) : <EOL> for header , value in extra_headers . iteritems ( ) : <EOL> headers [ header ] = value <EOL> if content_type : <EOL> headers [ '<STR_LIT:Content-Type>' ] = content_type <EOL> if operation == '<STR_LIT:GET>' : <EOL> method = urlfetch . GET <EOL> elif operation == '<STR_LIT:POST>' : <EOL> method = urlfetch . POST <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . PUT <EOL> elif operation == '<STR_LIT>' : <EOL> method = urlfetch . DELETE <EOL> else : <EOL> method = None <EOL> return HttpResponse ( urlfetch . Fetch ( url = full_url , payload = data_str , <EOL> method = method , headers = headers ) ) <EOL> def __ConvertDataPart ( data ) : <EOL> if not data or isinstance ( data , str ) : <EOL> return data <EOL> elif hasattr ( data , '<STR_LIT>' ) : <EOL> return data . read ( ) <EOL> return str ( data ) <EOL> class HttpResponse ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , urlfetch_response ) : <EOL> self . body = StringIO . StringIO ( urlfetch_response . content ) <EOL> self . headers = urlfetch_response . headers <EOL> self . status = urlfetch_response . status_code <EOL> self . reason = '<STR_LIT>' <EOL> def read ( self , length = None ) : <EOL> if not length : <EOL> return self . body . read ( ) <EOL> else : <EOL> return self . body . read ( length ) <EOL> def getheader ( self , name ) : <EOL> if not self . headers . has_key ( name ) : <EOL> return self . headers [ name . lower ( ) ] <EOL> return self . headers [ name ] </s>
