mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 09:13:23 +00:00
Let's do an instance/module-based API for serializers
This commit is contained in:
10
README.md
10
README.md
@@ -112,10 +112,10 @@ The Request object has the following properties
|
||||
## Register your own serializer
|
||||
|
||||
Don't like JSON or YAML? That's OK, VCR.py can serialize to any format
|
||||
you would like. Create your own class that has 2 classmethods:
|
||||
you would like. Create your own module or class instance with 2 methods:
|
||||
|
||||
* `def load(cls, cassette_path)`
|
||||
* `def dumps(cls, requests, responses)`
|
||||
* `def load(cassette_path)`
|
||||
* `def dumps(requests, responses)`
|
||||
|
||||
Finally, register your class with VCR to use your
|
||||
new serializer.
|
||||
@@ -125,12 +125,12 @@ import vcr
|
||||
|
||||
BogoSerializer(object):
|
||||
"""
|
||||
Must implement load() and dumps() classmethods
|
||||
Must implement load() and dumps() methods
|
||||
"""
|
||||
pass
|
||||
|
||||
my_vcr = VCR()
|
||||
my_vcr.register_serializer('bogo', BogoSerializer)
|
||||
my_vcr.register_serializer('bogo', BogoSerializer())
|
||||
|
||||
with my_vcr.use_cassette('test.bogo', serializer='bogo'):
|
||||
# your http here
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
'''The container for recorded requests and responses'''
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
try:
|
||||
from collections import Counter, OrderedDict
|
||||
except ImportError:
|
||||
@@ -11,7 +9,7 @@ except ImportError:
|
||||
# Internal imports
|
||||
from .patch import install, reset
|
||||
from .persist import load_cassette, save_cassette
|
||||
from .serializers.yamlserializer import YamlSerializer
|
||||
from .serializers import yamlserializer
|
||||
|
||||
class Cassette(object):
|
||||
'''A container for recorded requests and responses'''
|
||||
@@ -22,7 +20,7 @@ class Cassette(object):
|
||||
new_cassette._load()
|
||||
return new_cassette
|
||||
|
||||
def __init__(self, path, serializer=YamlSerializer):
|
||||
def __init__(self, path, serializer=yamlserializer):
|
||||
self._path = path
|
||||
self._serializer = serializer
|
||||
self.data = OrderedDict()
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import os
|
||||
from .cassette import Cassette
|
||||
from .serializers.yamlserializer import YamlSerializer
|
||||
from .serializers.jsonserializer import JSONSerializer
|
||||
from .serializers import yamlserializer, jsonserializer
|
||||
|
||||
class VCR(object):
|
||||
def __init__(self, serializer='yaml', cassette_library_dir=None):
|
||||
self.serializer = serializer
|
||||
self.cassette_library_dir = cassette_library_dir
|
||||
self.serializers = {
|
||||
'yaml': YamlSerializer,
|
||||
'json': JSONSerializer,
|
||||
'yaml': yamlserializer,
|
||||
'json': jsonserializer,
|
||||
}
|
||||
|
||||
def _get_serializer(self, serializer_name):
|
||||
|
||||
@@ -13,17 +13,14 @@ def _fix_response_unicode(d):
|
||||
d['body']['string'] = d['body']['string'].encode('utf-8')
|
||||
return d
|
||||
|
||||
class JSONSerializer(object):
|
||||
@classmethod
|
||||
def load(cls, cassette_path):
|
||||
def load(cassette_path):
|
||||
with open(cassette_path) as fh:
|
||||
data = json.load(fh)
|
||||
requests = [Request._from_dict(r['request']) for r in data]
|
||||
responses = [_fix_response_unicode(r['response']) for r in data]
|
||||
return requests, responses
|
||||
|
||||
@classmethod
|
||||
def dumps(cls, requests, responses):
|
||||
def dumps(requests, responses):
|
||||
data = ([{
|
||||
'request': request._to_dict(),
|
||||
'response': response,
|
||||
|
||||
@@ -7,16 +7,14 @@ except ImportError:
|
||||
from yaml import Loader, Dumper
|
||||
|
||||
|
||||
class YamlSerializer(object):
|
||||
@classmethod
|
||||
def load(cls, cassette_path):
|
||||
def load(cassette_path):
|
||||
data = yaml.load(open(cassette_path), Loader=Loader)
|
||||
requests = [r['request'] for r in data]
|
||||
responses = [r['response'] for r in data]
|
||||
return requests, responses
|
||||
|
||||
@classmethod
|
||||
def dumps(cls, requests, responses):
|
||||
|
||||
def dumps(requests, responses):
|
||||
data = ([{
|
||||
'request': request,
|
||||
'response': response,
|
||||
|
||||
Reference in New Issue
Block a user