diff --git a/DESCRIPTION b/DESCRIPTION index 5294c07..ce1e7b2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,6 +33,7 @@ Imports: xml2, curl, httr, + dplyr, purrr, stats, utils, @@ -42,8 +43,8 @@ Imports: formatR, openssl, stringi, - jsonlite, HARtools, + jsonlite, lubridate RoxygenNote: 6.0.1.9000 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 935d9d2..67fb0ca 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(as.data.frame,har) +S3method(as.data.frame,harentries) +S3method(as.data.frame,harentry) +S3method(as_data_frame,har) +S3method(as_data_frame,harentries) +S3method(as_data_frame,harentry) S3method(print,splash_debug) S3method(print,splash_json) export("%>%") @@ -93,6 +99,8 @@ importFrom(HARtools,HARviewerOutput) importFrom(HARtools,renderHARviewer) importFrom(HARtools,writeHAR) importFrom(curl,curl_unescape) +importFrom(dplyr,as_data_frame) +importFrom(dplyr,data_frame) importFrom(formatR,tidy_source) importFrom(jsonlite,fromJSON) importFrom(jsonlite,stream_in) diff --git a/NEWS.md b/NEWS.md index 7996235..5aa8b0b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ 0.5.0 * support Splash API basic auth +* `as_data_frame`/`as.data.frame` methods for HAR objects 0.4.1 diff --git a/R/as-data-frame-har.R b/R/as-data-frame-har.R new file mode 100644 index 0000000..cc44741 --- /dev/null +++ b/R/as-data-frame-har.R @@ -0,0 +1,65 @@ +#' Turns a "HAR"-like object into a data frame(tibble) +#' +#' @md +#' @param harentry_obj A `harentry` object +#' @return data frame (tibble) +#' @export +as_data_frame.harentry <- function(harentry_obj) { + + req <- harentry_obj$request + resp <- harentry_obj$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_ + ) + +} + +#' @md +#' @param harentries_obj A `harentry` object +#' @rdname as_data_frame.harentry +#' @export +as_data_frame.harentries <- function(harentries_obj) { + map_df(harentries_obj, as_data_frame) +} + +#' @md +#' @param har_obj A `har` object +#' @rdname as_data_frame.harentry +#' @export +as_data_frame.har <- function(har_obj) { + as_data_frame(har_obj$log$entries) +} + +#' @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 + diff --git a/R/splashr-package.R b/R/splashr-package.R index d4dbb99..1d6bea8 100644 --- a/R/splashr-package.R +++ b/R/splashr-package.R @@ -26,6 +26,7 @@ #' @importFrom formatR tidy_source #' @importFrom utils capture.output str #' @importFrom curl curl_unescape +#' @importFrom dplyr data_frame as_data_frame NULL #' splashr exported operators diff --git a/man/as_data_frame.harentry.Rd b/man/as_data_frame.harentry.Rd new file mode 100644 index 0000000..2ad3155 --- /dev/null +++ b/man/as_data_frame.harentry.Rd @@ -0,0 +1,36 @@ +% 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{ +\method{as_data_frame}{harentry}(harentry_obj) + +\method{as_data_frame}{harentries}(harentries_obj) + +\method{as_data_frame}{har}(har_obj) + +\method{as.data.frame}{har}(har_obj) + +\method{as.data.frame}{harentries}(harentries_obj) + +\method{as.data.frame}{harentry}(harentry_obj) +} +\arguments{ +\item{harentry_obj}{A \code{harentry} object} + +\item{harentries_obj}{A \code{harentry} object} + +\item{har_obj}{A \code{har} object} +} +\value{ +data frame (tibble) +} +\description{ +Turns a "HAR"-like object into a data frame(tibble) +}