Browse Source

helpers

master
boB Rudis 3 years ago
parent
commit
e714a2d4e0
7 changed files with 61 additions and 6 deletions
  1. +2
    -0
      NAMESPACE
  2. +18
    -0
      R/helpers.r
  3. +22
    -6
      R/render-har.r
  4. +1
    -0
      README.Rmd
  5. +1
    -0
      README.md
  6. +14
    -0
      man/as_har.Rd
  7. +3
    -0
      man/get_content_type.Rd

+ 2
- 0
NAMESPACE View File

@@ -5,6 +5,7 @@ S3method(print,splash_json)
export("%>%")
export(HARviewer)
export(HARviewerOutput)
export(as_har)
export(execute_lua)
export(get_body_size)
export(get_content_size)
@@ -22,6 +23,7 @@ export(is_json)
export(is_plain)
export(is_png)
export(is_svg)
export(is_xhr)
export(is_xml)
export(renderHARviewer)
export(render_file)


+ 18
- 0
R/helpers.r View File

@@ -64,6 +64,24 @@ is_svg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/sv
#' @export
is_gif <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/gif") }

#' @rdname get_content_type
#' @export
is_xhr <- function(x) {

if (is.null(x$request$headers)) return(NA)
if (length(x$request$headers)==0) return(NA)

y <- map(x$request$headers, "value")
names(y) <- tolower(map_chr(x$request$headers, "name"))

if ("x-requested-with" %in% names(y)) {
return(tolower("xmlhttprequest") == tolower(y[["x-requested-with"]]))
} else {
return(FALSE)
}

}

#' Retrieve size of content | body | headers
#'
#' @param har_resp_obj HAR response object


+ 22
- 6
R/render-har.r View File

@@ -40,13 +40,28 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3
out <- httr::content(res, as="text", encoding="UTF-8")
spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)

as_har(spl)

}


#' Turn a generic Splash HAR response into a HAR object
#'
#' @param as_har splash response object as returned by `splash:har()` lua script
#' @export
as_har <- function(splash_resp) {

if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp),
flatten=FALSE,
simplifyVector=FALSE)

sphar <- list(
log=list(
version=spl$log$version,
creator=spl$log$creator,
browser=spl$log$browser,
pages=spl$log$pages,
entries=spl$log$entries
version=splash_resp$log$version,
creator=splash_resp$log$creator,
browser=splash_resp$log$browser,
pages=splash_resp$log$pages,
entries=splash_resp$log$entries
)
)

@@ -67,4 +82,5 @@ render_har <- function(splash_obj, url, base_url, response_body=FALSE, timeout=3

sphar

}

}

+ 1
- 0
README.Rmd View File

@@ -66,6 +66,7 @@ Helpers:
- `is_plain`: Retrieve or test content type of a HAR request object
- `is_png`: Retrieve or test content type of a HAR request object
- `is_svg`: Retrieve or test content type of a HAR request object
- `is_xhr`: Retrieve or test content type of a HAR request object
- `is_xml`: Retrieve or test content type of a HAR request object

Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.


+ 1
- 0
README.md View File

@@ -63,6 +63,7 @@ Helpers:
- `is_plain`: Retrieve or test content type of a HAR request object
- `is_png`: Retrieve or test content type of a HAR request object
- `is_svg`: Retrieve or test content type of a HAR request object
- `is_xhr`: Retrieve or test content type of a HAR request object
- `is_xml`: Retrieve or test content type of a HAR request object

Some functions from `HARtools` are imported/exported and `%>%` is imported/exported.


+ 14
- 0
man/as_har.Rd View File

@@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/render-har.r
\name{as_har}
\alias{as_har}
\title{Turn a generic Splash HAR response into a HAR object}
\usage{
as_har(splash_resp)
}
\arguments{
\item{as_har}{splash response object as returned by `splash:har()` lua script}
}
\description{
Turn a generic Splash HAR response into a HAR object
}

+ 3
- 0
man/get_content_type.Rd View File

@@ -14,6 +14,7 @@
\alias{is_png}
\alias{is_svg}
\alias{is_gif}
\alias{is_xhr}
\title{Retrieve or test content type of a HAR request object}
\usage{
get_content_type(har_resp_obj)
@@ -41,6 +42,8 @@ is_png(har_resp_obj)
is_svg(har_resp_obj)

is_gif(har_resp_obj)

is_xhr(x)
}
\arguments{
\item{har_resp_obj}{HAR response object}


Loading…
Cancel
Save