From a97c4e607fa84e1272008930a14f0821dd324344 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Sat, 13 Aug 2022 18:15:13 -0400 Subject: [PATCH 1/8] testing try-error class - testing a try-error class evaluation --- R/callDATIMapi.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index a088731c..8e9e78ae 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -96,7 +96,7 @@ api_get <- function(path, handle = handle) ) - if (is.null(resp)) { + if (is.null(resp) & class(resp) != "try-error") { next } From 2804a6b9773ac94eb1c3aab06f1a54d920aac59d Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Sun, 14 Aug 2022 18:27:39 -0400 Subject: [PATCH 2/8] Update callDATIMapi.R testing try error catch --- R/callDATIMapi.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 8e9e78ae..4f55c453 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -96,7 +96,7 @@ api_get <- function(path, handle = handle) ) - if (is.null(resp) & class(resp) != "try-error") { + if (is.null(resp) | class(resp) != "try-error") { next } From 5745eda3d120def6f16d699e8ea9fa0f5f2acc76 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Sun, 14 Aug 2022 18:42:08 -0400 Subject: [PATCH 3/8] Update callDATIMapi.R test try-error class --- R/callDATIMapi.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 4f55c453..8d3ae6b0 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -96,8 +96,10 @@ api_get <- function(path, handle = handle) ) - if (is.null(resp) | class(resp) != "try-error") { + if (is.null(resp)) { next + } else if (class(resp) == "try-error") { + stop("There is an issue with the API, try again.") } response_code <- httr::status_code(resp) From 276e833628f27d17274ace8061c0487fa1771c1e Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Sun, 14 Aug 2022 22:02:46 -0400 Subject: [PATCH 4/8] Update callDATIMapi.R test another attempt to catch try-error --- R/callDATIMapi.R | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 8d3ae6b0..9e973c50 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -84,7 +84,7 @@ api_get <- function(path, print(url) } - # retry api get block, only retries if reponse code not in 400s + # retry api get block, only retries if response code not in 400s i <- 1 response_code <- 5 @@ -96,11 +96,9 @@ api_get <- function(path, handle = handle) ) - if (is.null(resp)) { + if (is.null(resp) && class(resp) != "try-error") { next - } else if (class(resp) == "try-error") { - stop("There is an issue with the API, try again.") - } + } response_code <- httr::status_code(resp) Sys.sleep(i - 1) From 2e1336fdea49daba3adc38479b861da9d2f09605 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Mon, 15 Aug 2022 10:57:55 -0400 Subject: [PATCH 5/8] Update callDATIMapi.R try-error take 5 --- R/callDATIMapi.R | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 9e973c50..f50a3afb 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -87,19 +87,16 @@ api_get <- function(path, # retry api get block, only retries if response code not in 400s i <- 1 response_code <- 5 - - while (i <= retry && (response_code < 400 || response_code >= 500)) { - resp <- NULL + resp <- "try-error" + + while (i <= retry && (resp == "try-error"|| response_code < 400 || response_code >= 500)) { + #resp <- NULL resp <- try( httr::GET(url, httr::timeout(timeout), handle = handle) ) - if (is.null(resp) && class(resp) != "try-error") { - next - } - response_code <- httr::status_code(resp) Sys.sleep(i - 1) i <- i + 1 From 5090298a50519316590fcba754fc7277a2f480b8 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Mon, 15 Aug 2022 13:01:40 -0400 Subject: [PATCH 6/8] try bypassing try error test fixing try error --- R/callDATIMapi.R | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index f50a3afb..2d9c617d 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -87,19 +87,20 @@ api_get <- function(path, # retry api get block, only retries if response code not in 400s i <- 1 response_code <- 5 - resp <- "try-error" - while (i <= retry && (resp == "try-error"|| response_code < 400 || response_code >= 500)) { - #resp <- NULL + while (i <= retry && (response_code < 400 || response_code >= 500)) { + resp <- NULL resp <- try( httr::GET(url, httr::timeout(timeout), handle = handle) ) - response_code <- httr::status_code(resp) + # try is added in order to handle if resp comes back as a "try-error" class + response_code <- try(httr::status_code(resp)) Sys.sleep(i - 1) i <- i + 1 + if (response_code == 200L && stringi::stri_replace(resp$url, regex = ".*/api/", replacement = "") == stringi::stri_replace(url, regex = ".*/api/", replacement = "") && @@ -108,6 +109,17 @@ api_get <- function(path, } } + + # let user know that by the last attempt the api continues to return an error, this should break before status code + # as a status code cannot be pulled from a failed api grab + if(class(resp) == "try-error") { + stop( + paste0( + "response error, server returned no response even on the last retry, this could a malformed link or a server issue, otherwise try again ", + url + ) + ) + } # unknown error catching which returns message and response code if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 500) { From bded3b297451ec818a1d873c28c84b21174a8612 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Mon, 15 Aug 2022 16:10:43 -0400 Subject: [PATCH 7/8] add message - add in line message for attempts --- R/callDATIMapi.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 2d9c617d..3a854288 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -98,6 +98,17 @@ api_get <- function(path, # try is added in order to handle if resp comes back as a "try-error" class response_code <- try(httr::status_code(resp)) + + if(is(response_code, "try-error")) { + message( + paste0( + "api call to server failed on attempt ", + i, + " trying again ..." + ) + ) + } + Sys.sleep(i - 1) i <- i + 1 From 61193025a928eff1fced14f3ec1c6f2c7b041014 Mon Sep 17 00:00:00 2001 From: Fausto Lopez Date: Mon, 15 Aug 2022 17:09:38 -0400 Subject: [PATCH 8/8] Linting -linted and adding silent on try --- R/callDATIMapi.R | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/R/callDATIMapi.R b/R/callDATIMapi.R index 3a854288..c0d4b097 100644 --- a/R/callDATIMapi.R +++ b/R/callDATIMapi.R @@ -87,7 +87,7 @@ api_get <- function(path, # retry api get block, only retries if response code not in 400s i <- 1 response_code <- 5 - + while (i <= retry && (response_code < 400 || response_code >= 500)) { resp <- NULL resp <- @@ -97,21 +97,21 @@ api_get <- function(path, ) # try is added in order to handle if resp comes back as a "try-error" class - response_code <- try(httr::status_code(resp)) - - if(is(response_code, "try-error")) { + response_code <- try(httr::status_code(resp), silent = TRUE) + + if (is(response_code, "try-error")) { message( paste0( - "api call to server failed on attempt ", + "Api call to server failed on attempt ", i, " trying again ..." ) ) } - + Sys.sleep(i - 1) i <- i + 1 - + if (response_code == 200L && stringi::stri_replace(resp$url, regex = ".*/api/", replacement = "") == stringi::stri_replace(url, regex = ".*/api/", replacement = "") && @@ -120,13 +120,14 @@ api_get <- function(path, } } - + # let user know that by the last attempt the api continues to return an error, this should break before status code # as a status code cannot be pulled from a failed api grab - if(class(resp) == "try-error") { + if (class(resp) == "try-error") { stop( paste0( - "response error, server returned no response even on the last retry, this could a malformed link or a server issue, otherwise try again ", + "Server returned no response even on the last retry, + this could a malformed link or a server issue, otherwise try again ", url ) )