How to determine intervals for a request?

Asked

Viewed 86 times

-2

Good morning, you guys! I have the following scenario: I am consuming a Google API that returns me data from a particular Youtube channel, basically I do the request and the return is a list of video Urls. Only the API has a quota of 10,000 requests per day, and this application will receive much more than this daily, because it is a dynamic commercial.

I was wondering if you could determine that only one requisition per day be made. Example: I make a request today, save the values within a variable or object, and the next day make a new request to update the list of Urls.

The code I have is this:

let json = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${userId}&part=snippet,id&order=date&maxResults=${getMax}`;

$.getJSON(json, function(response){

    for (var i = 0; i < response['videoId'].length; i++){
        var videoId = response['videoId'][i];
        let urlVideo = `https://www.youtube.com/embed/${videoId}`;
        const newElement = document.createElement('iframe');
          newElement.setAttribute('src', urlVideo);

          mySiema.append(newElement);
        }   
});
  • You cannot store the received data in a database and check whether a request has already been made that day?

  • Actually I did not want to involve database in this application. It would be possible to do this same check with Javascript?

  • Assuming that you will need to store this information somewhere, I can only think of cookies or Torage location. Of course it will not work if the user clears cookies or accesses from a different browser, but maybe it fits your scenario.

  • Do you want this delivery when the customer comes back to your page or do you want it to be installed in the browser as if it were a deamom? If it is the first case a cookie resolves or web push inviting registered customers to the page. In the second case there is as it is a matter of security and ephemerality of the data in the browser.

  • The cookie solution seems to me to be a good one, but how would I ban the request? In this case, how to inform that another request should only be made in 24h?

  • I believe it would just save the day you requested the cookie. Before making the request, you check if the last day of the request was today. If it is, you do not make the request.

  • Thanks friend, really. I will try to solve this problem in this way

  • Set a timer to only when given the desired interval, run the request again.

  • I put an answer to make something more visible. If it is useful, you can accept it by clicking on the check that is on the left of the answer. :)

Show 4 more comments

1 answer

0

Basically, you need to store the data somewhere, so you don’t need to perform the request again.

As you do not want to use database, the only way I see it would be using cookies or location (which I recommend, inclusive, you can read more about it at this link from MDN).

One logic you can follow is to save in Storage location along with the data from the videos, the date on which you made the last request. Before making any request, you get this data and check if you have already made the request today. That way, you will only make a daily request.

  • But in that case, a request would be made to each user, correct? I am trying a solution to make a daily request, and for any banner user it will not make a new request. I don’t know if I could make myself clear

  • Do you mean different users on different machines? Then the above solution won’t work at all. I believe that only involving database to be able to save the information and have access in different accesses.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.