mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 09:13:23 +00:00
Add use_cassette class so functinos that are decorated with use_cassette can be called multiple times.
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
'''The container for recorded requests and responses'''
|
||||
|
||||
import functools
|
||||
try:
|
||||
from collections import Counter
|
||||
except ImportError:
|
||||
from .compat.counter import Counter
|
||||
|
||||
from contextdecorator import ContextDecorator
|
||||
|
||||
# Internal imports
|
||||
from .patch import install, reset
|
||||
from .persist import load_cassette, save_cassette
|
||||
@@ -16,7 +14,29 @@ from .matchers import requests_match, uri, method
|
||||
from .errors import UnhandledHTTPRequestError
|
||||
|
||||
|
||||
class Cassette(ContextDecorator):
|
||||
class use_cassette(object):
|
||||
|
||||
def __init__(self, cls, *args, **kwargs):
|
||||
self.args = args
|
||||
self.kwargs = kwargs
|
||||
self.cls = cls
|
||||
|
||||
def __enter__(self):
|
||||
self._cassette = self.cls.load(*self.args, **self.kwargs)
|
||||
return self._cassette.__enter__()
|
||||
|
||||
def __exit__(self, *args):
|
||||
return self._cassette.__exit__(*args)
|
||||
|
||||
def __call__(self, function):
|
||||
@functools.wraps(function)
|
||||
def wrapped(*args, **kwargs):
|
||||
with self:
|
||||
return function(*args, **kwargs)
|
||||
return wrapped
|
||||
|
||||
|
||||
class Cassette(object):
|
||||
'''A container for recorded requests and responses'''
|
||||
|
||||
@classmethod
|
||||
@@ -26,6 +46,10 @@ class Cassette(ContextDecorator):
|
||||
new_cassette._load()
|
||||
return new_cassette
|
||||
|
||||
@classmethod
|
||||
def use_cassette(cls, *args, **kwargs):
|
||||
return use_cassette(cls, *args, **kwargs)
|
||||
|
||||
def __init__(self,
|
||||
path,
|
||||
serializer=yamlserializer,
|
||||
|
||||
Reference in New Issue
Block a user