Added more logging and changed flag name
diff --git a/apiclient/ext/django_orm.py b/apiclient/ext/django_orm.py
index d52d866..c92c3af 100644
--- a/apiclient/ext/django_orm.py
+++ b/apiclient/ext/django_orm.py
@@ -4,6 +4,7 @@
from django.db import models
+
class OAuthCredentialsField(models.Field):
__metaclass__ = models.SubfieldBase
diff --git a/apiclient/model.py b/apiclient/model.py
index b1f7c0e..4ca91f4 100644
--- a/apiclient/model.py
+++ b/apiclient/model.py
@@ -21,9 +21,10 @@
FLAGS = gflags.FLAGS
-gflags.DEFINE_boolean('dump_request', False,
+gflags.DEFINE_boolean('dump_request_response', False,
'Dump all http server requests and responses.')
+
def _abstract():
raise NotImplementedError('You need to override this function')
@@ -115,11 +116,10 @@
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:
+ if body_value is not None:
headers['content-type'] = 'application/json'
- return (headers, path_params, query, simplejson.dumps(body_value))
+ body_value = simplejson.dumps(body_value)
+ return (headers, path_params, query, body_value)
def _build_query(self, params):
"""Builds a query string.
@@ -185,7 +185,7 @@
Returns:
The body de-serialized as a Python object.
"""
- if FLAGS.dump_request:
+ if FLAGS.dump_request_response:
logging.info('--response-start--')
for h, v in resp.iteritems():
logging.info('%s: %s', h, v)
@@ -194,3 +194,38 @@
logging.info('--response-end--')
return super(LoggingJsonModel, self).response(
resp, content)
+
+ def request(self, headers, path_params, query_params, body_value):
+ """An overloaded request method that will output debug info if requested.
+
+ Args:
+ headers: dict, request headers
+ path_params: dict, parameters that appear in the request path
+ query_params: dict, parameters that appear in the query
+ body_value: object, the request body as a Python object, which must be
+ serializable by simplejson.
+ Returns:
+ A tuple of (headers, path_params, query, body)
+
+ headers: dict, request headers
+ path_params: dict, parameters that appear in the request path
+ query: string, query part of the request URI
+ body: string, the body serialized as JSON
+ """
+ (headers, path_params, query, body) = super(
+ LoggingJsonModel, self).request(
+ headers, path_params, query_params, body_value)
+ if FLAGS.dump_request_response:
+ logging.info('--request-start--')
+ logging.info('-headers-start-')
+ for h, v in headers.iteritems():
+ logging.info('%s: %s', h, v)
+ logging.info('-headers-end-')
+ logging.info('-path-parameters-start-')
+ for h, v in path_params.iteritems():
+ logging.info('%s: %s', h, v)
+ logging.info('-path-parameters-end-')
+ logging.info('body: %s', body)
+ logging.info('query: %s', query)
+ logging.info('--request-end--')
+ return (headers, path_params, query, body)
diff --git a/samples/debugging/main.py b/samples/debugging/main.py
index 6fe0c0f..6108f47 100644
--- a/samples/debugging/main.py
+++ b/samples/debugging/main.py
@@ -21,23 +21,20 @@
FLAGS = gflags.FLAGS
-# Uncomment the next line to get very detailed logging
-# httplib2.debuglevel = 4
-# create logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def main(argv):
try:
- argv = FLAGS(argv) # parse flags
+ argv = FLAGS(argv)
except gflags.FlagsError, e:
print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
sys.exit(1)
service = build('translate', 'v2',
- developerKey='AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0',
+ developerKey='AIzaSyAQIKv_gwnob-YNrXV2stnY86GSGY81Zr0',
model=LoggingJsonModel())
print service.translations().list(
source='en',
diff --git a/samples/prediction/main.py b/samples/prediction/main.py
index 5ae8d36..77c9df5 100644
--- a/samples/prediction/main.py
+++ b/samples/prediction/main.py
@@ -70,7 +70,7 @@
print 'Training is complete'
# Now make a prediction using that training
- body = {'input': {'mixture':["mucho bueno"]}}
+ body = {'input': {'mixture': ["mucho bueno"]}}
prediction = service.predict(body=body, data=OBJECT_NAME).execute()
print 'The prediction is:'
pprint.pprint(prediction)
diff --git a/samples/threadqueue/main.py b/samples/threadqueue/main.py
index 4570452..5415a65 100644
--- a/samples/threadqueue/main.py
+++ b/samples/threadqueue/main.py
@@ -121,8 +121,8 @@
"presenter": "me"
}
}
- topic_request = service.topics().insert(seriesId=series['id']['seriesId'],
- body=topic_body)
+ topic_request = service.topics().insert(
+ seriesId=series['id']['seriesId'], body=topic_body)
print "Adding request to queue"
queue.put(topic_request)
except CredentialsInvalidError:
diff --git a/tests/test_json_model.py b/tests/test_json_model.py
index 43455d2..29130d2 100644
--- a/tests/test_json_model.py
+++ b/tests/test_json_model.py
@@ -208,7 +208,7 @@
self[key] = value
apiclient.model.logging = MockLogging()
apiclient.model.FLAGS = copy.deepcopy(FLAGS)
- apiclient.model.FLAGS.dump_request = True
+ apiclient.model.FLAGS.dump_request_response = True
model = LoggingJsonModel()
request_body = {
'field1': 'value1',
@@ -223,11 +223,13 @@
'response_field_2': 'response_value_2'}
response_body = model.response(MockResponse(response), body_string)
self.assertEqual(request_body, response_body)
- self.assertEqual(apiclient.model.logging.info_record[:4],
- ['--response-start--',
- 'status: 200',
- 'response_field_1: response_value_1',
- 'response_field_2: response_value_2'])
+ self.assertEqual(apiclient.model.logging.info_record[:2],
+ ['--request-start--',
+ '-headers-start-'])
+ self.assertTrue('response_field_1: response_value_1' in
+ apiclient.model.logging.info_record)
+ self.assertTrue('response_field_2: response_value_2' in
+ apiclient.model.logging.info_record)
self.assertEqual(simplejson.loads(apiclient.model.logging.info_record[-2]),
request_body)
self.assertEqual(apiclient.model.logging.info_record[-1],