1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-10 01:25:34 +00:00

PEP8 cleanup and make it actually work.

This commit is contained in:
Kevin McCarthy
2012-05-30 21:03:05 -10:00
parent 431e385ded
commit 4ad6be7787
2 changed files with 21 additions and 23 deletions

View File

@@ -1,16 +1,19 @@
from httplib import HTTPConnection
from cStringIO import StringIO
from .files import save_cassette, load_cassette
from .cassette import Cassette
class VCRHTTPResponse(object):
def __init__(self,recorded_response):
def __init__(self, recorded_response):
self.recorded_response = recorded_response
self.msg = recorded_response['status']['message']
self.reason = recorded_response['status']['message']
self.status = recorded_response['status']['code']
self._content = StringIO(self.recorded_response['body']['string'])
def read(self,chunked=False):
return self.recorded_response['body']['string']
def read(self, chunked=False):
return self._content.read()
def getheaders(self):
return self.recorded_response['headers']
@@ -18,34 +21,34 @@ class VCRHTTPResponse(object):
class VCRHTTPConnection(HTTPConnection):
def __init__(self,*args,**kwargs):
def __init__(self, *args, **kwargs):
self._cassette = Cassette()
HTTPConnection.__init__(self,*args,**kwargs)
HTTPConnection.__init__(self, *args, **kwargs)
def _save_cassette(self):
save_cassette(self._vcr_cassette_path,self._cassette)
save_cassette(self._vcr_cassette_path, self._cassette)
def request(self,method,url,body=None,headers={}):
def request(self, method, url, body=None, headers={}):
old_cassette = load_cassette(self._vcr_cassette_path)
if old_cassette:
return
self._cassette.requests.append(dict(
method = method,
url = url,
body = body,
headers = headers
method=method,
url=url,
body=body,
headers=headers
))
return HTTPConnection.request(self,method,url,body=body,headers=headers)
return HTTPConnection.request(self, method, url, body=body, headers=headers)
def getresponse(self,buffering=False):
def getresponse(self, buffering=False):
old_cassette = load_cassette(self._vcr_cassette_path)
if old_cassette:
return VCRHTTPResponse(old_cassette[0]['response'])
response = HTTPConnection.getresponse(self,buffering=buffering)
response = HTTPConnection.getresponse(self, buffering=buffering)
self._cassette.responses.append({
'status':{'code':response.status,'message':response.reason},
'headers':dict(response.getheaders()),
'body':{'string':response.read()},
'status': {'code': response.status, 'message': response.reason},
'headers': dict(response.getheaders()),
'body': {'string': response.read()},
})
self._save_cassette()
return response