home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / atom / http_interface.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.6 KB  |  152 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """This module provides a common interface for all HTTP requests.
  5.  
  6.   HttpResponse: Represents the server's response to an HTTP request. Provides
  7.       an interface identical to httplib.HTTPResponse which is the response
  8.       expected from higher level classes which use HttpClient.request.
  9.  
  10.   GenericHttpClient: Provides an interface (superclass) for an object 
  11.       responsible for making HTTP requests. Subclasses of this object are
  12.       used in AtomService and GDataService to make requests to the server. By
  13.       changing the http_client member object, the AtomService is able to make
  14.       HTTP requests using different logic (for example, when running on 
  15.       Google App Engine, the http_client makes requests using the App Engine
  16.       urlfetch API). 
  17. """
  18. __author__ = 'api.jscudder (Jeff Scudder)'
  19. import StringIO
  20. USER_AGENT = '%s GData-Python/1.2.2'
  21.  
  22. class Error(Exception):
  23.     pass
  24.  
  25.  
  26. class UnparsableUrlObject(Error):
  27.     pass
  28.  
  29.  
  30. class ContentLengthRequired(Error):
  31.     pass
  32.  
  33.  
  34. class HttpResponse(object):
  35.     
  36.     def __init__(self, body = None, status = None, reason = None, headers = None):
  37.         """Constructor for an HttpResponse object. 
  38.  
  39.     HttpResponse represents the server's response to an HTTP request from
  40.     the client. The HttpClient.request method returns a httplib.HTTPResponse
  41.     object and this HttpResponse class is designed to mirror the interface
  42.     exposed by httplib.HTTPResponse.
  43.  
  44.     Args:
  45.       body: A file like object, with a read() method. The body could also
  46.           be a string, and the constructor will wrap it so that 
  47.           HttpResponse.read(self) will return the full string.
  48.       status: The HTTP status code as an int. Example: 200, 201, 404.
  49.       reason: The HTTP status message which follows the code. Example: 
  50.           OK, Created, Not Found
  51.       headers: A dictionary containing the HTTP headers in the server's 
  52.           response. A common header in the response is Content-Length.
  53.     """
  54.         if body:
  55.             if hasattr(body, 'read'):
  56.                 self._body = body
  57.             else:
  58.                 self._body = StringIO.StringIO(body)
  59.         else:
  60.             self._body = None
  61.         if status is not None:
  62.             self.status = int(status)
  63.         else:
  64.             self.status = None
  65.         self.reason = reason
  66.         if not headers:
  67.             pass
  68.         self._headers = { }
  69.  
  70.     
  71.     def getheader(self, name, default = None):
  72.         if name in self._headers:
  73.             return self._headers[name]
  74.         return default
  75.  
  76.     
  77.     def read(self, amt = None):
  78.         if not amt:
  79.             return self._body.read()
  80.         return self._body.read(amt)
  81.  
  82.  
  83.  
  84. class GenericHttpClient(object):
  85.     debug = False
  86.     
  87.     def __init__(self, http_client, headers = None):
  88.         """
  89.     
  90.     Args:
  91.       http_client: An object which provides a request method to make an HTTP 
  92.           request. The request method in GenericHttpClient performs a 
  93.           call-through to the contained HTTP client object.
  94.       headers: A dictionary containing HTTP headers which should be included
  95.           in every HTTP request. Common persistent headers include 
  96.           'User-Agent'.
  97.     """
  98.         self.http_client = http_client
  99.         if not headers:
  100.             pass
  101.         self.headers = { }
  102.  
  103.     
  104.     def request(self, operation, url, data = None, headers = None):
  105.         all_headers = self.headers.copy()
  106.         if headers:
  107.             all_headers.update(headers)
  108.         
  109.         return self.http_client.request(operation, url, data = data, headers = all_headers)
  110.  
  111.     
  112.     def get(self, url, headers = None):
  113.         return self.request('GET', url, headers = headers)
  114.  
  115.     
  116.     def post(self, url, data, headers = None):
  117.         return self.request('POST', url, data = data, headers = headers)
  118.  
  119.     
  120.     def put(self, url, data, headers = None):
  121.         return self.request('PUT', url, data = data, headers = headers)
  122.  
  123.     
  124.     def delete(self, url, headers = None):
  125.         return self.request('DELETE', url, headers = headers)
  126.  
  127.  
  128.  
  129. class GenericToken(object):
  130.     '''Represents an Authorization token to be added to HTTP requests.
  131.   
  132.   Some Authorization headers included calculated fields (digital
  133.   signatures for example) which are based on the parameters of the HTTP
  134.   request. Therefore the token is responsible for signing the request
  135.   and adding the Authorization header. 
  136.   '''
  137.     
  138.     def perform_request(self, http_client, operation, url, data = None, headers = None):
  139.         '''For the GenericToken, no Authorization token is set.'''
  140.         return http_client.request(operation, url, data = data, headers = headers)
  141.  
  142.     
  143.     def valid_for_scope(self, url):
  144.         """Tells the caller if the token authorizes access to the desired URL.
  145.     
  146.     Since the generic token doesn't add an auth header, it is not valid for
  147.     any scope.
  148.     """
  149.         return False
  150.  
  151.  
  152.