Switch to httr2 and get more error messages logged
Change-Id: I16050b5ad85f5940fcf6adb3cc647139c7d8d8da
diff --git a/Changelog.md b/Changelog.md
index fd952d1..9b4f875 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -1,5 +1,10 @@
# Changelog
+* use httr2 instead of httr
+* improve error handling even more
+ * error out also on empty results
+ * print request urls
+
## 0.2.9000
* improve error handling:
* show error if token is empty
diff --git a/DESCRIPTION b/DESCRIPTION
index 954d79d..2e66cbc 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -24,7 +24,7 @@
keyring,
stringr,
utils,
- httr,
+ httr2
Suggests:
testthat
Collate:
diff --git a/NAMESPACE b/NAMESPACE
index 5840a09..9148b1c 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -16,7 +16,6 @@
export(select)
export(summarise)
export(year)
-import(httr)
import(keyring)
import(tibble)
importFrom(dplyr,bind_cols)
@@ -27,6 +26,12 @@
importFrom(dplyr,rowwise)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
+importFrom(httr2,req_error)
+importFrom(httr2,req_perform)
+importFrom(httr2,req_url_query)
+importFrom(httr2,request)
+importFrom(httr2,resp_body_json)
+importFrom(httr2,resp_body_raw)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,year)
importFrom(magrittr,"%>%")
diff --git a/R/matomoquery.R b/R/matomoquery.R
index 28ad2f0..cfdfb86 100644
--- a/R/matomoquery.R
+++ b/R/matomoquery.R
@@ -17,7 +17,7 @@
#' @param getMethod API method to call – default: VisitsSummary.get
#' @return Data frame with visits summary as returned by matomo. Note that the \code{date} column in the returned data frame refers to the first day of the respective period.
#'
-#' @import httr
+#' @importFrom httr2 request req_url_query req_perform resp_body_json resp_body_raw req_error
#' @importFrom jsonlite fromJSON
#' @importFrom dplyr mutate rowwise bind_rows select summarise n
#' @import tibble
@@ -51,9 +51,8 @@
call. = FALSE
)
}
- httr::GET(
- url = matomoUrl,
- query = list(
+ res <- httr2::request(matomoUrl) %>%
+ httr2::req_url_query(
module = "API",
method = getMethod,
format = "json",
@@ -63,32 +62,25 @@
filter_limit = filter_limit,
language = "de",
token_auth = accessToken
- )
- ) -> res
+ ) %>%
+ httr2::req_error(body = error_body) %>%
+ httr2::req_perform()
- if (status_code(res) != 200) {
- stop(paste("API call failed:", httr::content(res, "text", encoding = "UTF-8")), call. = FALSE)
- }
+ l <-res %>% httr2::resp_body_json()
- if (!http_type(res) %in% c("application/json", "application/ld+json")) {
- stop(paste("API did not return json:", httr::content(res, "text", encoding = "UTF-8")), call. = FALSE)
- }
-
- json <- httr::content(res, "text", encoding = "UTF-8")
- warning(json)
- l <- jsonlite::fromJSON(json)
-
- if("result" %in% colnames(l) && l$result == 'error') {
- stop(paste("in api call:", l$message), call. = FALSE)
+ if("result" %in% names(l) && l[["result"]] == 'error') {
+ stop(paste("In api call", res$url, ":", l[["message"]], "\n"), call. = FALSE)
}
if (period=="range") {
df <- if (is.list(l) && length(l) == 0) {
- warning("API call returned an empty list.\n", call. = FALSE)
+ stop(paste0("API call ", res$url, " returned the empty list [].\n"), call. = FALSE)
+ } else if (is.list(l)) {
+ df <- bind_rows(l)
} else if (length(siteId) == 1) {
l %>% mutate(site_id=siteId)
} else {
- bind_rows(jsonlite::fromJSON(json), .id = "site_id")
+ bind_rows(l, .id = "site_id")
}
} else {
df <- (if (length(siteId) == 1) {
@@ -130,6 +122,11 @@
return(df)
}
+
+error_body <- function(resp) {
+ return(paste0("getting ", resp$url))
+}
+
utils::globalVariables(c("year", "month", "day", "week"))
#' Save access token persistently to your keyring
@@ -157,7 +154,6 @@
}
-
#' get access token for matomo from keyring
#'
#' @param matomoUrl base URL of your matomo instance