#### Internal ####
#' Return response from a SODA request
#'
#' Return response from a SODA request
#'
#' @import httr
#' @export
#' @param request a SODA request
#' @param ... dot-dot-dot, ignore
response.soda <- function(request, ...){
user <- attributes(request)$user
password <- attributes(request)$password
httr::GET(httr::build_url(request),
`if`(is.null(user), NULL, httr::authenticate(user = user, password = password))
)
}
#' Return content of response from a SODA request
#'
#' @import httr
#' @export
#' @param request a SODA request
#' @param ... dot-dot-dot, ignored
response_content.soda <- function(request, ...){
httr::content(response.soda(request), "text")
}
#' Set Limit and Offset of a SODA request
#'
#' @param request a SODA request
#' @param limit limit to the rows
#' @param offset offset of the rows from the beginning
#' @param ... dot-dot-dot, ignored
#' @noRd
set_query_limit_offset <- function(request, limit, offset = 0, ...){
if (has_plain_query(request)) {
request$query$`$query` <- paste(request$query$`$query`, "LIMIT ", limit)
request$query$`$query` <- paste(request$query$`$query`, "OFFSET ", offset)
} else {
request$query$`$limit` <- limit
request$query$`$offset` <- offset
}
request
}
#' Get SODA data types of an an asset
#'
#' @import jsonlite
#' @param request a SODA request
#' @param ... dot-dot-dot, ignored
#' @export
soda_data_types <- function(request, ...){
request <- set_query_limit_offset(request, limit = 1)
jsonlite::fromJSON(response.soda(request)$headers$`x-soda2-types`)
}
#' Retrieve and parse a SODA response as data frame
#'
#' @import jsonlite readr
#' @export
#' @param x a SODA request
#' @param ... dot-dot-dot, ignored
as_data_frame.soda <- function(x, guess_type = TRUE, ...){
request <- x
res <- as.data.frame.list(jsonlite::fromJSON(response_content.soda(request)), stringsAsFactors = FALSE)
res
}
#' Check if request uses plain SoQL query string
#'
#' A plain query is contrasted with parsed query generated by tidyish verbs, and will be placed in request$query$`query`
#'
#' @noRd
has_plain_query <- function(request){
!is.null(request$query$`$query`)
}
#' If missing, add HTTP protocol to a URL
#'
#' @noRd
add_protocol <- function(url){
if (!grepl("^http", url)) {url <- paste0("http://", url)}
url
}
#' If has .json in endpoint, remove
#'
#' @noRd
trim_tail <- function(url){
if (grepl("\\.json$", url)) {url <- gsub("\\.json$", "", url)}
url
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.