rpc | get rpc from spec | Cell 10 | Search

This code automates the process of fetching API discovery documents, converting them to a specific format, and saving them to local files. It is likely used to prepare API documentation or configuration for a system that relies on Google Discovery.

Run example

npm run import -- "test rpc from spec"

test rpc from spec

var fs = require('fs')
var path = require('path')
var importer = require('../Core')
var getRpcFromSpec = importer.import("get rpc from spec")

function testDiscovery(config = {api: 'drive', version: 'v3'}) {
    var discovery = getRpcFromSpec(require('/Users/briancullinan/Downloads/rest.json'));
//    Promise.resolve(discovery)
    return discovery.apis.getRest(config)
        .then(r => {
        try {
            fs.writeFileSync(path.join(__dirname, `../Resources/APIs/${config.api}.${config.version}.json`),
                             JSON.stringify(r.body, null, 4))
        } catch (up) {
            throw up
        }
        return r.body
    })
}

if(typeof $ !== 'undefined') {
    $.async();
    testDiscovery()
        .then(r => $.sendResult(r))
//        .then(r => $.sendResult(getRpcFromSpec(r)))
        .catch(e => $.sendError(e))
}

module.exports = testDiscovery

What the code could have been:

const fs = require('fs');
const path = require('path');
const importer = require('../Core');
const { getRpcFromSpec } = importer.import();

/**
 * Test API discovery for a given configuration.
 * 
 * @param {Object} config - Configuration object with 'api' and'version' properties.
 * @param {string} config.api - API name.
 * @param {string} config.version - API version.
 * @returns {Promise} Promise resolving with the API discovery result.
 */
async function testDiscovery(config = { api: 'drive', version: 'v3' }) {
    try {
        const discovery = await getRpcFromSpec(require('/Users/briancullinan/Downloads/rest.json'));
        const apiDiscovery = await discovery.apis.getRest(config);
        const apiConfigFile = path.join(__dirname, `../Resources/APIs/${config.api}.${config.version}.json`);
        await fs.promises.writeFile(apiConfigFile, JSON.stringify(apiDiscovery.body, null, 4));
        return apiDiscovery.body;
    } catch (error) {
        throw error;
    }
}

if (typeof $!== 'undefined') {
    try {
        await $.async();
        const result = await testDiscovery();
        $.sendResult(result);
    } catch (error) {
        $.sendError(error);
    }
}

module.exports = testDiscovery;

This code snippet fetches and saves API discovery documents in a specific format.

Key Points:

  • Dependencies: It uses fs for file system operations, path for path manipulation, and importer for loading a function getRpcFromSpec.
  • testDiscovery Function:
    • Takes an optional config object with api and version properties.
    • Loads an OpenAPI specification from a local file (rest.json).
    • Uses getRpcFromSpec to convert the specification into a Google Discovery format.
    • Fetches the API discovery document using the provided configuration.
    • Saves the discovery document to a file in a designated directory.
    • Returns the parsed discovery document.
  • Conditional Execution:
    • The code within the if(typeof $ !== 'undefined') block executes only in a specific environment (likely a browser).
    • It calls testDiscovery, handles the result (sending it as a success or error message), and likely interacts with a framework or environment using $.async(), $.sendResult(), and $.sendError().

Purpose:

This code likely automates the process of fetching, converting, and saving API discovery documents for use in a system that requires them.