Adapt to new API behaviour

Change-Id: Id09143fb460ccaea39eca88a982d19f0f7135c3d
diff --git a/R/matomoquery.R b/R/matomoquery.R
index d9fd57a..8a8aad4 100644
--- a/R/matomoquery.R
+++ b/R/matomoquery.R
@@ -34,7 +34,7 @@
                              siteId,
                              period = "month",
                              date = "last16",
-                             filter_limit = 100,
+                             filter_limit = 500,
                              removeFirst = FALSE,
                              removeLast = FALSE,
                              accessToken = getAccessToken(matomoUrl),
@@ -55,9 +55,8 @@
     url = matomoUrl,
     query = list(
       module = "API",
-      method = "API.getBulkRequest",
+      method = getMethod,
       format = "json",
-      "urls[0]" = paste0("method=", getMethod),
       idSite = paste0(siteId, collapse = ","),
       date = date,
       period = period,
@@ -86,31 +85,36 @@
   }
 
   json <- httr::content(res, "text", encoding = "UTF-8")
-  l <- jsonlite::fromJSON(json, simplifyVector = F)
 
-  if (!is.null(l[[1]]$result) && l[[1]]$result == "error") {
-    stop(l[[1]]$message, call. = FALSE)
-  }
-
-  df <- (if (length(siteId) == 1) {
-    bind_rows(l[[1]], .id=period) %>%
-      head(if(removeLast) -1 else filter_limit) %>%
-      tail(if(removeFirst) -1 else filter_limit) %>%
-      mutate(site_id=siteId)
-  } else {
-    df <- bind_rows(l[[1]][[1]], .id=period) %>%
-      head(if(removeLast) -1 else filter_limit) %>%
-      tail(if(removeFirst) -1 else filter_limit) %>%
-      mutate(site_id=siteId[1])
-    for (i in 2:length(l[[1]])) {
-      df <- bind_rows(df,
-                      bind_rows(l[[1]][[i]], .id=period) %>%
-                        head(if(removeLast) -1 else filter_limit) %>%
-                        tail(if(removeFirst) -1 else filter_limit) %>%
-                        mutate(site_id=siteId[i]))
+  if (period=="range") {
+    df <- if (length(siteId) == 1) {
+      jsonlite::fromJSON(json) %>%
+        mutate(site_id=siteId)
+    } else {
+      bind_rows(jsonlite::fromJSON(json), .id = "site_id")
     }
-    df
-  })
+  } else {
+    l <- jsonlite::fromJSON(json)
+    df <- (if (length(siteId) == 1) {
+      bind_rows(l, .id=period) %>%
+        head(if(removeLast) -1 else filter_limit) %>%
+        tail(if(removeFirst) -1 else filter_limit) %>%
+        mutate(site_id=siteId)
+    } else {
+      df <- bind_rows(l[[1]], .id=period) %>%
+        head(if(removeLast) -1 else filter_limit) %>%
+        tail(if(removeFirst) -1 else filter_limit) %>%
+        mutate(site_id=siteId[1])
+      for (i in 2:length(l)) {
+        df <- bind_rows(df,
+                        bind_rows(l[[i]], .id=period) %>%
+                          head(if(removeLast) -1 else filter_limit) %>%
+                          tail(if(removeFirst) -1 else filter_limit) %>%
+                          mutate(site_id=siteId[i]))
+      }
+      df
+    })
+  }
 
   if("day" %in% colnames(df) | "month" %in% colnames(df) | "ye" %in% colnames(df)) {
     df <- df %>%
@@ -125,7 +129,7 @@
           day
         else
           stop(paste0("unsupported period parameter: '", period, "'"), call. = FALSE)
-      ))
+      , optional = TRUE))
   }
   return(df)
 }