Added LoggingJsonModel and a sample application, debugging/main.py, on how to use it.
This commit is contained in:
@@ -12,12 +12,17 @@ object representation.
|
|||||||
|
|
||||||
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||||
|
|
||||||
|
import gflags
|
||||||
import logging
|
import logging
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
from anyjson import simplejson
|
from anyjson import simplejson
|
||||||
from errors import HttpError
|
from errors import HttpError
|
||||||
|
|
||||||
|
FLAGS = gflags.FLAGS
|
||||||
|
|
||||||
|
gflags.DEFINE_boolean('dump_request', False,
|
||||||
|
'Dump all http server requests and responses.')
|
||||||
|
|
||||||
def _abstract():
|
def _abstract():
|
||||||
raise NotImplementedError('You need to override this function')
|
raise NotImplementedError('You need to override this function')
|
||||||
@@ -165,3 +170,27 @@ class JsonModel(Model):
|
|||||||
else:
|
else:
|
||||||
logging.debug('Content from bad request was: %s' % content)
|
logging.debug('Content from bad request was: %s' % content)
|
||||||
raise HttpError(resp, content)
|
raise HttpError(resp, content)
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingJsonModel(JsonModel):
|
||||||
|
"""A printable JsonModel class that supports logging response info."""
|
||||||
|
|
||||||
|
def response(self, resp, content):
|
||||||
|
"""An overloaded response method that will output debug info if requested.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
resp: An httplib2.Response object.
|
||||||
|
content: A string representing the response body.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The body de-serialized as a Python object.
|
||||||
|
"""
|
||||||
|
if FLAGS.dump_request:
|
||||||
|
logging.info('--response-start--')
|
||||||
|
for h, v in resp.iteritems():
|
||||||
|
logging.info('%s: %s', h, v)
|
||||||
|
if content:
|
||||||
|
logging.info(content)
|
||||||
|
logging.info('--response-end--')
|
||||||
|
return super(LoggingJsonModel, self).response(
|
||||||
|
resp, content)
|
||||||
|
|||||||
49
samples/debugging/main.py
Normal file
49
samples/debugging/main.py
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#!/usr/bin/python2.4
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright 2010 Google Inc. All Rights Reserved.
|
||||||
|
|
||||||
|
"""Simple command-line example for Translate.
|
||||||
|
|
||||||
|
Command-line application that translates
|
||||||
|
some text.
|
||||||
|
"""
|
||||||
|
|
||||||
|
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||||
|
|
||||||
|
import gflags
|
||||||
|
import logging
|
||||||
|
import pprint
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from apiclient.discovery import build
|
||||||
|
from apiclient.model import LoggingJsonModel
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
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',
|
||||||
|
model=LoggingJsonModel())
|
||||||
|
print service.translations().list(
|
||||||
|
source='en',
|
||||||
|
target='fr',
|
||||||
|
q=['flower', 'car']
|
||||||
|
).execute()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv)
|
||||||
@@ -21,12 +21,19 @@ Unit tests for the JSON model.
|
|||||||
|
|
||||||
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
||||||
|
|
||||||
|
import copy
|
||||||
|
import gflags
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import httplib2
|
import httplib2
|
||||||
|
import apiclient.model
|
||||||
|
|
||||||
from apiclient.model import JsonModel
|
from apiclient.anyjson import simplejson
|
||||||
from apiclient.errors import HttpError
|
from apiclient.errors import HttpError
|
||||||
|
from apiclient.model import JsonModel
|
||||||
|
from apiclient.model import LoggingJsonModel
|
||||||
|
|
||||||
|
FLAGS = gflags.FLAGS
|
||||||
|
|
||||||
# Python 2.5 requires different modules
|
# Python 2.5 requires different modules
|
||||||
try:
|
try:
|
||||||
@@ -179,5 +186,54 @@ class Model(unittest.TestCase):
|
|||||||
content = model.response(resp, content)
|
content = model.response(resp, content)
|
||||||
self.assertEqual(content, 'data goes here')
|
self.assertEqual(content, 'data goes here')
|
||||||
|
|
||||||
|
|
||||||
|
class LoggingModel(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_logging_json_model(self):
|
||||||
|
class MockLogging(object):
|
||||||
|
def __init__(self):
|
||||||
|
self.info_record = []
|
||||||
|
self.debug_record = []
|
||||||
|
def info(self, message, *args):
|
||||||
|
self.info_record.append(message % args)
|
||||||
|
|
||||||
|
def debug(self, message, *args):
|
||||||
|
self.debug_record.append(message % args)
|
||||||
|
|
||||||
|
class MockResponse(dict):
|
||||||
|
def __init__(self, items):
|
||||||
|
super(MockResponse, self).__init__()
|
||||||
|
self.status = items['status']
|
||||||
|
for key, value in items.iteritems():
|
||||||
|
self[key] = value
|
||||||
|
apiclient.model.logging = MockLogging()
|
||||||
|
apiclient.model.FLAGS = copy.deepcopy(FLAGS)
|
||||||
|
apiclient.model.FLAGS.dump_request = True
|
||||||
|
model = LoggingJsonModel()
|
||||||
|
request_body = {
|
||||||
|
'field1': 'value1',
|
||||||
|
'field2': 'value2'
|
||||||
|
}
|
||||||
|
body_string = model.request({}, {}, {}, request_body)[-1]
|
||||||
|
json_body = simplejson.loads(body_string)
|
||||||
|
self.assertEqual(request_body, json_body)
|
||||||
|
|
||||||
|
response = {'status': 200,
|
||||||
|
'response_field_1': 'response_value_1',
|
||||||
|
'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(simplejson.loads(apiclient.model.logging.info_record[-2]),
|
||||||
|
request_body)
|
||||||
|
self.assertEqual(apiclient.model.logging.info_record[-1],
|
||||||
|
'--response-end--')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user