Best practise when using httplib2.Http() object

Posted by tomaz on Stack Overflow See other posts from Stack Overflow or by tomaz
Published on 2009-08-08T13:53:34Z Indexed on 2010/04/13 9:43 UTC
Read the original article Hit count: 447

Filed under:
|

I'm writing a pythonic web API wrapper with a class like this

import httplib2
import urllib

class apiWrapper:

    def __init__(self):
    	self.http = httplib2.Http()

    def _http(self, url, method, dict):
    	'''
    	Im using this wrapper arround the http object
    	all the time inside the class
    	'''
    	params = urllib.urlencode(dict)
    	response, content = self.http.request(url,params,method)


as you can see I'm using the _http() method to simplify the interaction with the httplib2.Http() object. This method is called quite often inside the class and I'm wondering what's the best way to interact with this object:

  • create the object in the __init__ and then reuse it when the _http() method is called (as shown in the code above)
  • or create the httplib2.Http() object inside the method for every call of the _http() method (as shown in the code sample below)


import httplib2
import urllib


class apiWrapper:

    def __init__(self):

    def _http(self, url, method, dict):
    	'''Im using this wrapper arround the http object
    	all the time inside the class'''
    	http = httplib2.Http()
    	params = urllib.urlencode(dict)
    	response, content = http.request(url,params,method)

© Stack Overflow or respective owner

Related posts about python

Related posts about httplib2