Print the API's welcome/ToS message when opening a connection
Change-Id: I856ec9b439db402579bcd5cf91cf308d8c7f720c
diff --git a/R/KorAPConnection.R b/R/KorAPConnection.R
index be1853a..9808f88 100644
--- a/R/KorAPConnection.R
+++ b/R/KorAPConnection.R
@@ -78,6 +78,7 @@
.Object@timeout = timeout
.Object@verbose = verbose
.Object@cache = cache
+ message(apiCall(.Object, .Object@apiUrl, json = FALSE, cache = FALSE))
.Object
})
@@ -151,46 +152,53 @@
#' @rdname KorAPConnection-class
#' @param kco KorAPConnection object
#' @param url request url
+#' @param json logical that determines if json result is expected
#' @importFrom jsonlite fromJSON
#' @export
-setMethod("apiCall", "KorAPConnection", function(kco, url) {
- if (kco@cache) {
- parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url, kco@accessToken))
- if (!is.null(parsed)) {
- if (!is.null(parsed$meta))
- parsed$meta$cached <- "local"
- return(parsed)
+setMethod("apiCall", "KorAPConnection", function(kco, url, json = TRUE, cache = kco@cache) {
+ result <- ""
+ if (cache) {
+ result <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url, kco@accessToken))
+ if (!is.null(result)) {
+ if (!is.null(result$meta))
+ result$meta$cached <- "local"
+ return(result)
}
}
if (!is.null(kco@accessToken))
resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout), add_headers(Authorization = paste("Bearer", kco@accessToken)))
else
resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
- if (!http_type(resp) %in% c("application/json", "application/ld+json")) {
- stop("API did not return json", call. = FALSE)
- }
- parsed <- jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8"))
- if (!is.null(parsed$warnings)) {
- message <- if (nrow(parsed$warnings) > 1)
- sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n"))
- else
- sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2])
- warning(message, call. = FALSE)
+ if (json || status_code(resp) != 200) {
+ if (json && !http_type(resp) %in% c("application/json", "application/ld+json")) {
+ stop("API did not return json", call. = FALSE)
+ }
+ result <- jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8"))
+ if (!is.null(result$warnings)) {
+ message <- if (nrow(result$warnings) > 1)
+ sapply(result$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n"))
+ else
+ sprintf("%s: %s", result$warnings[1], result$warnings[2])
+ warning(message, call. = FALSE)
+ }
}
if (status_code(resp) != 200) {
if (kco@verbose) {
cat("\n")
}
message <- sprintf("%s KorAP API request failed", status_code(resp))
- if (!is.null(parsed$errors)) {
- message <- sprintf("%s - %s %s", message, parsed$errors[1], parsed$errors[2])
+ if (!is.null(result$errors)) {
+ message <- sprintf("%s - %s %s", message, result$errors[1], result$errors[2])
}
stop(message, call. = FALSE)
}
- if (kco@cache) {
- R.cache::saveCache(parsed, key = list(url, kco@accessToken), dir = KorAPCacheSubDir(), compress = TRUE)
+ if (!json) {
+ result <- content(resp, "text", encoding = "UTF-8")
}
- parsed
+ if (cache) {
+ R.cache::saveCache(result, key = list(url, kco@accessToken), dir = KorAPCacheSubDir(), compress = TRUE)
+ }
+ result
})
setGeneric("clearCache", function(kco) standardGeneric("clearCache") )
diff --git a/man/KorAPConnection-class.Rd b/man/KorAPConnection-class.Rd
index b660582..6b3f4fd 100644
--- a/man/KorAPConnection-class.Rd
+++ b/man/KorAPConnection-class.Rd
@@ -32,7 +32,7 @@
\S4method{clearAccessToken}{KorAPConnection}(kco)
-\S4method{apiCall}{KorAPConnection}(kco, url)
+\S4method{apiCall}{KorAPConnection}(kco, url, json = TRUE, cache = kco@cache)
\S4method{clearCache}{KorAPConnection}(kco)
@@ -78,6 +78,8 @@
\item{url}{request url}
+\item{json}{logical that determines if json result is expected}
+
\item{object}{KorAPConnection object}
}
\value{
diff --git a/tests/testthat/test-KorAPConnection.R b/tests/testthat/test-KorAPConnection.R
index 298a333..65c8f82 100644
--- a/tests/testthat/test-KorAPConnection.R
+++ b/tests/testthat/test-KorAPConnection.R
@@ -3,12 +3,16 @@
expect_error(print(kco), NA)
})
-test_that("Opening KorAPConnection with apiToken works", {
- kco <- new("KorAPConnection", accessToken="test token")
- persistAccessToken(kco)
- kco <- new("KorAPConnection")
- expect_equal(kco@accessToken, "test token")
- clearAccessToken(kco)
+test_that("Opening KorAPConnection prints some message.", {
+ expect_message(new("KorAPConnection"), "KorAP")
+})
+
+test_that("Opening KorAPConnection with invalid apiToken fails", {
+ expect_error(new("KorAPConnection", accessToken="test token"),
+ "401")
+})
+
+test_that("Persisting null apiToken fails", {
kco <- new("KorAPConnection")
expect_null(kco@accessToken)
expect_error(persistAccessToken(kco),