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 Admin SDK API
#'
#' 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 'https://admin.googleapis.com'.
#' @param batch_path Defaults to 'batch/admin/directory/v1'. If you get a 404 error, you will need to consult the \href{https://developers.google.com/admin-sdk/}{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.admin.directory.v1 <- function(requests, content_ids, gargle_token = gargle::token_fetch(), base_url = 'https://admin.googleapis.com', batch_path = 'batch/admin/directory/v1', 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.