boB Rudis 5 years ago
parent
commit
7042f06e55
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      .travis.yml
  2. 2
      NAMESPACE
  3. 9
      R/madhttr-package.R
  4. 74
      R/tidy-har.R
  5. 6
      README.Rmd
  6. 32
      README.md
  7. BIN
      inst/extdat/example-har.rds
  8. 28
      inst/tinytest/test_madhttr.R
  9. 18
      man/tidy_har.Rd
  10. 16
      man/write_har.Rd

2
.travis.yml

@ -3,4 +3,4 @@ sudo: false
cache: packages
after_success:
- Rscript -e 'covr::codecov()'
- Rscript -e 'covr::codecov()'

2
NAMESPACE

@ -8,7 +8,9 @@ export(download_ssl_cert)
export(nslookup)
export(read_har)
export(tidy_cert)
export(tidy_har)
export(tidy_response)
export(write_har)
import(httr)
importFrom(curl,nslookup)
importFrom(magrittr,"%>%")

9
R/madhttr-package.R

@ -36,6 +36,15 @@ NULL
#' @export
read_har <- HARtools::readHAR
#' Write HAR objects
#'
#' Snake-case re-exported alias for [HARtools::writeHAR()].
#'
#' @param har a string, list/URL or a file containing JSON HAR data
#' @param file,force,... see [HARtools::writeHAR()]
#' @export
write_har <- HARtools::writeHAR
#' @name nslookup
#' @title Lookup a hostname
#' @param host,ipv4_only,multiple,error see [curl::nslookup()]

74
R/tidy-har.R

@ -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
}

6
README.Rmd

@ -62,6 +62,12 @@ tidy_cert(download_ssl_cert("r-project.org"))
tidy_response(GET("https://rud.is/b"))
```
### HARdy
```{r hardy}
tidy_har(readRDS(system.file("extdat", "example-har.rds", package = "madhttr")))
```
## madhttr Metrics
```{r cloc, echo=FALSE}

32
README.md

@ -30,7 +30,9 @@ The following functions are implemented:
- `read_har`: Read HAR objects
- `tidy_cert`: Turn an openssl downloaded SSL certificate into a tidy
data frame
- `tidy_har`: Tidy HAR entries
- `tidy_response`: Turn an httr response object into a tidy data frame
- `write_har`: Write HAR objects
## Installation
@ -79,15 +81,35 @@ tidy_response(GET("https://rud.is/b"))
## # A tibble: 1 x 7
## url status_code date headers cookies content times
## <chr> <int> <dttm> <I<list>> <I<list>> <I<list>> <I<list>>
## 1 https://rud.is/b/ 200 2019-07-08 18:35:49 <list [2]> <tibble [1 × 7]> < [60,585]> <tibble [6 × 2]>
## 1 https://rud.is/b/ 200 2019-07-08 20:15:36 <list [2]> <tibble [1 × 7]> < [60,585]> <tibble [6 × 2]>
```
### HARdy
``` r
tidy_har(readRDS(system.file("extdat", "example-har.rds", package = "madhttr")))
## # A tibble: 88 x 11
## started total_time page_ref timings req_url resp_url resp_rdrurl resp_type resp_size status headers
## <chr> <int> <chr> <I<list> <chr> <chr> <chr> <chr> <int> <int> <I<lis>
## 1 2019-07-… 352 1 <tibble https://rud.i https://rud.is "" text/html NA 200 <tibbl
## 2 2019-07-… 35 1 <tibble https://rud.i https://rud.is "" applicati NA 200 <tibbl
## 3 2019-07-… 41 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 4 2019-07-… 62 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 5 2019-07-… 82 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 6 2019-07-… 91 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 7 2019-07-… 86 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 8 2019-07-… 93 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 9 2019-07-… 109 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## 10 2019-07-… 112 1 <tibble https://rud.i https://rud.is "" text/css NA 200 <tibbl
## # … with 78 more rows
```
## madhttr Metrics
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| R | 7 | 0.88 | 149 | 0.93 | 43 | 0.66 | 112 | 0.74 |
| Rmd | 1 | 0.12 | 12 | 0.07 | 22 | 0.34 | 40 | 0.26 |
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | --: | -------: | ---: |
| R | 8 | 0.89 | 204 | 0.94 | 55 | 0.7 | 128 | 0.75 |
| Rmd | 1 | 0.11 | 13 | 0.06 | 24 | 0.3 | 43 | 0.25 |
## Code of Conduct

BIN
inst/extdat/example-har.rds

Binary file not shown.

28
inst/tinytest/test_madhttr.R

@ -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"
))
)

18
man/tidy_har.Rd

@ -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")))
}

16
man/write_har.Rd

@ -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…
Cancel
Save