Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63caac0

Browse files
committed
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.
1 parent 7547de6 commit 63caac0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

googleapiclient/sample_tools.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from oauth2client import tools
3232

3333

34-
def init(argv, name, version, doc, filename, scope=None, parents=[]):
34+
def init(argv, name, version, doc, filename, scope=None, parents=[], discovery_filename=None):
3535
"""A common initialization routine for samples.
3636
3737
Many of the sample applications do the same initialization, which has now
@@ -49,6 +49,7 @@ def init(argv, name, version, doc, filename, scope=None, parents=[]):
4949
file: string, filename of the application. Usually set to __file__.
5050
parents: list of argparse.ArgumentParser, additional command-line flags.
5151
scope: string, The OAuth scope used.
52+
discovery_filename: string, name of local discovery file (JSON). Use when discovery doc not available via URL.
5253
5354
Returns:
5455
A tuple of (service, flags), where service is the service object and flags
@@ -88,6 +89,14 @@ def init(argv, name, version, doc, filename, scope=None, parents=[]):
8889
credentials = tools.run_flow(flow, storage, flags)
8990
http = credentials.authorize(http = httplib2.Http())
9091

91-
# Construct a service object via the discovery service.
92-
service = discovery.build(name, version, http=http)
92+
if discovery_filename is None:
93+
# Construct a service object via the discovery service.
94+
service = discovery.build(name, version, http=http)
95+
else:
96+
# Construct a service object using a local discovery document file.
97+
with open(discovery_filename) as discovery_file:
98+
service = discovery.build_from_document(
99+
discovery_file.read(),
100+
base='https://www.googleapis.com/',
101+
http=http)
93102
return (service, flags)

0 commit comments

Comments
 (0)