Added more logging and changed flag name
This commit is contained in:
@@ -4,6 +4,7 @@ import pickle
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class OAuthCredentialsField(models.Field):
|
||||
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
@@ -21,9 +21,10 @@ from errors import HttpError
|
||||
|
||||
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 @@ class JsonModel(Model):
|
||||
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 @@ class LoggingJsonModel(JsonModel):
|
||||
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 @@ class LoggingJsonModel(JsonModel):
|
||||
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)
|
||||
|
||||
@@ -21,23 +21,20 @@ 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
|
||||
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',
|
||||
|
||||
@@ -70,7 +70,7 @@ def main():
|
||||
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)
|
||||
|
||||
@@ -121,8 +121,8 @@ def main():
|
||||
"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:
|
||||
|
||||
@@ -208,7 +208,7 @@ class LoggingModel(unittest.TestCase):
|
||||
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 @@ class LoggingModel(unittest.TestCase):
|
||||
'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],
|
||||
|
||||
Reference in New Issue
Block a user