def record_requests(request):
# vcr interceptor
global REQUESTS
global REQUESTID
if type(request) == dict:
thistype = 'response'
else:
REQUESTID += 1
thistype = 'request'
if thistype == 'request':
ydata = {}
ydata['request'] = {}
ydata['request']['method'] = request.method
ydata['request']['uri'] = request.uri
if request.headers:
ydata['request']['headers'] = {}
for x in request.headers.items():
ydata['request']['headers'][x[0]] = x[1]
else:
ydata['request']['headers'] = request.headers
ydata['request']['body'] = literal(pretty_xml(request.body))
REQUESTS.append(ydata)
else:
ydata = REQUESTS[-1]
ydata['response'] = request.copy()
REQUESTS[-1]['response'] = request.copy()
REQUESTS[-1]['response']['body'] = literal(pretty_xml(request['body']['string']))
fid = str(REQUESTID).rjust(4, '0')
fname = os.path.join('/tmp', 'fixtures', '%s.yml' % (fid))
with open(fname, 'wb') as f:
yaml.dump(REQUESTS[-1], f, width=1000, indent=2)
return request
# Make a custom VCR instance so we can inject our spyware into it
评论列表
文章目录