Search code examples
rjirajira-rest-apihttr

httr::content throws error when supplying a variable as the request object


I am trying to get a list of Jira tickets that were opened from anyone within an organization. I have a list of user IDs but there are nearly 1400 people in this organization.

Since there are this many folks I cannot simply query all of them in 1 request so I created a loop to do so. The GET request part of the loop works fine and I get a response back for each ID (whether or not any Jira tickets were actually created by that user). Where I run into an issue is trying to extract the content of these responses, httr::content throws the error is.response(x) is not true . This seems to be due to me passing the list as x to 'content' though I have tried some other options as well with no success.

org_list <- as.data.frame(read.csv("org_list.csv"))

Where the csv file is like this (but with 1378 IDs):

ID 
abc123 
xyz987 

Here is the loop:

for (i in 1:1378) {

assign(paste0("query_", org_list[i,]), GET("https://myjira.com/", 
                   path = "rest/api/2/search", 
                   query = list(jql = paste0("project in (ABC,DEF,XYZ) AND reporter = ", org_list[i,]), maxResults = 500),
                   authenticate(my_UN, my_PW),
                   verbose()
) 
)

  tmp_var <-  as.character(org_list[i,])
  api_request_content <- httr::content(tmp_var, as = "text")
  api_request_content_flat <- fromJSON(api_request_content, flatten = TRUE)
  assign(paste0("data_", tmp_var), as.data.frame(api_request_content_flat$issues))
  rm(paste0("query_", tmp_var))

}

This creates a response object like "query_abc123", "query_xyz987", etc. The loop itself works fine if I remove everything outside of 'assign' function.

I have also tried:

api_request_content <- httr::content(org_list[i,], as = "text")

And

api_request_content <- httr::content(paste0(org_list[i,]), as = "text")

And they all return the same error. But if I manually pass the object like

api_request_content <- httr::content(query_abc123, as = "text")

Everything works as intended. An afternoon of Googling hasn't returned anything even close to an answer for me on this.


Solution

  • Without a proper reproducible example, this is all kind of guess work, but a more common strategy would be something like this

    alldata <- dplyr::bind_rows(lapply(as.character(org_list[[1]]), function(reporter) {
    
      req <- GET("https://myjira.com/", 
                path = "rest/api/2/search", 
                query = list(jql = paste0("project in (ABC,DEF,XYZ) AND reporter = ", reporter), maxResults = 500),
                authenticate(my_UN, my_PW),
                verbose()
      ) 
    
      api_request_content <- httr::content(req, as = "text")
      api_request_content_flat <- fromJSON(api_request_content, flatten = TRUE)
      as.data.frame(api_request_content_flat$issues)
    }))