Jonas Alvarson
Jonas Alvarson

Reputation: 471

How to save external API response to Firebase

Im working with a React App where I present a list top Podcasts. I'm using iTunes Search API to dynamically present data to the user. For now, I working with a Node Express server to setup my custom endpoints. The problem is that the API has a request limit, so I tought that I could save what I get from the response to Firebase and present the data from firebase instead.

To my question;
Can in some way save the response I get from iTunes Search API to Firebase?

For now my code for fetching data from my API Endpoints looks like this in my Node+Express server:

const express = require('express');
const unirest = require('unirest');

const app = express();
const port = process.env.PORT || 5000;

// Get all Episodes from a specific podcast
app.get('/api/podcast/episodes', (req, res) => {
  const feedurl = req.query.feedurl
  unirest.get(feedurl)
        .end((response) => {
            res.status(200).send(response.body)
  });
});

// Get Podcast by ID
app.get('/api/podcast/:id', (req, res) => {
  const podID = req.params.id;
  unirest.get(`https://itunes.apple.com/lookup?id=${podID}&country=se`)
        .end((response) => {
            res.status(200).send(response.body)
  });
});

// Get Podcast Categorys
app.get('/api/podcast/:category/:amount', (req, res) => {
  const categoryID = req.params.category;
  const amount = req.params.amount;
  unirest.get(`https://itunes.apple.com/se/rss/toppodcasts/limit=${amount}/genre=${categoryID}/explicit=true/json`)
        .end((response) => {
            res.status(200).send(response.body)
  });
});

// Get Podcast Categorys
app.get('/api/categorys', (req, res) => {
  unirest.get('https://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=26&cc=se')
        .end((response) => {
            res.status(200).send(response.body)
  });
});


app.listen(port, () => console.log(`Listening on port ${port}`));

Im just looking for someone who could point me in the right direction how to proceed. Cause for now I'm stuck, big time.

Upvotes: 1

Views: 860

Answers (1)

bentesha
bentesha

Reputation: 948

Depending on how long you want to cache the response, you can use a whole different things - a physical database like MySql, Sqlite, MongoDB etc to locally persist data.

If you only want to keep the cached result for a short period of time, you can use in-memory cache or just any other tool that offers you same functionality. Redis is also a good contender as a temporary store, especially when you expect to scale to more than one node instance for your application.

Below, I have modified a part of your code to cache result for 10mins, using memory-cache npm module

const express = require('express');
const unirest = require('unirest');
const cache = require('memory-cache');

const CACHE_DURATION = 10 * 60 * 1000; //10mins

const app = express();
const port = process.env.PORT || 5000;

// Get all Episodes from a specific podcast
app.get('/api/podcast/episodes', (req, res) => {
  const cacheKey = req.query.feedurl; //Or anything unique to this route
  const cachedData = cache.get(cacheKey);
  if(cachedData) {
    return res.json(cachedData);
  }

  const feedurl = req.query.feedurl
  unirest.get(feedurl)
        .end((response) => {
            res.status(200).send(response.body);
  cache.put(cacheKey, response.body, CACHE_DURATION);
  });
});
---- the rest of your code ----

You can hit the route as many times as you want and be guaranteed that data will be fetched from iTunes only once in 10mins.

The second and subsequent requests will be served a lot faster from cache.

Let me know if this is what you are looking for.

Upvotes: 1

Related Questions