mirror of https://git.sr.ht/~hrbrmstr/splashr
boB Rudis
4 years ago
60 changed files with 1006 additions and 524 deletions
@ -1,62 +1,116 @@ |
|||
#' Turns a "HAR"-like object into a data frame(tibble) |
|||
.tidy_one_entry <- function(x, include_content = TRUE) { |
|||
|
|||
.x <- x |
|||
|
|||
if (length(.x[["timings"]])) { |
|||
data.frame( |
|||
stage = names(.x[["timings"]]), |
|||
value = unlist(.x[["timings"]], use.names = FALSE), |
|||
stringsAsFactors = FALSE |
|||
) -> timings |
|||
} else { |
|||
timings <- data.frame(stringsAsFactors = FALSE) |
|||
} |
|||
class(timings) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
|
|||
if (length(.x[["request"]][["headers"]])) { |
|||
data.frame( |
|||
name =vapply(.x[["request"]][["headers"]], `[[`, character(1), "name", USE.NAMES = FALSE), |
|||
value = vapply(.x[["request"]][["headers"]], `[[`, character(1), "value", USE.NAMES = FALSE), |
|||
stringsAsFactors = FALSE |
|||
) -> req_headers |
|||
} else { |
|||
req_headers <- data.frame(stringsAsFactors = FALSE) |
|||
} |
|||
class(req_headers) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
if (length(.x[["response"]][["headers"]])) { |
|||
data.frame( |
|||
name =vapply(.x[["response"]][["headers"]], `[[`, character(1), "name", USE.NAMES = FALSE), |
|||
value = vapply(.x[["response"]][["headers"]], `[[`, character(1), "value", USE.NAMES = FALSE), |
|||
stringsAsFactors = FALSE |
|||
) -> headers |
|||
} else { |
|||
headers <- data.frame(stringsAsFactors = FALSE) |
|||
} |
|||
class(headers) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
if (length(.x[["request"]][["cookies"]])) { |
|||
data.frame( |
|||
name =vapply(.x[["request"]][["cookies"]], `[[`, character(1), "name", USE.NAMES = FALSE), |
|||
value = vapply(.x[["request"]][["cookies"]], `[[`, character(1), "value", USE.NAMES = FALSE), |
|||
stringsAsFactors = FALSE |
|||
) -> req_cookies |
|||
} else { |
|||
req_cookies <- data.frame(stringsAsFactors = FALSE) |
|||
} |
|||
class(req_cookies) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
if (length(.x[["response"]][["cookies"]])) { |
|||
data.frame( |
|||
name =vapply(.x[["response"]][["cookies"]], `[[`, character(1), "name", USE.NAMES = FALSE), |
|||
value = vapply(.x[["response"]][["cookies"]], `[[`, character(1), "value", USE.NAMES = FALSE), |
|||
stringsAsFactors = FALSE |
|||
) -> res_cookies |
|||
} else { |
|||
res_cookies <- data.frame(stringsAsFactors = FALSE) |
|||
} |
|||
class(res_cookies) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
data.frame( |
|||
status = .x[["response"]][["status"]] %l0% NA_character_, |
|||
started = .x[["startedDateTime"]] %l0% NA_character_, |
|||
total_time = .x[["time"]] %l0% NA_integer_, |
|||
page_ref = .x[["pageref"]] %l0% NA_character_, |
|||
timings = I(list(timings)), |
|||
req_url = .x[["request"]][["url"]] %l0% NA_character_, |
|||
req_method = .x[["request"]][["method"]] %l0% NA_character_, |
|||
req_http_version = .x[["request"]][["httpVersion"]] %l0% NA_character_, |
|||
req_hdr_size = .x[["request"]][["headersSize"]] %l0% NA_character_, |
|||
req_headers = I(list(req_headers)), |
|||
req_cookies = I(list(req_cookies)), |
|||
resp_url = .x[["response"]][["url"]] %l0% NA_character_, |
|||
resp_rdrurl = .x[["response"]][["redirectURL"]] %l0% NA_character_, |
|||
resp_type = .x[["response"]][["content"]][["mimeType"]] %l0% NA_character_, |
|||
resp_size = .x[["resonse"]][["bodySize"]] %l0% NA_integer_, |
|||
resp_cookies = I(list(res_cookies)), |
|||
resp_headers = I(list(headers)), |
|||
resp_encoding = .x[["resonse"]][["content"]][["encoding"]] %l0% NA_character_, |
|||
resp_content_size = as.numeric(.x[["resonse"]][["content"]][["size"]]) %l0% NA_real_, |
|||
stringsAsFactors = FALSE |
|||
) -> out |
|||
|
|||
if (include_content) out$resp_content <- .x[["resonse"]][["content"]][["text"]] %l0% NA_character_ |
|||
|
|||
class(out) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
out |
|||
|
|||
} |
|||
|
|||
#' Turn a gnHARly HAR object into a tidy data frame (tibble) |
|||
#' |
|||
#' @md |
|||
#' @param x A `harentry` object |
|||
#' @param ... ignored |
|||
#' @param include_content if `TRUE` (the default) the encoded element content will be returned in the data frame |
|||
#' @return data frame (tibble) |
|||
#' @export |
|||
as_data_frame.harentry <- function(x, ...) { |
|||
|
|||
req <- x$request |
|||
resp <- x$response |
|||
|
|||
data_frame( |
|||
request_url = req$url, |
|||
request_method = req$method, |
|||
request_http_version = req$httpVersion, |
|||
request_query_string = list(req$queryString), |
|||
request_header_size = req$headersSize, |
|||
requestheaders = list(if (length(req$headers) > 0) flatten_df(req$headers) else data_frame()), |
|||
requestcookies = list(if (length(req$cookies) > 0) flatten_df(req$cookies) else data_frame()), |
|||
response_url = resp$url, |
|||
response_http_version = resp$httpVersion, |
|||
status_text = resp$statusText, |
|||
status = resp$status, |
|||
ok = resp$ok, |
|||
redirect_url = resp$redirectURL, |
|||
response_headers_size = resp$headersSize, |
|||
response_headers = list(if (length(resp$headers) > 0) flatten_df(resp$headers) else data_frame()), |
|||
response_cookies = list(if (length(resp$cookies) > 0) flatten_df(resp$cookies) else data_frame()), |
|||
response_body_size = resp$bodySize, |
|||
content_type = resp$content$mimeType, |
|||
content_encoding = resp$content$encoding %||% NA_character_, |
|||
content_size = resp$content$size, |
|||
content = resp$content$text %||% NA_character_ |
|||
) |
|||
|
|||
} |
|||
|
|||
#' @rdname as_data_frame.harentry |
|||
#' @export |
|||
as_data_frame.harentries <- function(x, ...) { |
|||
map_df(x, as_data_frame) |
|||
} |
|||
tidy_har <- function(.x, include_content = TRUE) { |
|||
|
|||
#' @rdname as_data_frame.harentry |
|||
#' @export |
|||
as_data_frame.har <- function(x, ...) { |
|||
as_data_frame(x$log$entries) |
|||
if (inherits(.x, "har")) { |
|||
out <- tidy_har(.x[["log"]][["entries"]], include_content = include_content) |
|||
} else if (inherits(.x, "harlog")) { |
|||
out <- tidy_har(.x[["entries"]], include_content = include_content) |
|||
} else if (inherits(.x, "harentries")) { |
|||
out <- do.call(rbind.data.frame, lapply(.x, .tidy_one_entry, include_content = include_content)) |
|||
class(out) <- c("tbl_df", "tbl", "data.frame") |
|||
} else if (inherits(.x, "harentry")) { |
|||
out <- .tidy_one_entry(.x, include_content = include_content) |
|||
} else { |
|||
stopifnot(inherits(.x, "harentries")) |
|||
} |
|||
|
|||
#' @export |
|||
#' @rdname as_data_frame.harentry |
|||
as.data.frame.har <- as_data_frame.har |
|||
|
|||
#' @export |
|||
#' @rdname as_data_frame.harentry |
|||
as.data.frame.harentries <- as_data_frame.harentries |
|||
|
|||
#' @export |
|||
#' @rdname as_data_frame.harentry |
|||
as.data.frame.harentry <- as_data_frame.harentry |
|||
out |
|||
|
|||
} |
|||
|
@ -0,0 +1,15 @@ |
|||
check_or_report_status <- function(res) { |
|||
|
|||
if (httr::status_code(res) >= 400) { |
|||
msg <- httr::content(res, as="text", encoding="UTF-8") |
|||
msg <- jsonlite::fromJSON(msg) |
|||
httr::stop_for_status( |
|||
x = res, |
|||
task = sprintf( |
|||
fmt = "render due to %s", |
|||
paste0(c(msg[["type"]], msg[["description"]], msg[["info"]]), collapse="; ") |
|||
) |
|||
) |
|||
} |
|||
|
|||
} |
@ -0,0 +1,4 @@ |
|||
`%l0%` <- function(x, y) if (length(x) == 0) y else x |
|||
`%||%` <- function(x, y) if (is.null(x)) y else x |
|||
`%@%` <- function(x, name) attr(x, name, exact = TRUE) |
|||
`%nin%` <- function(x, table) match(x, table, nomatch = 0) == 0 |
Before Width: | Height: | Size: 42 KiB After Width: | Height: | Size: 42 KiB |
Before Width: | Height: | Size: 521 KiB After Width: | Height: | Size: 590 KiB |
@ -1,34 +0,0 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/as-data-frame-har.R |
|||
\name{as_data_frame.harentry} |
|||
\alias{as_data_frame.harentry} |
|||
\alias{as_data_frame.harentries} |
|||
\alias{as_data_frame.har} |
|||
\alias{as.data.frame.har} |
|||
\alias{as.data.frame.harentries} |
|||
\alias{as.data.frame.harentry} |
|||
\title{Turns a "HAR"-like object into a data frame(tibble)} |
|||
\usage{ |
|||
as_data_frame.harentry(x, ...) |
|||
|
|||
as_data_frame.harentries(x, ...) |
|||
|
|||
as_data_frame.har(x, ...) |
|||
|
|||
\method{as.data.frame}{har}(x, ...) |
|||
|
|||
\method{as.data.frame}{harentries}(x, ...) |
|||
|
|||
\method{as.data.frame}{harentry}(x, ...) |
|||
} |
|||
\arguments{ |
|||
\item{x}{A \code{harentry} object} |
|||
|
|||
\item{...}{ignored} |
|||
} |
|||
\value{ |
|||
data frame (tibble) |
|||
} |
|||
\description{ |
|||
Turns a "HAR"-like object into a data frame(tibble) |
|||
} |
@ -0,0 +1,19 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/as-data-frame-har.R |
|||
\name{tidy_har} |
|||
\alias{tidy_har} |
|||
\title{Turn a gnHARly HAR object into a tidy data frame (tibble)} |
|||
\usage{ |
|||
tidy_har(.x, include_content = TRUE) |
|||
} |
|||
\arguments{ |
|||
\item{include_content}{if \code{TRUE} (the default) the encoded element content will be returned in the data frame} |
|||
|
|||
\item{x}{A \code{harentry} object} |
|||
} |
|||
\value{ |
|||
data frame (tibble) |
|||
} |
|||
\description{ |
|||
Turn a gnHARly HAR object into a tidy data frame (tibble) |
|||
} |
Loading…
Reference in new issue