-
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.
Merge pull request #79 from ropensci/httr-integration
httr integration done
- Loading branch information
Showing
12 changed files
with
283 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#' RequestHandlerHttr - methods for httr package | ||
#' @export | ||
#' @inherit RequestHandler | ||
#' @examples \dontrun{ | ||
#' vcr_configure( | ||
#' dir = tempdir(), | ||
#' record = "once" | ||
#' ) | ||
#' | ||
#' library(httr) | ||
#' load("~/httr_req.rda") | ||
#' req | ||
#' x <- RequestHandlerHttr$new(req) | ||
#' # x$handle() | ||
#' } | ||
RequestHandlerHttr <- R6::R6Class( | ||
'RequestHandlerHttr', | ||
inherit = RequestHandler, | ||
|
||
public = list( | ||
initialize = function(request) { | ||
self$request_original <- request | ||
self$request <- { | ||
Request$new(request$method, request$url, | ||
request$body, request$headers) | ||
} | ||
self$cassette <- tryCatch(current_cassette(), error = function(e) e) | ||
} | ||
), | ||
|
||
private = list( | ||
# make a `vcr` response | ||
response_for = function(x) { | ||
VcrResponse$new( | ||
httr::http_status(x), | ||
x$headers, | ||
httr::content(x, encoding = "UTF-8"), | ||
x$all_headers[[1]]$version, | ||
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 | ||
response <- eval(parse(text = paste0("httr::", request$method)))(request$url) | ||
|
||
# run through response_for() | ||
self$vcr_response <- private$response_for(response) | ||
|
||
# return real response | ||
return(response) | ||
}, | ||
|
||
on_stubbed_by_vcr_request = function(request) { | ||
# return stubbed vcr response - no real response to do | ||
serialize_to_httr(request, super$get_stubbed_response(request)) | ||
}, | ||
|
||
on_recordable_request = function(request) { | ||
# do real request - then stub response - then return stubbed vcr response | ||
# - this may need to be called from webmockr httradapter? | ||
|
||
# real request | ||
webmockr::httr_mock(FALSE) | ||
tmp2 <- eval(parse(text = paste0("httr::", self$request_original$method)))(self$request_original$url) | ||
response <- webmockr::build_httr_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") | ||
cas$record_http_interaction(response) | ||
|
||
# return real response | ||
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
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,26 @@ | ||
# generate actual httr response | ||
serialize_to_httr <- 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 | ||
) | ||
) | ||
|
||
# response | ||
resp <- webmockr::Response$new() | ||
resp$set_url(request$uri) | ||
bod <- response$body | ||
resp$set_body(if ("string" %in% names(bod)) bod$string else bod) | ||
resp$set_request_headers(request$headers, capitalize = FALSE) | ||
resp$set_response_headers(response$headers, capitalize = FALSE) | ||
resp$set_status(status = response$status %||% 200) | ||
|
||
# generate httr response | ||
webmockr::build_httr_response(req, resp) | ||
} |
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.