From d996487999ca4799e1b90ed47e9beb48b0d79cad Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:06:01 +0100 Subject: [PATCH 1/4] Test if `check_request_success()` returns an error on a bad request --- tests/testthat/test-utils.R | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/testthat/test-utils.R diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..0c6f862 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,17 @@ +test_that("check_request_success() returns error on bad access token", { + bad_request <- + httr2::request("https://api.iasset.nl/getCustomInspectionFields/") %>% + httr2::req_body_form( + access_token = "NOT AN ACCESS TOKEN" + ) %>% + httr2::req_perform() %>% + httr2::resp_body_json(check_type = F) + expect_error( + check_request_success(bad_request), + regexp = "Something went wrong while accessing the API:" + ) +}) + +test_that("check_request_success() returns TRUE on a correct request", { + # TODO: caching some responses without any secrets in them +}) From 35246714e66fd12ce2fd19778b54598ca01938ed Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:06:26 +0100 Subject: [PATCH 2/4] Create `check_request_success()`: forward api errors --- R/utils.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 R/utils.R diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..174e60a --- /dev/null +++ b/R/utils.R @@ -0,0 +1,19 @@ +#' Helper to check if a Request was successful +#' +#' @param parsed_response +#' +#' @return `TRUE` for a successful request, otherwise, an error. +#' +#' @examples +check_request_success <- function(parsed_response) { + # If the request is successful, the response will have `error$FALSE` otherwise, + # it'll contain a message + assertthat::assert_that(isFALSE(purrr::pluck(parsed_response, "error")), + msg = paste0( + "Something went wrong while accessing the API:\n", + purrr::pluck(parsed_response, "error", "error_msg") + ) + ) + # This will only get evaluated if the above assertion passes. + return(TRUE) +} From 84f39c0b64dbb5c6ce65902302a63d7509cf89c8 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:16:56 +0100 Subject: [PATCH 3/4] set family, don't render rd --- R/utils.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/utils.R b/R/utils.R index 174e60a..dc9d8fd 100644 --- a/R/utils.R +++ b/R/utils.R @@ -3,10 +3,10 @@ #' @param parsed_response #' #' @return `TRUE` for a successful request, otherwise, an error. -#' -#' @examples +#' @family helper functions +#' @noRd check_request_success <- function(parsed_response) { - # If the request is successful, the response will have `error$FALSE` otherwise, + # If the request is successful, the response will have `error$FALSE`otherwise, # it'll contain a message assertthat::assert_that(isFALSE(purrr::pluck(parsed_response, "error")), msg = paste0( From 6c43a7665c65d028fa8356e0c6d20e3a3bd47473 Mon Sep 17 00:00:00 2001 From: Pieter Huybrechts <48065851+PietrH@users.noreply.github.com> Date: Tue, 14 Nov 2023 16:17:10 +0100 Subject: [PATCH 4/4] init helper to check for valid access token shape --- R/utils.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/utils.R b/R/utils.R index dc9d8fd..5bf7ef6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -17,3 +17,17 @@ check_request_success <- function(parsed_response) { # This will only get evaluated if the above assertion passes. return(TRUE) } + +#' Helper to assert that the access_token has the right shape +#' +#' @param access_token +#' +#' @return `TRUE` for a string in the shape of an access token, +#' otherwise an error +#' @family helper functions +#' @noRd +is_access_token <- function(access_token) { + assertthat::assert_that(assertthat::is.string(access_token)) + assertthat::assert_that(nchar(access_token) == 120) + assertthat::assert_that(grepl("^[-A-Za-z0-9+\\/]*={0,3}$", access_token)) +}