BigMacchia / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

Patch: enhance Response for compatibility with CookieJar #174

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
At the current time, it is not possible to use httplib2 (without changes) when 
you need to maintains cookie:
- Response class can not keep multiple 'Set-Cookie' headers
- http.cookiejar.CookieJar() can not directly extract cookies from Response

The attached patch solves this:
- Response maintains a secondary directory that keeps all values associated to 
a header name
- Adds a get_all() method to Response for interoperability with CookieJar()
- Provides a fake info method for interoperability with CookieJar.

Below is an example showing how the integration works (connection is an 
httplib2 connection):

class _HttpRequestWithMethod(urllib.request.Request):
    """A subclass of urllib.request.Request that allow specifying the HTTP method."""
    def __init__( self, method, *args, **kwargs ):
        self.method = method
        super(_HttpRequestWithMethod, self).__init__( *args, **kwargs )

    def get_method( self ):
        return self.method

cookie_jar = http.cookiejar.CookieJar()

# integration with CookieJar by creating a request object and passing the fake 
Response
request = _HttpRequestWithMethod( method, url, 
    data=body, headers=request_headers )
cookie_jar.add_cookie_header(request)
response, content = connection.request( 
    request.full_url, 
    method=request.get_method(),
    body=request.get_data(), 
    headers=dict( request.header_items() ) )
cookie_jar.extract_cookies( response, request )

Original issue reported on code.google.com by baptiste...@gmail.com on 11 Sep 2011 at 12:28

Attachments: