def GetApiDiscovery(api_name, api_version): """Get a discovery doc from the discovery server.""" api_path = 'apis/%s/%s/rest' % (api_name, api_version) discovery_url = 'https://%s/discovery/%s/%s' % ( FLAGS.discovery_server, FLAGS.discovery_version, api_path) http = httplib2.Http() _, content = http.request(discovery_url) discovery_doc = json.loads(content) error = discovery_doc.get('error') if error: raise app.Error(error) return content
def main(unused_argv): if not (FLAGS.api_name or FLAGS.input): raise app.UsageError('You must specify one of --api_name or --input') if not (FLAGS.output_dir or FLAGS.output_file): raise app.UsageError( 'You must specify one of --output_dir or --output_file') # Get the discovery document if FLAGS.api_name: if not FLAGS.api_version: raise app.UsageError( 'You must specify --api_version with --api_name') api_path = 'apis/%s/%s/rest' % (FLAGS.api_name, FLAGS.api_version) discovery_url = 'https://%s/discovery/%s/%s' % ( FLAGS.discovery_server, FLAGS.discovery_version, api_path) http = httplib2.Http() unused_resp, content = http.request(discovery_url) discovery_doc = simplejson.loads(content) error = discovery_doc.get('error') if error: raise app.Error(error) else: f = open(FLAGS.input) discovery_doc = simplejson.loads(f.read()) f.close() options = { # Emit a manifest file like a source jar 'emit_manifest': False, # Include other files needed to compile (e.g. base jar files) 'include_dependencies': True, # WARNING - change to false before commit # Put all the sources into a source jar 'include_source_jar': False, # Include the timestamp in the generated library 'include_timestamp': FLAGS.include_timestamp, # Prefix paths in the output with the library name 'use_library_name_in_path': False, # Put API version in the package 'version_package': FLAGS.version_package, } if FLAGS.output_type == 'full': options['emit_manifest'] = True options['include_dependencies'] = True options['include_source_jar'] = True options['use_library_name_in_path'] = True # Instantiate the right code generator generators = { 'csharp': CSharpGenerator, 'go': GoGenerator, 'gwt': GwtGenerator, 'java': JavaGenerator, 'objc': ObjCGenerator, 'php': PHPGenerator, } if FLAGS.language in generators: generator = generators[FLAGS.language](discovery_doc, options=options) else: raise app.UsageError('Unsupported language option: %s' % FLAGS.language) # Get the path to the template set. language_variants = Targets().TargetsForLanguage(FLAGS.language) variant_features = language_variants[FLAGS.language_variant] template_dir = os.path.join(os.path.dirname(__file__), FLAGS.language, variant_features['path']) generator.SetTemplateDir(template_dir) generator.SetSurfaceFeatures(variant_features) # Get an output writer if FLAGS.output_dir: package_writer = FilesystemLibraryPackage(FLAGS.output_dir) else: out = open(FLAGS.output_file, 'w') package_writer = ZipLibraryPackage(out) if options.get('emit_manifest'): package_writer.IncludeMinimalJarManifest( created_by='1.0.0-googleapis-v1 (Google Inc.)') # do it generator.GeneratePackage(package_writer) package_writer.DoneWritingArchive() if FLAGS.output_file: out.close() return 0