Ich bin mit einer Klasse wie dieseBest Practice bei der Verwendung von httplib2.Http() Objekt
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)
einen pythonic Web-API-Wrapper zu schreiben, wie Sie mir bin mit der _http()
Methode sehen zu vereinfachen die Interaktion mit dem Objekt httplib2.Http()
. Dieses Verfahren wird sehr oft in der Klasse genannt und ich frage mich, was der beste Weg ist, mit diesem Objekt zu interagieren:
- das Objekt in dem
__init__
erstellen und dann Wiederverwendung es, wenn die_http()
Methode aufgerufen wird (wie gezeigt in dem obigen Code) - oder schaffen die
httplib2.Http()
Objekts innerhalb des Verfahrens für jeden Aufruf der Methode_http()
(wie in dem Codebeispiel unten gezeigt)
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)