4

I'm trying to build a JSON file by making successive HTTP requests with Axios:

  1. Get an array of objects (projects)
  2. Create an array property in each project named attachments
  3. Get each project's tasks
  4. Get each task's attachments
  5. Push each project's task's attachments in to the project's attachments array
  6. Create a JSON file out of the modified projects array

Code:

let getProjects = function() {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/')
  } catch (error) {
    console.error(error)
  }
}

let getTasks = function(project) {
  try {
    return axios.get('https://app.asana.com/api/1.0/projects/'+project+'/tasks')
  } catch (error) {
    console.error(error)
  }
}

let getAttachments = function(task) {
  try {
    return axios.get('https://app.asana.com/api/1.0/tasks/'+task+'/attachments')
  } catch (error) {
    console.error(error)
  }
}

async function getAsanaData() {
  let projects = await getProjects()
  return Promise.all(projects.data.data.map(async (project) => {
      project.attachments = []
      let tasks = await getTasks(project.gid)
      return Promise.all(tasks.data.data.map(async (task) => {
        let attachments = await getAttachments(task.gid)
        project.attachments = !!attachments ? project.attachments.concat(attachments.data.data) : project.attachments 
        return project
      }))
  }))
}

getAsanaData()
.then((projects) => {  
  var asanaData = safeJsonStringify(projects);
  fs.writeFile("thing.json", asanaData);
})
.catch(err=>console.log(err))

But I'm running into this error:

status: 429,
statusText: 'Too Many Requests

I haven't found anything helpful yet for figuring out how to resolve it. What can I do?

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Gabriel Rivera
  • 175
  • 1
  • 1
  • 6
  • Sounds like, as it says, you're making too many requests, perhaps - how many requests are you making in a row with your current code? – CertainPerformance Nov 03 '18 at 02:09
  • You need to throttle your requests - asana is likely limiting you. The use of underscore or lodash is recommended. Both have a throttle method to slow down the velocity of your requests. – Randy Casburn Nov 03 '18 at 02:11
  • @CertainPerformance Apologies; I don't yet know how to get feedback like number of requests attempted. But having an idea of the data, it's probably in the several thousands. – Gabriel Rivera Nov 03 '18 at 02:15
  • **READ:** https://asana.com/developers/documentation/getting-started/rate-limits – Randy Casburn Nov 03 '18 at 02:15
  • How big is the `projects.data.data` array? – jfriend00 Nov 03 '18 at 02:35

2 Answers2

0

HTTP response status code 429 indicates sending too many requests than what server could handle. It has been documented at https://asana.com/developers/documentation/getting-started/errors too. The maximum allowed is 150 per minute as documented at https://asana.com/developers/documentation/getting-started/rate-limits.

So, yes, as @Randy Casburn commented, you will have to throttle your requests.

Raj
  • 778
  • 1
  • 10
  • 14
0

You're getting throttled by Asana for sending too many requests and reaching the maximum rate.

When it happens, you need to check for the Retry-After response header and wait for the specified amount of time before sending another request.

https://asana.com/developers/documentation/getting-started/rate-limits

You can also learn more in the RFC 6585 about HTTP 429

Community
  • 1
  • 1
Anthony Simmon
  • 1,579
  • 12
  • 26