R/util.R

paginate <- function(url, usr, ps, parms=NA) {
  
  
  ### COMBINE PARMS WITH COUNT=TRUE ---
  if(all(is.na(parms))) {
    query <- list(count='true')
  } else {
    query <- c(parms,list(count='true'))
  }
  
  
  ### LOOP THROUGH URLS ---
  
  results <- lapply(url, function(url) GET( url, authenticate( usr,pw ), query = query ) )
  names(results) <- url
  
  ### FIND # OF PAGES FOR EACH URL
  results <- lapply(results, function(results) content(results) )
  results <- lapply(results, function(results) ifelse(class(results)=='integer', as.numeric(results) , 1 ) )
  results <- lapply(results, function(results) ifelse(results==0, 1, results) )
  
  ### CALCULATE # OF PAGES BASED ON EMMAS RESULTS LIMIT
  limit <- 500
  
  output <- lapply(results, function(results) {
    pages <- ceiling(results/limit)
    indexEnd <- seq(1, pages, by=1 )
    indexEnd <- indexEnd*limit
    indexStart <- indexEnd-limit
    l <- list(
      pages= pages,
      indexEnd=indexEnd,
      indexStart=indexStart
    )
    return(l)
  }  
  )
  
  return(output)
  
}


requestUrls <- function(urlList, url, type, endPoint, parms) {
  
  urls <- lapply( names(urlList),  function(x) {
    list(
      url=x,
      start=urlList[[x]]$indexStart,
      q=lapply(urlList[[x]]$indexStart, function(x)  list(start=x,end=x+500) )
    ) 
    
  }
  )
  
  df <- lapply(urls, function(x) 
    lapply(x$q, function(y) {
      GET( x$url, authenticate( usr,pw ), query=c(parms,y) )
    }
    
    ))
  
  
  ### COMBINE PAGINATED CALLS INTO ONE LIST
  
  condManyURL <- length(urls[[1]]) > 1
  condOneEnd <- length(unique(names(urlList))) == 1
  
  ## PARSE PAGINATED RESULTS DIFFERENTLY THAN MULTIPLE SEPARATE REQUESTS
  if (condManyURL==T & condOneEnd==T ) {
    
    ## MOVE LIST LEVEL UP ONE  
    df <- df[[1]]
    
    ## EXTRACT CONTENT FROM EACH RESPONSE
    df <- lapply(df, function(df) content(df)) 
    
    ### FOR RESULTS INTO ONE LIST FOR PAGINATED REQUESTS
    final <- list()
    i <- 0
    
    for(x in df) {
      for(y in x) {
        
        i <- i+1
        final[[i]] <- y
      }
    }
    
    df <- final
    
  } else {
    
    ## EXTRACT CONTENT FOR EACH NON-PAGINATED RESPONSE
    df <- lapply(df, function(df) df[[1]] )
    df <- lapply(df, function(df) content(df)) 
    df <- df
    names(df) <- url
    
  }  
  
  #if(type %in% c('opens','sends') ) {
  if( endPoint %in% c('response','mailings') & !is.na(type) ) {
    
    df <- lapply(df, function(x) lapply(x, function(z) lapply(z, function(y) {
    
      if(is.list(y)) {
        out <- jsonlite::toJSON(y)
        out <- as.character(out)
      } else {
        out <- y
      }

      return(out)
      
    })))
  
    ### replace nulls
    df <- lapply(df, function(x) lapply(x, function(z) lapply(z, function(y) {
     
       if(is.null(y)) {
         y <- NA
       } else {
         y <- y
       }
       
       return(y)
    }
    )
    ))
    
    df <- lapply(df, function(df) lapply(df, function(x) as.data.frame(x, stringsAsFacters=F) ))
    df <- lapply(df, function(df) do.call('rbind', df))
    
  } else {
    df <- df
  }
  
  
  return(df)
  
}
andrewgeisler/emmaR documentation built on May 10, 2019, 10:31 a.m.