From d3190d4b0d94581ea2b446c7a4f0ee472f52180a Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Wed, 27 Jan 2021 16:01:55 -0500 Subject: [PATCH] web service --- DESCRIPTION | 2 ++ NAMESPACE | 2 ++ R/h2c.R | 49 ++++++++++++++++++++++++++++++++++++------------- R/httr2curl-package.R | 2 +- README.Rmd | 18 ++++++++++++++++++ README.md | 29 +++++++++++++++++++++++++---- man/h2c.Rd | 5 ++++- 7 files changed, 88 insertions(+), 19 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 07c97fe..27a61b6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,6 +23,8 @@ Depends: Imports: httr, processx, + rvest, + xml2, magrittr Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 9e3cef1..f7c4010 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,4 +4,6 @@ export("%>%") export(h2c) import(httr) import(processx) +import(rvest) +import(xm2) importFrom(magrittr,"%>%") diff --git a/R/h2c.R b/R/h2c.R index da832b3..1389eb0 100644 --- a/R/h2c.R +++ b/R/h2c.R @@ -2,6 +2,8 @@ #' #' @param complete_httr_verb_call wrap an `httr` `VERB` call with this function #' and it will return the text of a working `curl` command line +#' @param use_web_service if `TRUE` (`FALSE` by default) this function will use +#' `https://curl.se/h2c/` to process the headers. #' @export #' @examples \dontrun{ #' h2c( @@ -15,14 +17,10 @@ #' ) #' ) #' } -h2c <- function(complete_httr_verb_call) { +h2c <- function(complete_httr_verb_call, use_web_service = FALSE) { ƒ_call <- substitute(complete_httr_verb_call) - perl <- find_perl() - - args <- system.file("bin", "h2c.pl", package = "httr2curl") - capture.output( capture.output( httr::with_verbose( @@ -34,15 +32,40 @@ h2c <- function(complete_httr_verb_call) { res[grepl("^->", res)] %>% sub("^-> ", "", .) %>% - paste0(collapse = "\n") %>% - writeLines(out) + paste0(collapse = "\n") -> res + + if (use_web_service) { + + httr::POST( + url = "https://curl.se/h2c/", + httr:::content_type_json(), + body = list( + http = res + ), + encode = "form" + ) -> res1 + + pg <- httr::content(res1) + + rvest::html_nodes(pg, xpath = ".//p[contains(., 'Command:')]/following-sibling::div[1]") %>% + rvest::html_text() %>% + trimws() + + } else { + + writeLines(res, out) + + perl <- find_perl() + + args <- system.file("bin", "h2c.pl", package = "httr2curl") - processx::run( - command = perl, - args = args, - stdin = out - ) -> res + processx::run( + command = perl, + args = args, + stdin = out + ) -> res - res$stdout + res$stdout + } } \ No newline at end of file diff --git a/R/httr2curl-package.R b/R/httr2curl-package.R index ab0d5a2..f4512dd 100644 --- a/R/httr2curl-package.R +++ b/R/httr2curl-package.R @@ -8,5 +8,5 @@ #' @name httr2curl #' @keywords internal #' @author Bob Rudis (bob@@rud.is) -#' @import httr processx +#' @import httr processx rvest xm2 "_PACKAGE" diff --git a/README.Rmd b/README.Rmd index 8ddf128..b0557b9 100644 --- a/README.Rmd +++ b/README.Rmd @@ -39,6 +39,8 @@ library(httr2curl) packageVersion("httr2curl") ``` +### Using local perl + ```{r ex01} h2c( httr::GET( @@ -52,6 +54,22 @@ h2c( ) ``` +### Using the web service + +```{r ex02} +h2c( + httr::GET( + url = "https://rud.is/", + httr::user_agent(splashr::ua_apple_tv), + query = list( + a = "b", + c = 1 + ) + ), + use_web_service = TRUE +) +``` + ## httr2curl Metrics ```{r cloc, echo=FALSE} diff --git a/README.md b/README.md index 1abe2da..6cbda4e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ packageVersion("httr2curl") ## [1] '0.1.0' ``` +### Using local perl + ``` r h2c( httr::GET( @@ -59,17 +61,36 @@ h2c( ) ) ) +## [1] "/var/folders/bx/41ph1lms22q9mc83j5tx4bg80000gn/T//RtmpFs8uvt/fileced55b4fcbba" ## [1] "curl --http2 --header \"Accept: application/json, text/xml, application/xml, */*\" --compressed --user-agent \"AppleTV6,2/11.1\" \"https://rud.is/?a=b&c=1\"\n" ``` +### Using the web service + +``` r +h2c( + httr::GET( + url = "https://rud.is/", + httr::user_agent(splashr::ua_apple_tv), + query = list( + a = "b", + c = 1 + ) + ), + use_web_service = TRUE +) +## [1] "/var/folders/bx/41ph1lms22q9mc83j5tx4bg80000gn/T//RtmpFs8uvt/fileced53c5bb06" +## NULL +``` + ## httr2curl Metrics | Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | |:-----|---------:|-----:|----:|-----:|------------:|-----:|---------:|-----:| -| R | 5 | 0.36 | 37 | 0.24 | 15 | 0.23 | 38 | 0.27 | -| YAML | 1 | 0.07 | 22 | 0.14 | 2 | 0.03 | 2 | 0.01 | -| Rmd | 1 | 0.07 | 18 | 0.12 | 16 | 0.24 | 31 | 0.22 | -| SUM | 7 | 0.50 | 77 | 0.50 | 33 | 0.50 | 71 | 0.50 | +| R | 5 | 0.36 | 52 | 0.25 | 21 | 0.25 | 40 | 0.26 | +| Rmd | 1 | 0.07 | 29 | 0.14 | 19 | 0.23 | 35 | 0.23 | +| YAML | 1 | 0.07 | 22 | 0.11 | 2 | 0.02 | 2 | 0.01 | +| SUM | 7 | 0.50 | 103 | 0.50 | 42 | 0.50 | 77 | 0.50 | clock Package Metrics for httr2curl diff --git a/man/h2c.Rd b/man/h2c.Rd index 5bf1ea2..f587251 100644 --- a/man/h2c.Rd +++ b/man/h2c.Rd @@ -4,11 +4,14 @@ \alias{h2c} \title{Convert an 'httr' call to 'curl' command line} \usage{ -h2c(complete_httr_verb_call) +h2c(complete_httr_verb_call, use_web_service = FALSE) } \arguments{ \item{complete_httr_verb_call}{wrap an \code{httr} \code{VERB} call with this function and it will return the text of a working \code{curl} command line} + +\item{use_web_service}{if \code{TRUE} (\code{FALSE} by default) this function will use +\verb{https://curl.se/h2c/} to process the headers.} } \description{ Convert an 'httr' call to 'curl' command line