<reponame>LavanyaKoduri/Cloud-Run-and-Cloud-SQL<filename>rest-api-google-sheets/index.js<gh_stars>0const { google } = require('googleapis');
const express = require('express');
const app = express();

const port = process.env.PORT || 8080;
app.listen(port, () => {
  console.log('SlipSlap REST API listening on port', port);
});

app.get('/:id', async (req, res) => {
  const id = req.params.id;
  const ingredient = await getIngredient(id);
  let retVal;
  if (ingredient) {
    retVal = {status: 'success', data: {ingredient: ingredient}};
  }
  else {
    res.status(404);
    retVal = {status: 'fail', data: {title: `Ingredient ${id} not found`}};
  }
  res.setHeader('content-type', 'application/json');
  res.end(JSON.stringify(retVal));
});

async function getIngredient(id) {
  const auth = await google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/spreadsheets']
  });
  const api = google.sheets({version: 'v4', auth});
  const response = await api.spreadsheets.values.get({
    spreadsheetId: 'se2xy1bknelxn4y8xzxu3trosptip3q5',
    range: 'Ingredients!A:E'
  });
  for (let row of response.data.values) {
    if (row[0] == id) {
      return {
        id: row[0],
        name: row[1],
        amount: row[2],
        unit: row[3],
        warehouseLocation: row[4],
      }
    }
  }
}
