inst/templates/make_batch_request.R

"
process_batch_response <- function(response){{
   
   responses <-
      httr::content(response, as = 'text') %>% 
      stringr::str_split('Content-Type: application/http') %>% 
      purrr::pluck(1) %>% 
      stringr::str_subset('\r\nContent-ID: response-') %>% 
      stringr::str_split('\r\n\r\n') %>% 
      purrr::map(~ .x %>% stringr::str_remove_all('[\r \n]'))
   
   content_ids <-
      responses %>%
      purrr::map_chr(~ .x %>% stringr::str_subset('Content-ID'))
   
   contents <-
      responses %>%
      purrr::map(~ .x %>% stringr::str_subset(stringr::fixed('{{'))) %>% 
      stringr::str_remove_all('--batch.*') %>%
      purrr::map(~ .x %>% jsonlite::fromJSON()) %>%
      setNames(content_ids %>% stringr::str_remove('response-'))
   
   contents
}}

#' Makes a Batch API Request to the {api_name}
#'
#' Autogenerated via \code{{\link[googlePackageMaker]{{package_make}}}}.
#'
#' @seealso \href{{https://developers.google.com/gmail/api/guides/batch#details}}{{Sample Gmail Batch Request Info}}
#'
#' @details The server's response is a single standard HTTP response with a multipart/mixed content type; each part is the response to one of the requests in the batched request, in the same order as the requests.
#' @param requests A list of requests to the same endpoint based on generated functions from this package. Set return_request = TRUE for the function calls.
#' @param content_ids A list of the same length as requests with identifiers for the data returned. These will be the names of the list of results when setting return_response = FALSE.
#' @param gargle_token A token prepared by one of gargle's token generating functions. Defaults to gargle::token_fetch(...) with appropriate scopes. See \code{{\link[gargle]{{token_fetch}}}} for more info.
#' @param base_url The base URL for the API. Defaults to '{base_url %>% stringr::str_remove('/$')}'.
#' @param batch_path Defaults to '{batch_path}'. If you get a 404 error, you will need to consult the \href{{{api_info$documentationLink}}{{API documentation}} to find the correct batch path.
#' @param batch_delim A string used to separate requests in the batch request body. Defaults to 'gpm_batch_delimiter'.
#' @param return_response Whether to return the response or the response content. Defaults to FALSE (return response content).
#' @export
{batch_function_name} <- function(requests, content_ids, gargle_token = gargle::token_fetch(), base_url = '{base_url %>% stringr::str_remove('/$')}', batch_path = '{batch_path}', batch_delim = 'gpm_batch_delimiter',  return_response = F){{
     
     if(!is.null(names(requests))){{
          requests <- list(requests)
     }}
     
     batch_request <-
          purrr::map2_chr(.x = requests, 
                          .y = content_ids,
                          .f =  ~ glue::glue('--{{batch_delim}}', 
                                             'Content-Type: application/http', 
                                             'Content-ID: {{.y}}', 
                                             '', 
                                             paste(.x$method, .x$url %>% stringr::str_remove(base_url)), 
                                             ifelse(length(.x$body) == 0, 
                                                    '', 
                                                    .x$body %>% jsonlite::toJSON(pretty = T)), 
                                             '', 
                                             .sep = '\n')) %>% 
          append(glue::glue('--{{batch_delim}}--')) %>% 
          paste(collapse = '\n')
     
     response <-
          httr::POST(url = glue::glue('{{base_url}}/{{batch_path}}'), 
                     body = batch_request, 
                     encode = 'multipart', 
                     httr::add_headers(
                          Authorization = 
                               paste('Bearer', gargle_token$credentials$access_token)), 
                     httr::add_headers(`Accept-Encoding` = 'gzip'), 
                     httr::add_headers(`Content-Type` = glue::glue('multipart/mixed; boundary={{batch_delim}}'))
          )
     
     if(return_response) return(response)
     
     process_batch_response(response)
}}
"
samterfa/googlePackageMaker documentation built on May 18, 2022, 10:58 a.m.