Add support for local discovery doc to sample_tools.py.
This patch (originally based on one by @jaybeeo) updates sample_tools to also
accept a local discovery file, and avoids calling out to the discovery service
in that case.
diff --git a/googleapiclient/sample_tools.py b/googleapiclient/sample_tools.py
index 4dd6935..69f698e 100644
--- a/googleapiclient/sample_tools.py
+++ b/googleapiclient/sample_tools.py
@@ -31,7 +31,7 @@
from oauth2client import tools
-def init(argv, name, version, doc, filename, scope=None, parents=[]):
+def init(argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None):
"""A common initialization routine for samples.
Many of the sample applications do the same initialization, which has now
@@ -49,6 +49,7 @@
file: string, filename of the application. Usually set to __file__.
parents: list of argparse.ArgumentParser, additional command-line flags.
scope: string, The OAuth scope used.
+ discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL.
Returns:
A tuple of (service, flags), where service is the service object and flags
@@ -88,6 +89,14 @@
credentials = tools.run_flow(flow, storage, flags)
http = credentials.authorize(http = httplib2.Http())
- # Construct a service object via the discovery service.
- service = discovery.build(name, version, http=http)
+ if discovery_filename is None:
+ # Construct a service object via the discovery service.
+ service = discovery.build(name, version, http=http)
+ else:
+ # Construct a service object using a local discovery document file.
+ with open(discovery_filename) as discovery_file:
+ service = discovery.build_from_document(
+ discovery_file.read(),
+ base='https://www.googleapis.com/',
+ http=http)
return (service, flags)