Browse Source

web service

master
boB Rudis 2 months ago
parent
commit
d3190d4b0d
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
7 changed files with 88 additions and 19 deletions
  1. +2
    -0
      DESCRIPTION
  2. +2
    -0
      NAMESPACE
  3. +36
    -13
      R/h2c.R
  4. +1
    -1
      R/httr2curl-package.R
  5. +18
    -0
      README.Rmd
  6. +25
    -4
      README.md
  7. +4
    -1
      man/h2c.Rd

+ 2
- 0
DESCRIPTION View File

@ -23,6 +23,8 @@ Depends:
Imports:
httr,
processx,
rvest,
xml2,
magrittr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1

+ 2
- 0
NAMESPACE View File

@ -4,4 +4,6 @@ export("%>%")
export(h2c)
import(httr)
import(processx)
import(rvest)
import(xm2)
importFrom(magrittr,"%>%")

+ 36
- 13
R/h2c.R View File

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

+ 1
- 1
R/httr2curl-package.R View File

@ -8,5 +8,5 @@
#' @name httr2curl
#' @keywords internal
#' @author Bob Rudis (bob@@rud.is)
#' @import httr processx
#' @import httr processx rvest xm2
"_PACKAGE"

+ 18
- 0
README.Rmd View File

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


+ 25
- 4
README.md View File

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


+ 4
- 1
man/h2c.Rd View File

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


Loading…
Cancel
Save