|
| 1 | +convert_registry_to_list <- function() { |
| 2 | + data <- request_registry() |
| 3 | + reqs <- unname(data$request_signatures$hash) |
| 4 | + out <- lapply(reqs, \(x) { |
| 5 | + tmp <- c(key = x$key, count = x$count, x$sig$as_list(), x) |
| 6 | + names(tmp)[names(tmp) == "sig"] <- "request" |
| 7 | + tmp |
| 8 | + }) |
| 9 | + out |
| 10 | +} |
| 11 | + |
| 12 | +#' Request registry filter |
| 13 | +#' |
| 14 | +#' If no filters are given, returns all requests |
| 15 | +#' |
| 16 | +#' @export |
| 17 | +#' @param method (character) http method. default: `NULL` |
| 18 | +#' @param url (character) a url. default: `NULL` |
| 19 | +#' @param body (various) a request body. default: `NULL`. |
| 20 | +#' Can be any of: `character`, `json`, `list`, `raw`, `numeric`, |
| 21 | +#' `NULL`, `FALSE` |
| 22 | +#' @param headers (list) if given, must be a named list. default: `NULL` |
| 23 | +#' @return list, length of number of unique requests recorded, or |
| 24 | +#' those requests matching filters supplied by parameters |
| 25 | +#' @examples |
| 26 | +#' enable(adapter="httr") |
| 27 | +#' |
| 28 | +#' stub_request("any", uri_regex = ".+") |
| 29 | +#' |
| 30 | +#' library(httr) |
| 31 | +#' GET("http://example.com") |
| 32 | +#' GET("https://hb.cran.dev/get") |
| 33 | +#' POST("https://hb.cran.dev/post", body = list(fruit = "apple")) |
| 34 | +#' POST("https://hb.cran.dev/post", body = list(cheese = "swiss")) |
| 35 | +#' GET("https://hb.cran.dev/get", add_headers(Accept = "application/json")) |
| 36 | +#' |
| 37 | +#' request_registry_filter() |
| 38 | +#' request_registry_filter(method="get") |
| 39 | +#' request_registry_filter(method="post") |
| 40 | +#' request_registry_filter(method="get", url="http://example.com") |
| 41 | +#' request_registry_filter(method="post", body=list(fruit = "apple")) |
| 42 | +#' request_registry_filter(method="post", body=list(cheese = "swiss")) |
| 43 | +#' request_registry_filter(method="post", body=list(cheese = "cheddar")) |
| 44 | +#' request_registry_filter(method="get", headers=list(Accept = "application/json")) |
| 45 | +#' |
| 46 | +#' match <- request_registry_filter(method="post")[[1]] |
| 47 | +#' match$request |
| 48 | +#' match$request$to_s() |
| 49 | +#' match$count |
| 50 | +#' |
| 51 | +#' disable() |
| 52 | +request_registry_filter <- function( |
| 53 | + method = NULL, |
| 54 | + url = NULL, |
| 55 | + body = NULL, |
| 56 | + headers = NULL |
| 57 | +) { |
| 58 | + assert_is(url, "character") |
| 59 | + assert_is(method, "character") |
| 60 | + assert_is(headers, "list") |
| 61 | + if (!all(hz_namez(headers))) { |
| 62 | + abort("'headers' must be a named list") |
| 63 | + } |
| 64 | + requests <- convert_registry_to_list() |
| 65 | + |
| 66 | + if (!is.null(method)) { |
| 67 | + requests <- Filter( |
| 68 | + \(x) MethodPattern$new(x$method)$matches(method), |
| 69 | + requests |
| 70 | + ) |
| 71 | + } |
| 72 | + if (!is.null(url)) { |
| 73 | + requests <- Filter(\(x) UriPattern$new(x$uri)$matches(url), requests) |
| 74 | + } |
| 75 | + if (!is.null(body)) { |
| 76 | + requests <- Filter(\(x) BodyPattern$new(x$body)$matches(body), requests) |
| 77 | + } |
| 78 | + if (!is.null(headers)) { |
| 79 | + requests <- Filter( |
| 80 | + \(x) HeadersPattern$new(x$headers)$matches(headers), |
| 81 | + requests |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + requests |
| 86 | +} |
0 commit comments