From cdbf4ec445037479bba88f60e2010192f9fd7b7b Mon Sep 17 00:00:00 2001 From: Tarciso Braz Date: Wed, 1 Apr 2020 20:22:20 +0000 Subject: [PATCH 1/3] =?UTF-8?q?Melhora=20log=20de=20checagem=20de=20recurs?= =?UTF-8?q?o=20na=20cache,=20tornando-o=20menos=20verborr=C3=A1gico.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/cache.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/cache.R b/R/cache.R index cd6290f..9e37d2e 100644 --- a/R/cache.R +++ b/R/cache.R @@ -54,9 +54,9 @@ setup_cache <- function (name) { } if (is.null(value)) { - print(c("Not in cache", .cache_file_path)) + cat("\nNot in cache") } else { - print(c("In cache", .cache_file_path)) + cat("\nIn cache") } return(value) } From 56aa4df1a9f1d2d6c7f0f59229d3156080d437c9 Mon Sep 17 00:00:00 2001 From: Tarciso Braz Date: Wed, 1 Apr 2020 20:23:31 +0000 Subject: [PATCH 2/3] =?UTF-8?q?Adiciona=20toler=C3=A2ncia=20a=20falhas=20n?= =?UTF-8?q?o=20n=C3=ADvel=20de=20exce=C3=A7=C3=A3o=20do=20c=C3=B3digo=20?= =?UTF-8?q?=C3=A0=20requisi=C3=A7=C3=A3o,=20utilizando=20back-off=20expone?= =?UTF-8?q?ncial.=20Melhora=20log=20dos=20erros/tentativas=20do=20back-off?= =?UTF-8?q?=20exponencial.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- R/utils.R | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/R/utils.R b/R/utils.R index 1eb01aa..eb68ba6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -37,6 +37,7 @@ if (getRversion() >= "2.15.1") utils::globalVariables(".") status_code = 1000 resp_in_cache = FALSE resp <- NULL + error_msg <- '' if (is.null(base_url) || base_url == '') { warning("URL deve ser não-nula e não-vazia.") @@ -53,23 +54,35 @@ if (getRversion() >= "2.15.1") utils::globalVariables(".") } if (num_tries > 0) { - cat("\n","Error on Calling URL:",url," - Status Code:",status_code) + cat("\nError on Calling URL:", url, + " - Status Code:", status_code, + " - Attempt no.", num_tries + 1) + if (error_msg != '') cat("\nDetailed Error:",error_msg,"\n") sleep_time <- base_sleep_time^(num_tries) Sys.sleep(sleep_time) } - - resp <- .get_from_cache(api_url) - - if (is.null(resp)) { - if (accept_json) resp <- httr::GET(url, httr::accept_json()) - else resp <- httr::GET(url) - Sys.sleep(.DEF_POST_REQ_SLEEP_TIME) - status_code <- httr::status_code(resp) + + error_msg <- tryCatch({ + resp <- .get_from_cache(url) + + if (is.null(resp)) { + if (accept_json) resp <- httr::GET(url, httr::accept_json()) + else resp <- httr::GET(url) + Sys.sleep(.DEF_POST_REQ_SLEEP_TIME) + status_code <- httr::status_code(resp) + } else { + resp_in_cache <- TRUE + status_code <- 200 + } + + }, warning = function(w) { + message("\n","Warning while fetching: ", url) + warning(w) + }, error = function(e) { + return(e$message) + }, finally = { num_tries <- num_tries + 1 - } else { - resp_in_cache <- TRUE - status_code <- 200 - } + }) } if ((status_code >= .COD_ERRO_CLIENTE)) { From 414348eea722d6ccad6cbe349f5c7606c45d8323 Mon Sep 17 00:00:00 2001 From: Tarciso Braz Date: Wed, 1 Apr 2020 20:24:23 +0000 Subject: [PATCH 3/3] =?UTF-8?q?Adiciona=20teste=20para=20novo=20caso=20de?= =?UTF-8?q?=20uso=20(back-off=20exponencial=20em=20caso=20de=20exce=C3=A7?= =?UTF-8?q?=C3=A3o=20no=20c=C3=B3digo=20da=20requisi=C3=A7=C3=A3o)=20e=20m?= =?UTF-8?q?elhora=20testes=20existentes=20do=20arquivo=20utils.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/testthat/test_utils.R | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test_utils.R b/tests/testthat/test_utils.R index 79e1dda..5f32a68 100644 --- a/tests/testthat/test_utils.R +++ b/tests/testthat/test_utils.R @@ -79,18 +79,26 @@ test_that(".unnest_df_column returns correct result", { test_that(".get_with_exponential_backoff_cached does not apply exponential backoff when response code is 404", { start_time <- Sys.time() - tryCatch({ - .get_with_exponential_backoff_cached(base_url="https://dadosabertos.camara.leg.br", + + expect_error(.get_with_exponential_backoff_cached(base_url="https://dadosabertos.camara.leg.br", path='/api/v2/proposicoes/-1', base_sleep_time=1, - max_attempts=1) - }, warning = function(w) { - }, error = function(e) { - }, finally = { - end_time <- Sys.time() - }) - + max_attempts=1)) + end_time <- Sys.time() elapsed_time <- end_time - start_time expect_true(elapsed_time < 3) }) + +test_that(".get_with_exponential_backoff_cached applies exponential backoff when there is an exception during request", { + start_time <- Sys.time() + + expect_error(.get_with_exponential_backoff_cached(base_url="https://-1", + path='', + base_sleep_time=2, + max_attempts=3)) + end_time <- Sys.time() + elapsed_time <- end_time - start_time + + expect_true(elapsed_time > 4) +})