boB Rudis
5 years ago
10 changed files with 179 additions and 8 deletions
@ -0,0 +1,74 @@ |
|||
.tidy_one_entry <- function(.x) { |
|||
|
|||
stopifnot(inherits(.x, "harentry")) |
|||
|
|||
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[["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") |
|||
|
|||
data.frame( |
|||
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_, |
|||
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_, |
|||
status = .x[["response"]][["status"]] %l0% NA_character_, |
|||
headers = I(list(headers)), |
|||
stringsAsFactors = FALSE |
|||
) -> out |
|||
|
|||
class(out) <- c("tbl_df", "tbl", "data.frame") |
|||
|
|||
out |
|||
|
|||
} |
|||
|
|||
#' Tidy HAR entries |
|||
#' |
|||
#' Given a single `harentry` or a `harentries` list/`harlog`/`har`, return a data frame |
|||
#' with selected columns. |
|||
#' |
|||
#' @param a `harentry` or `harentries`/`harlog`/`har` object |
|||
#' @export |
|||
#' @examples |
|||
#' tidy_har(readRDS(system.file("extdat", "example-har.rds", package = "madhttr"))) |
|||
tidy_har <- function(.x) { |
|||
|
|||
if (inherits(.x, "har")) { |
|||
out <- tidy_har(.x[["log"]][["entries"]]) |
|||
} else if (inherits(.x, "harlog")) { |
|||
out <- tidy_har(.x[["entries"]]) |
|||
} else if (inherits(.x, "harentries")) { |
|||
out <- do.call(rbind.data.frame, lapply(.x, .tidy_one_entry)) |
|||
class(out) <- c("tbl_df", "tbl", "data.frame") |
|||
} else if (inherits(.x, "harentry")) { |
|||
out <- .tidy_one_entry(.x) |
|||
} else { |
|||
stopifnot(inherits(.x, "harentries")) |
|||
} |
|||
|
|||
out |
|||
|
|||
} |
Binary file not shown.
@ -1,4 +1,28 @@ |
|||
library(madhttr) |
|||
|
|||
# Placeholder with simple test |
|||
expect_equal(1 + 1, 2) |
|||
x1 <- tidy_cert(download_ssl_cert("r-project.org")) |
|||
expect_true(inherits(x1, "data.frame")) |
|||
expect_true(nrow(x1) == 4) |
|||
expect_true( |
|||
all(colnames(x1) %in% c( |
|||
"subject", "issuer", "algorithm", "signature", "valid_start", |
|||
"valid_end", "self_signed", "alt_names", "pub_key") |
|||
) |
|||
) |
|||
|
|||
x2 <- tidy_response(GET("https://rud.is/b")) |
|||
expect_true(inherits(x2, "data.frame")) |
|||
expect_true(nrow(x2) == 1) |
|||
expect_true( |
|||
all(colnames(x2) %in% c("url", "status_code", "date", "headers", "cookies", "content", "times")) |
|||
) |
|||
|
|||
x3 <- tidy_har(readRDS(system.file("extdat", "example-har.rds", package = "madhttr"))) |
|||
expect_true(inherits(x3, "data.frame")) |
|||
expect_true(nrow(x3) == 88) |
|||
expect_true( |
|||
all(colnames(x3) %in% c( |
|||
"started", "total_time", "page_ref", "timings", "req_url", "resp_url", |
|||
"resp_rdrurl", "resp_type", "resp_size", "status", "headers" |
|||
)) |
|||
) |
|||
|
@ -0,0 +1,18 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/tidy-har.R |
|||
\name{tidy_har} |
|||
\alias{tidy_har} |
|||
\title{Tidy HAR entries} |
|||
\usage{ |
|||
tidy_har(.x) |
|||
} |
|||
\arguments{ |
|||
\item{a}{\code{harentry} or \code{harentries}/\code{harlog}/\code{har} object} |
|||
} |
|||
\description{ |
|||
Given a single \code{harentry} or a \code{harentries} list/\code{harlog}/\code{har}, return a data frame |
|||
with selected columns. |
|||
} |
|||
\examples{ |
|||
tidy_har(readRDS(system.file("extdat", "example-har.rds", package = "madhttr"))) |
|||
} |
@ -0,0 +1,16 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/madhttr-package.R |
|||
\name{write_har} |
|||
\alias{write_har} |
|||
\title{Write HAR objects} |
|||
\usage{ |
|||
write_har(har, file, force = TRUE, ...) |
|||
} |
|||
\arguments{ |
|||
\item{har}{a string, list/URL or a file containing JSON HAR data} |
|||
|
|||
\item{file, force, ...}{see \code{\link[HARtools:writeHAR]{HARtools::writeHAR()}}} |
|||
} |
|||
\description{ |
|||
Snake-case re-exported alias for \code{\link[HARtools:writeHAR]{HARtools::writeHAR()}}. |
|||
} |
Loading…
Reference in new issue