0

I have implemented a function to fetch the drive files and display their path of the file in the drive. I take a file's parents field and hit the file API again to fetch the name of the parent folder name. If the folder has a parent again, then I call the same function again and so I made it recursive. I have a common array to populate the file paths name. So, If I can now process only one file at a time. If I try to process many files at a time since the array to populate path of a file is declared globally, the parallel process doesn't populate my file with appropriate results. Looking for a better approach.

let labels = [];
getDriveItems: ({params}) => {
    // hit API to fetch all files
    //for each file call get path
    if (nextPageToken) {
        getDriveItems({params})
    }
}

getpath(fileDetails) {
    if (fileDetails.parents) {
        parentFileDetails = {get file details in parentFileDetails}
        labels.push(parentFileDetails.title);
        if (parentFileDetails.parents.length > 0) {
            getpath(parentFileDetails);
        }
    }
}
Kousika Ganesan
  • 539
  • 1
  • 6
  • 22
  • 1
    The code is too unspecific. Actual API calls aren't shown, it's unclear how it can be improved and what 'decreases my API performance' actually means. The code is currently synchronous. Of course, it blocks main thread if there's a considerable amount of recursive calls. – Estus Flask Jul 21 '18 at 11:56
  • @estus. Well, I am looking for a solution to resolve that block! – Kousika Ganesan Jul 21 '18 at 11:59
  • see option 3 of https://stackoverflow.com/questions/41741520/how-do-i-search-sub-folders-and-sub-sub-folders-in-google-drive – pinoyyid Jul 21 '18 at 12:04
  • @pinoyyid that's cool. But In my scenario, I store only files and not the folders – Kousika Ganesan Jul 21 '18 at 12:27
  • Again, *it's unclear how it can be improved and what 'decreases my API performance' actually mean*. Please, provide all relevant details that describe that 'my scenario'. Specific code-related problems need https://stackoverflow.com/help/mcve . Otherwise the question is considered off-topic and can be closed. – Estus Flask Jul 21 '18 at 13:12
  • A simple thought.. use batch requests. If you already are, you should actually provide call details in your question. – tehhowch Jul 21 '18 at 13:32
  • 1
    do not use batch requests. They achieve nothing and massively complicate your code. – pinoyyid Jul 21 '18 at 23:31
  • 2
    @KøusikaGanesh You have misunderstood option 3 of that answer. You should fetch *all* of the folders in a single API call. Then create an in-memory tree of your folder structure onto which you can then map your files. – pinoyyid Jul 21 '18 at 23:32
  • @pinoyyid so you say I need to preserve all details I fetch during the call to fetch all drive items...and then map through the preserved data ... Instead of hitting another time!!! Could you please let me know whether I'm correct? – Kousika Ganesan Jul 22 '18 at 08:45
  • fetch all folders, not all items – pinoyyid Jul 23 '18 at 05:12
  • Will try it @pinoyyid – Kousika Ganesan Jul 23 '18 at 08:04

0 Answers0