Skip to content

Commit

Permalink
Add support for specifying version identifier in generated files. (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
cglouch authored Sep 16, 2024
1 parent 484c671 commit dbffcca
Show file tree
Hide file tree
Showing 4 changed files with 760 additions and 9 deletions.
8 changes: 7 additions & 1 deletion apitools/gen/gen_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _GetCodegenFromFlags(args):

client_info = util.ClientInfo.Create(
discovery_doc, args.scope, client_id, client_secret,
args.user_agent, names, args.api_key)
args.user_agent, names, args.api_key, args.version_identifier)
outdir = os.path.expanduser(args.outdir) or client_info.default_directory
if os.path.exists(outdir) and not args.overwrite:
raise exceptions.ConfigurationValueError(
Expand Down Expand Up @@ -225,6 +225,12 @@ def main(argv=None):
help=('Base package path of protorpc '
'(defaults to apitools.base.protorpclite'))

parser.add_argument(
'--version-identifier',
help=('Version identifier to use for the generated client (defaults to '
'"version" value in discovery doc). This must be a valid '
'identifier when used in a Python module name.'))

parser.add_argument(
'--outdir',
default='',
Expand Down
38 changes: 32 additions & 6 deletions apitools/gen/gen_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def testGenClient_SimpleDocNoInit(self):
])
expected_files = (
set(['dns_v1_client.py', 'dns_v1_messages.py']))
self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
self.assertEqual(expected_files, set(os.listdir(tmp_dir_path)))

def testGenClient_SimpleDocEmptyInit(self):
with test_utils.TempDir() as tmp_dir_path:
Expand All @@ -69,7 +69,7 @@ def testGenClient_SimpleDocEmptyInit(self):
])
expected_files = (
set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']))
self.assertEquals(expected_files, set(os.listdir(tmp_dir_path)))
self.assertEqual(expected_files, set(os.listdir(tmp_dir_path)))
init_file = _GetContent(os.path.join(tmp_dir_path, '__init__.py'))
self.assertEqual("""\"""Package marker file.\"""
Expand All @@ -91,7 +91,7 @@ def testGenClient_SimpleDocWithV4(self):
'--root_package', 'google.apis',
'client'
])
self.assertEquals(
self.assertEqual(
set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
set(os.listdir(tmp_dir_path)))

Expand All @@ -106,10 +106,36 @@ def testGenClient_SimpleDocWithV5(self):
'--root_package', 'google.apis',
'client'
])
self.assertEquals(
self.assertEqual(
set(['dns_v1_client.py', 'dns_v1_messages.py', '__init__.py']),
set(os.listdir(tmp_dir_path)))

def testGenClient_ApiVersioning(self):
with test_utils.TempDir() as tmp_dir_path:
gen_client.main([
gen_client.__file__,
'--infile', GetTestDataPath(
'dns', 'dns_2015-08-07-preview.json'),
'--outdir', tmp_dir_path,
'--overwrite',
'--version-identifier', 'v2015_08_07_preview',
'--root_package', 'google.apis',
'client'
])
self.assertEqual(
set([
'dns_v2015_08_07_preview_client.py',
'dns_v2015_08_07_preview_messages.py',
'__init__.py']),
set(os.listdir(tmp_dir_path)))
client_file = _GetContent(
os.path.join(tmp_dir_path, 'dns_v2015_08_07_preview_client.py'))
# Check that "apiVersion" system parameter values from discovery doc
# appear in generated client.
self.assertIn('2015-01-01-preview', client_file)
self.assertIn('2015-02-02-preview', client_file)
self.assertIn('2015-03-03-preview', client_file)

def testGenPipPackage_SimpleDoc(self):
with test_utils.TempDir() as tmp_dir_path:
gen_client.main([
Expand All @@ -120,7 +146,7 @@ def testGenPipPackage_SimpleDoc(self):
'--root_package', 'google.apis',
'pip_package'
])
self.assertEquals(
self.assertEqual(
set(['apitools', 'setup.py']),
set(os.listdir(tmp_dir_path)))

Expand All @@ -134,6 +160,6 @@ def testGenProto_SimpleDoc(self):
'--root_package', 'google.apis',
'proto'
])
self.assertEquals(
self.assertEqual(
set(['dns_v1_messages.proto', 'dns_v1_services.proto']),
set(os.listdir(tmp_dir_path)))
Loading

0 comments on commit dbffcca

Please sign in to comment.