Cassette: Cassette handler

CassetteR Documentation

Cassette handler

Description

Main R6 class that is called from the main user facing function use_cassette()

Value

an object of class Cassette

Points of webmockr integration

  • 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

Public fields

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

Methods

Public methods


Method new()

Create a new Cassette object

Usage
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
)
Arguments
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

Returns

A new Cassette object


Method print()

print method for Cassette objects

Usage
Cassette$print(x, ...)
Arguments
x

self

...

ignored


Method call_block()

run code

Usage
Cassette$call_block(...)
Arguments
...

pass in things to be evaluated

Returns

various


Method eject()

ejects the current cassette

Usage
Cassette$eject()
Returns

self


Method file()

get the file path for the cassette

Usage
Cassette$file()
Returns

character


Method recording()

is the cassette in recording mode?

Usage
Cassette$recording()
Returns

logical


Method is_empty()

is the cassette on disk empty

Usage
Cassette$is_empty()
Returns

logical


Method originally_recorded_at()

timestamp the cassette was originally recorded at

Usage
Cassette$originally_recorded_at()
Returns

POSIXct date


Method serializable_hash()

Get a list of the http interactions to record + recorded_with

Usage
Cassette$serializable_hash()
Returns

list


Method interactions_to_record()

Get the list of http interactions to record

Usage
Cassette$interactions_to_record()
Returns

list


Method merged_interactions()

Get interactions to record

Usage
Cassette$merged_interactions()
Returns

list


Method up_to_date_interactions()

Cleans out any old interactions based on the re_record_interval and clean_outdated_http_interactions settings

Usage
Cassette$up_to_date_interactions(interactions)
Arguments
interactions

list of http interactions, of class HTTPInteraction

Returns

list of interactions to record


Method should_re_record()

Should re-record interactions?

Usage
Cassette$should_re_record()
Returns

logical


Method should_stub_requests()

Is record mode NOT "all"?

Usage
Cassette$should_stub_requests()
Returns

logical


Method should_remove_matching_existing_interactions()

Is record mode "all"?

Usage
Cassette$should_remove_matching_existing_interactions()
Returns

logical


Method storage_key()

Get the serializer path

Usage
Cassette$storage_key()
Returns

character


Method raw_cassette_bytes()

Get character string of entire cassette; bytes is a misnomer

Usage
Cassette$raw_cassette_bytes()
Returns

character


Method make_dir()

Create the directory that holds the cassettes, if not present

Usage
Cassette$make_dir()
Returns

no return; creates a directory recursively, if missing


Method deserialized_hash()

get http interactions from the cassette via the serializer

Usage
Cassette$deserialized_hash()
Returns

list


Method previously_recorded_interactions()

get all previously recorded interactions

Usage
Cassette$previously_recorded_interactions()
Returns

list


Method write_recorded_interactions_to_disk()

write recorded interactions to disk

Usage
Cassette$write_recorded_interactions_to_disk()
Returns

nothing returned


Method record_http_interaction()

record an http interaction (doesn't write to disk)

Usage
Cassette$record_http_interaction(x)
Arguments
x

an crul or httr response object, with the request at ⁠$request⁠

Returns

nothing returned


Method any_new_recorded_interactions()

Are there any new recorded interactions?

Usage
Cassette$any_new_recorded_interactions()
Returns

logical


Method make_args()

make list of all options

Usage
Cassette$make_args()
Returns

nothing returned


Method write_metadata()

write metadata to the cassette

Usage
Cassette$write_metadata()
Returns

nothing returned


Method http_interactions()

make HTTPInteractionList object, assign to http_interactions_ var

Usage
Cassette$http_interactions()
Returns

nothing returned


Method make_http_interaction()

Make an HTTPInteraction object

Usage
Cassette$make_http_interaction(x)
Arguments
x

an crul or httr response object, with the request at ⁠$request⁠

Returns

an object of class HTTPInteraction


Method serialize_to_crul()

Make a crul response object

Usage
Cassette$serialize_to_crul()
Returns

a crul response


Method clone()

The objects of this class are cloneable with this method.

Usage
Cassette$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

vcr_configure(), use_cassette(), insert_cassette()

Examples

## Not run: 
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")

## End(Not run)

ropenscilabs/vcr documentation built on Feb. 5, 2024, 5:58 p.m.