vcr (version 0.5.4)

VcrResponse: The response of an HTTPInteraction

Description

Custom vcr http response object

Arguments

Public fields

status

the status of the response

headers

the response headers

body

the response body

http_version

the HTTP version

opts

a list

adapter_metadata

Additional metadata used by a specific VCR adapter

hash

a list

disk

a boolean

Methods

Public methods

Method new()

Create a new VcrResponse object

Usage

VcrResponse$new(
  status,
  headers,
  body,
  http_version,
  opts,
  adapter_metadata = NULL,
  disk
)

Arguments

status

the status of the response

headers

the response headers

body

the response body

http_version

the HTTP version

opts

a list

adapter_metadata

Additional metadata used by a specific VCR adapter

disk

boolean, is body a file on disk

Returns

A new VcrResponse object

Method to_hash()

Create a hash

Usage

VcrResponse$to_hash()

Returns

a list

Method from_hash()

Get a hash back to an R list

Usage

VcrResponse$from_hash(hash)

Arguments

hash

a list

Returns

an VcrResponse object

Method update_content_length_header()

Updates the Content-Length response header so that it is accurate for the response body

Usage

VcrResponse$update_content_length_header()

Returns

no return; modifies the content length header

Method get_header()

Get a header by name

Usage

VcrResponse$get_header(key)

Arguments

key

(character) header name to get

Returns

the header value (if it exists)

Method edit_header()

Edit a header

Usage

VcrResponse$edit_header(key, value = NULL)

Arguments

key

(character) header name to edit

value

(character) new value to assign

Returns

no return; modifies the header in place

Method delete_header()

Delete a header

Usage

VcrResponse$delete_header(key)

Arguments

key

(character) header name to delete

Returns

no return; the header is deleted if it exists

Method content_encoding()

Get the content-encoding header value

Usage

VcrResponse$content_encoding()

Returns

(character) the content-encoding value

Method is_compressed()

Checks if the encoding is one of "gzip" or "deflate"

Usage

VcrResponse$is_compressed()

Returns

logical

Method clone()

The objects of this class are cloneable with this method.

Usage

VcrResponse$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

Run this code
# NOT RUN {
vcr_configure(dir = tempdir())


url <- "https://google.com"
(cli <- crul::HttpClient$new(url = url))
(res <- cli$get("get", query = list(q = "stuff")))
(x <- VcrResponse$new(res$status_http(), res$response_headers,
   res$parse("UTF-8"), res$response_headers$status))
x$body
x$status
x$headers
x$http_version
x$to_hash()
x$from_hash(x$to_hash())


# update content length header
## example 1
### content-length header present, but no change
url <- "https://fishbase.ropensci.org"
cli <- crul::HttpClient$new(url = url, headers = list(`Accept-Encoding` = '*'))
res <- cli$get("species/34")
x <- VcrResponse$new(res$status_http(), res$response_headers,
   res$parse("UTF-8"), res$response_headers$status)
x$headers$`content-length`
x$update_content_length_header()
x$headers$`content-length`

## example 2
### no content-length header
url <- "https://google.com"
cli <- crul::HttpClient$new(url = url)
res <- cli$get()
x <- VcrResponse$new(res$status_http(), res$response_headers,
   rawToChar(res$content), res$response_headers$status)
x$headers$`content-length`
x$update_content_length_header() # no change, b/c header doesn't exist


# check if body is compressed
url <- "https://fishbase.ropensci.org"
(cli <- crul::HttpClient$new(url = url))
(res <- cli$get("species/3"))
res$response_headers
(x <- VcrResponse$new(res$status_http(), res$response_headers,
   res$parse("UTF-8"), res$response_headers$status))
x$content_encoding()
x$is_compressed()

# with disk
url <- "https://google.com"
(cli <- crul::HttpClient$new(url = url))
f <- tempfile()
(res <- cli$get("get", query = list(q = "stuff"), disk = f))
(x <- VcrResponse$new(res$status_http(), res$response_headers,
   f, res$response_headers$status, disk = TRUE))
# }

Run the code above in your browser using DataLab