Main R6 class that is called from the main user facing
function use_cassette()
an object of class Cassette
initialize()
: webmockr is used in the initialize()
method to
create webmockr stubs. stubs are created on call to Cassette$new()
within insert_cassette()
, but then on exiting use_cassette()
,
or calling eject()
on Cassette
class from insert_cassette()
,
stubs are cleaned up.
eject()
method: webmockr::disable()
is called before exiting
eject to disable webmock so that webmockr does not affect any HTTP
requests that happen afterwards
call_block()
method: call_block is used in the use_cassette()
function to evaluate whatever code is passed to it; within call_block
webmockr::webmockr_allow_net_connect()
is run before we evaluate
the code block to allow real HTTP requests, then
webmockr::webmockr_disable_net_connect()
is called after evalulating
the code block to disallow real HTTP requests
make_http_interaction()
method: webmockr::pluck_body()
utility
function is used to pull the request body out of the HTTP request
serialize_to_crul()
method: method: webmockr::RequestSignature and
webmockr::Response are used to build a request and response,
respectively, then passed to webmockr::build_crul_response()
to make a complete crul
HTTP response object
name
(character) cassette name
record
(character) record mode
manfile
(character) cassette file path
recorded_at
(character) date/time recorded at
serialize_with
(character) serializer to use (yaml|json)
serializer
(character) serializer to use (yaml|json)
persist_with
(character) persister to use (FileSystem only)
persister
(character) persister to use (FileSystem only)
match_requests_on
(character) matchers to use default: method & uri
re_record_interval
(numeric) the re-record interval
tag
ignored, not used right now
tags
ignored, not used right now
root_dir
root dir, gathered from vcr_configuration()
update_content_length_header
(logical) Whether to overwrite the
Content-Length
header
allow_playback_repeats
(logical) Whether to allow a single HTTP interaction to be played back multiple times
allow_unused_http_interactions
(logical) ignored, not used right now
exclusive
(logical) ignored, not used right now
preserve_exact_body_bytes
(logical) Whether to base64 encode the bytes of the requests and responses
args
(list) internal use
http_interactions_
(list) internal use
new_recorded_interactions
(list) internal use
clean_outdated_http_interactions
(logical) Should outdated interactions be recorded back to file
to_return
(logical) internal use
cassette_opts
(list) various cassette options
new()
Create a new Cassette
object
Cassette$new(
name,
record,
serialize_with,
persist_with,
match_requests_on,
re_record_interval,
tag,
tags,
update_content_length_header,
allow_playback_repeats,
allow_unused_http_interactions,
exclusive,
preserve_exact_body_bytes,
clean_outdated_http_interactions
)
name
The name of the cassette. vcr will sanitize this to ensure it is a valid file name.
record
The record mode. Default: "once". In the future we'll support "once", "all", "none", "new_episodes". See recording for more information
serialize_with
(character) Which serializer to use. Valid values are "yaml" (default), the only one supported for now.
persist_with
(character) Which cassette persister to use. Default: "file_system". You can also register and use a custom persister.
match_requests_on
List of request matchers
to use to determine what recorded HTTP interaction to replay. Defaults to
["method", "uri"]
. The built-in matchers are "method", "uri",
"headers" and "body" ("host" and "path" not supported yet, but should
be in a future version)
re_record_interval
(numeric) When given, the cassette will be re-recorded at the given interval, in seconds.
tag, tags
tags ignored, not used right now
update_content_length_header
(logical) Whether or
not to overwrite the Content-Length
header of the responses to
match the length of the response body. Default: FALSE
allow_playback_repeats
(logical) Whether or not to
allow a single HTTP interaction to be played back multiple times.
Default: FALSE
.
allow_unused_http_interactions
(logical) ignored, not used right now
exclusive
(logical) ignored, not used right now
preserve_exact_body_bytes
(logical) Whether or not
to base64 encode the bytes of the requests and responses for
this cassette when serializing it. See also preserve_exact_body_bytes
in vcr_configure()
. Default: FALSE
clean_outdated_http_interactions
(logical) Should outdated interactions
be recorded back to file. Default: FALSE
A new Cassette
object
x
self
...
ignored
call_block()
run code
Cassette$call_block(...)
...
pass in things to be evaluated
various
eject()
ejects the current cassette
Cassette$eject()
self
character
recording()
is the cassette in recording mode?
Cassette$recording()
logical
is_empty()
is the cassette on disk empty
Cassette$is_empty()
logical
originally_recorded_at()
timestamp the cassette was originally recorded at
Cassette$originally_recorded_at()
POSIXct date
serializable_hash()
Get a list of the http interactions to record + recorded_with
Cassette$serializable_hash()
list
interactions_to_record()
Get the list of http interactions to record
Cassette$interactions_to_record()
list
merged_interactions()
Get interactions to record
Cassette$merged_interactions()
list
up_to_date_interactions()
Cleans out any old interactions based on the re_record_interval and clean_outdated_http_interactions settings
Cassette$up_to_date_interactions(interactions)
interactions
list of http interactions, of class HTTPInteraction
list of interactions to record
should_re_record()
Should re-record interactions?
Cassette$should_re_record()
logical
should_stub_requests()
Is record mode NOT "all"?
Cassette$should_stub_requests()
logical
should_remove_matching_existing_interactions()
Is record mode "all"?
Cassette$should_remove_matching_existing_interactions()
logical
storage_key()
Get the serializer path
Cassette$storage_key()
character
raw_cassette_bytes()
Get character string of entire cassette; bytes is a misnomer
Cassette$raw_cassette_bytes()
character
make_dir()
Create the directory that holds the cassettes, if not present
Cassette$make_dir()
no return; creates a directory recursively, if missing
deserialized_hash()
get http interactions from the cassette via the serializer
Cassette$deserialized_hash()
list
previously_recorded_interactions()
get all previously recorded interactions
Cassette$previously_recorded_interactions()
list
write_recorded_interactions_to_disk()
write recorded interactions to disk
Cassette$write_recorded_interactions_to_disk()
nothing returned
record_http_interaction()
record an http interaction (doesn't write to disk)
Cassette$record_http_interaction(x)
x
a crul, httr, or httr2 response object, with the request at $request
nothing returned
any_new_recorded_interactions()
Are there any new recorded interactions?
Cassette$any_new_recorded_interactions()
logical
make_args()
make list of all options
Cassette$make_args()
nothing returned
write_metadata()
write metadata to the cassette
Cassette$write_metadata()
nothing returned
http_interactions()
make HTTPInteractionList object, assign to http_interactions_ var
Cassette$http_interactions()
nothing returned
make_http_interaction()
Make an HTTPInteraction
object
Cassette$make_http_interaction(x)
x
A crul, httr, or httr2 response object, with the request at $request
an object of class HTTPInteraction
serialize_to_crul()
Make a crul response object
Cassette$serialize_to_crul()
a crul response
clone()
The objects of this class are cloneable with this method.
Cassette$clone(deep = FALSE)
deep
Whether to make a deep clone.
vcr_configure()
, use_cassette()
, insert_cassette()
if (FALSE) {
library(vcr)
vcr_configure(dir = tempdir())
res <- Cassette$new(name = "bob")
res$file()
res$originally_recorded_at()
res$recording()
res$serializable_hash()
res$eject()
res$should_remove_matching_existing_interactions()
res$storage_key()
res$match_requests_on
# record all requests
res <- Cassette$new("foobar", record = "all")
res$eject()
# cleanup
unlink(file.path(tempdir(), c("bob.yml", "foobar.yml")))
library(vcr)
vcr_configure(dir = tempdir())
res <- Cassette$new(name = "jane")
library(crul)
# HttpClient$new("https://hb.opencpu.org")$get("get")
}
Run the code above in your browser using DataLab