Now using the dataWrapper features flag to control serialization of JSON requests.
diff --git a/apiclient/discovery.py b/apiclient/discovery.py
index b7bc823..7ae6a8a 100644
--- a/apiclient/discovery.py
+++ b/apiclient/discovery.py
@@ -68,7 +68,7 @@
http=None,
discoveryServiceUrl=DISCOVERY_URI,
developerKey=None,
- model=JsonModel(),
+ model=None,
requestBuilder=HttpRequest):
"""Construct a Resource for interacting with an API.
@@ -125,7 +125,7 @@
future=None,
http=None,
developerKey=None,
- model=JsonModel(),
+ model=None,
requestBuilder=HttpRequest):
"""Create a Resource for interacting with an API.
@@ -160,6 +160,8 @@
future = {}
auth_discovery = {}
+ if model is None:
+ model = JsonModel('dataWrapper' in service.get('features', []))
resource = createResource(http, base, model, requestBuilder, developerKey,
service, future)
diff --git a/apiclient/http.py b/apiclient/http.py
index f713ec8..094271f 100644
--- a/apiclient/http.py
+++ b/apiclient/http.py
@@ -150,7 +150,7 @@
resp, content = self.responses[methodId]
return HttpRequestMock(resp, content, postproc)
else:
- model = JsonModel()
+ model = JsonModel(False)
return HttpRequestMock(None, '{}', model.response)
diff --git a/apiclient/model.py b/apiclient/model.py
index 58cd624..55bcc94 100644
--- a/apiclient/model.py
+++ b/apiclient/model.py
@@ -73,6 +73,14 @@
object representation of HTTP request and response bodies.
"""
+ def __init__(self, data_wrapper):
+ """Construct a JsonModel
+
+ Args:
+ data_wrapper: boolean, wrap requests and responses in a data wrapper
+ """
+ self._data_wrapper = data_wrapper
+
def request(self, headers, path_params, query_params, body_value):
"""Updates outgoing requests with JSON bodies.
@@ -97,6 +105,10 @@
else:
headers['user-agent'] = ''
headers['user-agent'] += 'google-api-python-client/1.0'
+
+ if (isinstance(body_value, dict) and 'data' not in body_value and
+ self._data_wrapper):
+ body_value = {'data': body_value}
if body_value is None:
return (headers, path_params, query, None)
else:
diff --git a/tests/test_json_model.py b/tests/test_json_model.py
index 084e0a9..fa7ad05 100644
--- a/tests/test_json_model.py
+++ b/tests/test_json_model.py
@@ -36,7 +36,7 @@
class Model(unittest.TestCase):
def test_json_no_body(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
headers = {}
path_params = {}
@@ -51,7 +51,7 @@
self.assertEqual(body, None)
def test_json_body(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
headers = {}
path_params = {}
@@ -65,9 +65,24 @@
self.assertNotEqual(query, '')
self.assertEqual(body, '{}')
+ def test_json_body_data_wrapper(self):
+ model = JsonModel(data_wrapper=True)
+
+ headers = {}
+ path_params = {}
+ query_params = {}
+ body = {}
+
+ headers, params, query, body = model.request(headers, path_params, query_params, body)
+
+ self.assertEqual(headers['accept'], 'application/json')
+ self.assertEqual(headers['content-type'], 'application/json')
+ self.assertNotEqual(query, '')
+ self.assertEqual(body, '{"data": {}}')
+
def test_json_body_default_data(self):
"""Test that a 'data' wrapper doesn't get added if one is already present."""
- model = JsonModel()
+ model = JsonModel(data_wrapper=True)
headers = {}
path_params = {}
@@ -82,7 +97,7 @@
self.assertEqual(body, '{"data": "foo"}')
def test_json_build_query(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
headers = {}
path_params = {}
@@ -100,7 +115,7 @@
self.assertEqual(body, '{}')
def test_user_agent(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
headers = {'user-agent': 'my-test-app/1.23.4'}
path_params = {}
@@ -112,7 +127,7 @@
self.assertEqual(headers['user-agent'], 'my-test-app/1.23.4 google-api-python-client/1.0')
def test_bad_response(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
resp = httplib2.Response({'status': '401'})
resp.reason = 'Unauthorized'
content = '{"error": {"message": "not authorized"}}'
@@ -133,7 +148,7 @@
def test_good_response(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=True)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '{"data": "is good"}'
@@ -142,7 +157,7 @@
self.assertEqual(content, 'is good')
def test_good_response_wo_data(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '{"foo": "is good"}'
@@ -151,7 +166,7 @@
self.assertEqual(content, {'foo': 'is good'})
def test_good_response_wo_data_str(self):
- model = JsonModel()
+ model = JsonModel(data_wrapper=False)
resp = httplib2.Response({'status': '200'})
resp.reason = 'OK'
content = '"data goes here"'