summaryrefslogtreecommitdiffstats
path: root/R/api.R
diff options
context:
space:
mode:
Diffstat (limited to 'R/api.R')
-rw-r--r--R/api.R16
1 files changed, 11 insertions, 5 deletions
diff --git a/R/api.R b/R/api.R
index 0d13c67..895c52a 100644
--- a/R/api.R
+++ b/R/api.R
@@ -13,7 +13,10 @@
#' @section Conditions:
#' \describe{
#' \item{`cepis_invalid_cep`}{the CEP is not 8 digits. No request is made.}
-#' \item{`cepis_not_found`}{well-formed, but not in the database.}
+#' \item{`cepis_not_found`}{well-formed, but not in the database. Carries
+#' `faixa`, a [cepis_faixa] naming the municipality whose range the CEP
+#' fell into, when it fell into one. Still a not-found: the CEP does not
+#' exist, and a `faixa` is not an address.}
#' \item{`cepis_rate_limited`}{429 on every attempt.}
#' \item{`cepis_server_error`}{5xx on every attempt.}
#' \item{`cepis_network_error`}{the request never completed.}
@@ -38,10 +41,13 @@ cep_lookup <- function(cep, client = default_client()) {
response <- cepis_fetch(client, paste0("/", number))
if (response$status == 404L) {
- stop(not_found_error(
- paste0("CEP ", number, " does not exist in the DNE database"),
- cep = number
- ))
+ faixa <- new_faixa(json_field(response$body, "faixa"))
+ message <- paste0("CEP ", number, " does not exist in the DNE database")
+ if (!is.null(faixa)) {
+ message <- paste0(message, "; the range it falls in belongs to ",
+ faixa$localidade, "/", faixa$uf)
+ }
+ stop(not_found_error(message, cep = number, faixa = faixa))
}
if (response$status == 400L) {
stop(invalid_cep_error(