Switch to httr2 and get more error messages logged

Change-Id: I16050b5ad85f5940fcf6adb3cc647139c7d8d8da
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