-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* started httr2 integration work #237 * export RequestHandlerHttr2 * bmp version * import httr2 * remove --no-build-vignettes in R CMD INSTALL * add depends R >= 3.5 for data * add RequestHandlerHttr2 to pkgdown yaml * add req_error to httr2 handler so that httr2 doesnt turn a http errror status code into an R error * compatabiilty for httr2 here and there * udpate docs to talk about all 3 http pkgs * update readm.md * woops, errant debug line * tickle R check * update man file for Cassette class * httr2: get last response with last_response after req_perform * httr2: fix status code retrieval * httr2 response handling in Cassette class * uncomment commented out tests that now work * fix a bunch of httr2 tests * update roxygen2 version * REVDEPCHECK * REVDEPCHECK - use gh pat
- Loading branch information
Showing
22 changed files
with
628 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#' @title RequestHandlerHttr2 | ||
#' @description Methods for the httr2 package, building on [RequestHandler] | ||
#' @export | ||
#' @param request The request from an object of class `HttpInteraction` | ||
#' @examples \dontrun{ | ||
#' # GET request | ||
#' library(httr2) | ||
#' req <- request("https://hb.opencpu.org/post") %>% | ||
#' req_body_json(list(foo = "bar")) | ||
#' x <- RequestHandlerHttr2$new(req) | ||
#' # x$handle() | ||
#' | ||
#' # POST request | ||
#' library(httr2) | ||
#' mydir <- file.path(tempdir(), "testing_httr2") | ||
#' invisible(vcr_configure(dir = mydir)) | ||
#' req <- request("https://hb.opencpu.org/post") %>% | ||
#' req_body_json(list(foo = "bar")) | ||
#' use_cassette(name = "testing3", { | ||
#' response <- req_perform(req) | ||
#' }, match_requests_on = c("method", "uri", "body")) | ||
#' use_cassette(name = "testing3", { | ||
#' response2 <- req_perform(req) | ||
#' }, match_requests_on = c("method", "uri", "body")) | ||
#' } | ||
RequestHandlerHttr2 <- R6::R6Class( | ||
"RequestHandlerHttr2", | ||
inherit = RequestHandler, | ||
|
||
public = list( | ||
#' @description Create a new `RequestHandlerHttr2` object | ||
#' @param request The request from an object of class `HttpInteraction` | ||
#' @return A new `RequestHandlerHttr2` object | ||
initialize = function(request) { | ||
if (!length(request$method)) { | ||
request$method <- webmockr:::req_method_get_w(request) | ||
} | ||
self$request_original <- request | ||
self$request <- { | ||
Request$new(request$method, request$url, | ||
webmockr::pluck_body(request), request$headers, | ||
fields = request$fields, opts = request$options, | ||
policies = request$policies) | ||
} | ||
self$cassette <- tryCatch(current_cassette(), error = function(e) e) | ||
} | ||
), | ||
|
||
private = list( | ||
# make a `vcr` response | ||
response_for = function(x) { | ||
VcrResponse$new( | ||
list(status_code = x$status_code, description = httr2::resp_status_desc(x)), | ||
x$headers, | ||
x$body, | ||
"", | ||
super$cassette$cassette_opts | ||
) | ||
}, | ||
|
||
# these will replace those in | ||
on_ignored_request = function(request) { | ||
# perform and return REAL http response | ||
# * make real request | ||
# * run through response_for() to make vcr response, store vcr response | ||
# * give back real response | ||
|
||
# real request | ||
webmockr::httr2_mock(FALSE) | ||
on.exit(webmockr::httr2_mock(TRUE), add = TRUE) | ||
tmp2 <- httr2::req_perform(request) | ||
|
||
# run through response_for() | ||
self$vcr_response <- private$response_for(tmp2) | ||
|
||
# return real response | ||
return(response) | ||
}, | ||
|
||
on_stubbed_by_vcr_request = function(request) { | ||
# print("------- on_stubbed_by_vcr_request -------") | ||
# return stubbed vcr response - no real response to do | ||
serialize_to_httr2(request, super$get_stubbed_response(request)) | ||
}, | ||
|
||
on_recordable_request = function(request) { | ||
# print("------- on_recordable_request -------") | ||
# do real request - then stub response - then return stubbed vcr response | ||
# real request | ||
webmockr::httr2_mock(FALSE) | ||
on.exit(webmockr::httr2_mock(TRUE), add = TRUE) | ||
xx <- self$request_original %>% | ||
httr2::req_error(is_error = function(resp) FALSE) | ||
# print(xx) | ||
tryCatch(httr2::req_perform(xx), error = function(e) e) | ||
tmp2 <- httr2::last_response() | ||
# print("------- after the req_perform -------") | ||
|
||
response <- webmockr::build_httr2_response(self$request_original, tmp2) | ||
|
||
# make vcr response | then record interaction | ||
self$vcr_response <- private$response_for(response) | ||
cas <- tryCatch(current_cassette(), error = function(e) e) | ||
if (inherits(cas, "error")) stop("no cassette in use") | ||
response$request <- self$request_original | ||
response$request$method <- webmockr:::req_method_get_w(response$request) | ||
cas$record_http_interaction(response) | ||
|
||
# return real response | ||
# print("------- before return -------") | ||
return(response) | ||
} | ||
) | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# generate actual httr2 response | ||
# request <- httr2::request("https://hb.opencpu.org/301") | ||
# | ||
serialize_to_httr2 <- function(request, response) { | ||
# request | ||
req <- webmockr::RequestSignature$new( | ||
method = request$method, | ||
uri = request$uri, | ||
options = list( | ||
body = request$body %||% NULL, | ||
headers = request$headers %||% NULL, | ||
proxies = NULL, | ||
auth = NULL, | ||
disk = if (inherits(response$body, "httr2_path")) response$body %||% NULL, | ||
fields = request$fields %||% NULL, | ||
output = request$output %||% NULL | ||
) | ||
) | ||
|
||
# response | ||
resp <- webmockr::Response$new() | ||
resp$set_url(request$uri) | ||
resp$set_body(response$body, inherits(response$body, "httr2_path")) | ||
resp$set_request_headers(request$headers, capitalize = FALSE) | ||
resp$set_response_headers(response$headers, capitalize = FALSE) | ||
resp$set_status(status = response$status$status_code %||% 200) | ||
|
||
# generate httr2 response | ||
webmockr::build_httr2_response(as_httr2_request(req), resp) | ||
} | ||
|
||
as_httr2_request <- function(x) { | ||
structure(list(url = x$url$url, method = toupper(x$method), | ||
headers = x$headers, body = x$body, fields = x$fields, | ||
options = x$options, policies = x$policies), | ||
class = "httr2_request") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.