Learn R Programming

webmockr (version 2.0.0)

CrulAdapter: Adapters for Modifying HTTP Requests

Description

Adapter is the base parent class used to implement webmockr support for different HTTP clients. It should not be used directly. Instead, use one of the client-specific adapters that webmockr currently provides:

  • CrulAdapter for crul

  • HttrAdapter for httr

  • Httr2Adapter for httr2

Arguments

Super class

webmockr::Adapter -> CrulAdapter

Public fields

client

HTTP client package name

name

adapter name

Methods

Public methods

Inherited methods


Method clone()

The objects of this class are cloneable with this method.

Usage

CrulAdapter$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Super class

webmockr::Adapter -> HttrAdapter

Public fields

client

HTTP client package name

name

adapter name

Methods

Public methods

Inherited methods


Method clone()

The objects of this class are cloneable with this method.

Usage

HttrAdapter$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Super class

webmockr::Adapter -> Httr2Adapter

Public fields

client

HTTP client package name

name

adapter name

Methods

Public methods

Inherited methods


Method clone()

The objects of this class are cloneable with this method.

Usage

Httr2Adapter$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Public fields

client

HTTP client package name

name

adapter name

Methods


Method new()

Create a new Adapter object

Usage

Adapter$new()


Method enable()

Enable the adapter

Usage

Adapter$enable(quiet = FALSE)

Arguments

quiet

(logical) suppress messages? default: FALSE

Returns

TRUE, invisibly


Method disable()

Disable the adapter

Usage

Adapter$disable(quiet = FALSE)

Arguments

quiet

(logical) suppress messages? default: FALSE

Returns

FALSE, invisibly


Method handle_request()

All logic for handling a request

Usage

Adapter$handle_request(req)

Arguments

req

a request

Returns

various outcomes


Method remove_stubs()

Remove all stubs

Usage

Adapter$remove_stubs()

Returns

nothing returned; removes all request stubs


Method clone()

The objects of this class are cloneable with this method.

Usage

Adapter$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Details

Note that the documented fields and methods are the same across all client-specific adapters.

Examples

Run this code
if (FALSE) {
if (requireNamespace("httr", quietly = TRUE)) {
  # library(httr)

  # normal httr request, works fine
  # real <- GET("https://httpbin.org/get")
  # real

  # with webmockr
  # library(webmockr)
  ## turn on httr mocking
  # httr_mock()
  ## now this request isn't allowed
  # GET("https://httpbin.org/get")
  ## stub the request
  # stub_request('get', uri = 'https://httpbin.org/get') %>%
  #   wi_th(
  #     headers = list(
  #      'Accept' = 'application/json, text/xml, application/xml, */*'
  #     )
  #   ) %>%
  #   to_return(status = 418, body = "I'm a teapot!", headers = list(a = 5))
  ## now the request succeeds and returns a mocked response
  # (res <- GET("https://httpbin.org/get"))
  # res$status_code
  # rawToChar(res$content)

  # allow real requests while webmockr is loaded
  # webmockr_allow_net_connect()
  # webmockr_net_connect_allowed()
  # GET("https://httpbin.org/get?animal=chicken")
  # webmockr_disable_net_connect()
  # webmockr_net_connect_allowed()
  # GET("https://httpbin.org/get?animal=chicken")

  # httr_mock(FALSE)
}
}

Run the code above in your browser using DataLab