Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 1 | ################################################################################ |
| 2 | # Use setClassUnion to define the unholy NULL-data union as a virtual class. |
| 3 | ################################################################################ |
| 4 | setClassUnion("characterOrNULL", c("character", "NULL")) |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 5 | setClassUnion("listOrNULL", c("list", "NULL")) |
Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 6 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 7 | #' Class KorAPConnection |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 8 | #' |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 9 | #' `KorAPConnection` objects represent the connection to a KorAP server. |
| 10 | #' New `KorAPConnection` objects can be created by `new("KorAPConnection")`. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 11 | #' |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 12 | #' @import R.cache |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 13 | #' @import utils |
| 14 | #' @import methods |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 15 | #' @export |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 16 | KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", indexRevision="characterOrNULL", apiUrl="character", accessToken="characterOrNULL", userAgent="character", timeout="numeric", verbose="logical", cache="logical", welcome="listOrNULL")) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 17 | |
| 18 | #' @param .Object KorAPConnection object |
| 19 | #' @param KorAPUrl the URL of the KorAP server instance you want to access. |
| 20 | #' @param apiVersion which version of KorAP's API you want to connect to. |
| 21 | #' @param apiUrl URL of the KorAP web service. |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 22 | #' @param accessToken OAuth2 access token. To use authorization based on an access token |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 23 | #' in subsequent queries, initialize your KorAP connection with |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 24 | #' `kco <- new("KorAPConnection", accessToken="<access token>")`. |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 25 | #' In order to make the API |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 26 | #' token persistent for the currently used `KorAPUrl` (you can have one |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 27 | #' token per KorAPUrl / KorAP server instance), use |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 28 | #' `persistAccessToken(kco)`. This will store it in your keyring using the |
| 29 | #' [keyring()] package. Subsequent new("KorAPConnection") calls will |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 30 | #' then automatically retrieve the token from your keying. To stop using a |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 31 | #' persisted token, call `clearAccessToken(kco)`. Please note that for |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 32 | #' DeReKo, authorized queries will behave differently inside and outside the |
| 33 | #' IDS, because of the special license situation. This concerns also cached |
| 34 | #' results which do not take into account from where a request was issued. If |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 35 | #' you experience problems or unexpected results, please try `kco <- |
| 36 | #' new("KorAPConnection", cache=FALSE)` or use |
| 37 | #' [clearCache()] to clear the cache completely. |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 38 | #' @param userAgent user agent string. |
| 39 | #' @param timeout time out in seconds. |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 40 | #' @param verbose logical. Decides whether following operations will default to |
| 41 | #' be verbose. |
| 42 | #' @param cache logical. Decides if API calls are cached locally. You can clear |
Marc Kupietz | 67edcb5 | 2021-09-20 21:54:24 +0200 | [diff] [blame] | 43 | #' the cache with [clearCache()]. |
| 44 | #' @return [KorAPConnection()] object that can be used e.g. with |
| 45 | #' [corpusQuery()] |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 46 | #' |
| 47 | #' @examples |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 48 | #' \dontrun{ |
| 49 | #' |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 50 | #' kcon <- new("KorAPConnection", verbose = TRUE) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 51 | #' kq <- corpusQuery(kcon, "Ameisenplage") |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 52 | #' kq <- fetchAll(kq) |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 53 | #' } |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 54 | #' |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 55 | #' \dontrun{ |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 56 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 57 | #' kcon <- new("KorAPConnection", verbose = TRUE, accessToken="e739u6eOzkwADQPdVChxFg") |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 58 | #' kq <- corpusQuery(kcon, "Ameisenplage", metadataOnly=FALSE) |
| 59 | #' kq <- fetchAll(kq) |
| 60 | #' kq@collectedMatches$snippet |
| 61 | #' } |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 62 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 63 | #' @rdname KorAPConnection-class |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 64 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 65 | setMethod("initialize", "KorAPConnection", |
Marc Kupietz | 6a3185b | 2021-12-07 10:23:16 +0100 | [diff] [blame] | 66 | function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, accessToken = getAccessToken(KorAPUrl), userAgent = "R-KorAP-Client", timeout=240, verbose = FALSE, cache = TRUE) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 67 | .Object <- callNextMethod() |
| 68 | m <- regexpr("https?://[^?]+", KorAPUrl, perl = TRUE) |
| 69 | .Object@KorAPUrl <- regmatches(KorAPUrl, m) |
| 70 | if (!endsWith(.Object@KorAPUrl, '/')) { |
| 71 | .Object@KorAPUrl <- paste0(.Object@KorAPUrl, "/") |
| 72 | } |
| 73 | if (missing(apiUrl)) { |
| 74 | .Object@apiUrl = paste0(.Object@KorAPUrl, 'api/', apiVersion, '/') |
| 75 | } else { |
| 76 | .Object@apiUrl = apiUrl |
| 77 | } |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 78 | .Object@accessToken = accessToken |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 79 | .Object@apiVersion = apiVersion |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 80 | .Object@userAgent = userAgent |
| 81 | .Object@timeout = timeout |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 82 | .Object@verbose = verbose |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 83 | .Object@cache = cache |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 84 | .Object@welcome = apiCall(.Object, .Object@apiUrl, json = FALSE, cache = FALSE, getHeaders = TRUE) |
| 85 | if (!is.null(.Object@welcome)) { |
| 86 | message(.Object@welcome[[2]]) |
| 87 | } |
| 88 | .Object@indexRevision <- .Object@welcome[[1]][["x-index-revision"]] |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 89 | .Object |
| 90 | }) |
| 91 | |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 92 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 93 | accessTokenServiceName <- "RKorAPClientAccessToken" |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 94 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 95 | setGeneric("persistAccessToken", function(kco, ...) standardGeneric("persistAccessToken") ) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 96 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 97 | #' @aliases persistAccessToken |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 98 | #' @rdname KorAPConnection-class |
| 99 | #' @import keyring |
| 100 | #' @export |
| 101 | #' @examples |
| 102 | #' \dontrun{ |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 103 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 104 | #' kco <- new("KorAPConnection", accessToken="e739u6eOzkwADQPdVChxFg") |
| 105 | #' persistAccessToken(kco) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 106 | #' } |
| 107 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 108 | setMethod("persistAccessToken", "KorAPConnection", function(kco, accessToken = kco@accessToken) { |
| 109 | if (is.null(accessToken)) |
| 110 | stop("It seems that you have not supplied any access token that could be persisted.", call. = FALSE) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 111 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 112 | kco@accessToken <- accessToken |
| 113 | key_set_with_value(accessTokenServiceName, kco@KorAPUrl, accessToken) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 114 | }) |
| 115 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 116 | setGeneric("clearAccessToken", function(kco) standardGeneric("clearAccessToken") ) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 117 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 118 | #' @aliases clearAccessToken |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 119 | #' @rdname KorAPConnection-class |
| 120 | #' @import keyring |
| 121 | #' @export |
| 122 | #' @examples |
| 123 | #' \dontrun{ |
Marc Kupietz | 6ae7605 | 2021-09-21 10:34:00 +0200 | [diff] [blame] | 124 | #' |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 125 | #' kco <- new("KorAPConnection") |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 126 | #' clearAccessToken(kco) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 127 | #' } |
| 128 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 129 | setMethod("clearAccessToken", "KorAPConnection", function(kco) { |
| 130 | key_delete(accessTokenServiceName, kco@KorAPUrl) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 131 | }) |
| 132 | |
| 133 | #' @import keyring |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 134 | getAccessToken <- function(KorAPUrl) { |
Marc Kupietz | 59e449b | 2019-12-12 12:53:54 +0100 | [diff] [blame] | 135 | keyList <- tryCatch(withCallingHandlers(key_list(service = accessTokenServiceName), |
Marc Kupietz | ddce556 | 2019-11-24 15:45:38 +0100 | [diff] [blame] | 136 | warning = function(w) invokeRestart("muffleWarning"), |
Marc Kupietz | 59e449b | 2019-12-12 12:53:54 +0100 | [diff] [blame] | 137 | error = function(e) return(NULL)), |
| 138 | error = function(e) { }) |
Marc Kupietz | 01c2477 | 2021-07-14 18:27:36 +0200 | [diff] [blame] | 139 | if (KorAPUrl %in% keyList$username) |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 140 | key_get(accessTokenServiceName, KorAPUrl) |
Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 141 | else |
| 142 | NULL |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 143 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 144 | |
Marc Kupietz | 581a29b | 2021-09-04 20:51:04 +0200 | [diff] [blame] | 145 | |
| 146 | warnIfNoAccessToken <- function(kco) { |
| 147 | if (is.null(kco@accessToken)) { |
| 148 | warning( |
| 149 | paste0( |
| 150 | "In order to receive KWICSs also from corpora with restricted licenses, you need an access token.\n", |
| 151 | "To generate an access token, login to KorAP and navigite to KorAP's OAuth settings <", |
| 152 | kco@KorAPUrl, |
| 153 | "settings/oauth#page-top>" |
| 154 | ) |
| 155 | ) |
| 156 | } |
| 157 | } |
| 158 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 159 | KorAPCacheSubDir <- function() { |
Marc Kupietz | 70b2c72 | 2020-02-18 13:32:09 +0100 | [diff] [blame] | 160 | paste0("RKorAPClient_", |
| 161 | gsub( |
| 162 | "^([0-9]+\\.[0-9]+).*", |
| 163 | "\\1", |
| 164 | packageVersion("RKorAPClient"), |
| 165 | perl = TRUE |
| 166 | )) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 169 | setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") ) |
| 170 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 171 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 172 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
| 173 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 174 | #' @aliases apiCall |
| 175 | #' @rdname KorAPConnection-class |
| 176 | #' @param kco KorAPConnection object |
| 177 | #' @param url request url |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 178 | #' @param json logical that determines if json result is expected |
Marc Kupietz | b49afa0 | 2020-06-04 15:50:29 +0200 | [diff] [blame] | 179 | #' @param getHeaders logical that determines if headers and content should be returned (as a list) |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 180 | #' @importFrom jsonlite fromJSON |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 181 | #' @importFrom curl has_internet |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 182 | #' @export |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 183 | setMethod("apiCall", "KorAPConnection", function(kco, url, json = TRUE, getHeaders = FALSE, cache = kco@cache, timeout=kco@timeout) { |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 184 | result <- "" |
| 185 | if (cache) { |
Marc Kupietz | b49afa0 | 2020-06-04 15:50:29 +0200 | [diff] [blame] | 186 | result <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url, kco@accessToken, kco@indexRevision)) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 187 | if (!is.null(result)) { |
| 188 | if (!is.null(result$meta)) |
| 189 | result$meta$cached <- "local" |
| 190 | return(result) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 193 | |
| 194 | # From https://community.rstudio.com/t/internet-resources-should-fail-gracefully/49199/11 |
| 195 | # Thanks to kvasilopoulos |
| 196 | try_GET <- function(x, ...) { |
| 197 | tryCatch( |
| 198 | GET(url = x, timeout(timeout), ...), |
| 199 | error = function(e) conditionMessage(e), |
| 200 | warning = function(w) conditionMessage(w) |
| 201 | ) |
| 202 | } |
| 203 | is_response <- function(x) { |
| 204 | class(x) == "response" |
| 205 | } |
| 206 | |
| 207 | # First check internet connection |
| 208 | if (!curl::has_internet()) { |
| 209 | message("No internet connection.") |
| 210 | return(invisible(NULL)) |
| 211 | } |
| 212 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame] | 213 | if (!is.null(kco@accessToken)) |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 214 | resp <- try_GET(url, user_agent(kco@userAgent), timeout(timeout), add_headers(Authorization = paste("Bearer", kco@accessToken))) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 215 | else |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 216 | resp <- try_GET(url, user_agent(kco@userAgent), timeout(timeout)) |
| 217 | |
| 218 | if (!is_response(resp)) { |
| 219 | message(resp) |
| 220 | return(invisible(NULL)) |
| 221 | } |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 222 | if (json || status_code(resp) != 200) { |
| 223 | if (json && !http_type(resp) %in% c("application/json", "application/ld+json")) { |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 224 | # message("API did not return json") |
| 225 | return(invisible(NULL)) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 226 | } |
| 227 | result <- jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8")) |
| 228 | if (!is.null(result$warnings)) { |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 229 | msg <- if (nrow(result$warnings) > 1) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 230 | sapply(result$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")) |
| 231 | else |
| 232 | sprintf("%s: %s", result$warnings[1], result$warnings[2]) |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 233 | message(msg) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 234 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 235 | } |
| 236 | if (status_code(resp) != 200) { |
Marc Kupietz | b7d8c27 | 2020-01-31 18:51:50 +0100 | [diff] [blame] | 237 | if (kco@verbose) { |
| 238 | cat("\n") |
| 239 | } |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 240 | msg <- sprintf("%s KorAP API request failed", status_code(resp)) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 241 | if (!is.null(result$errors)) { |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 242 | errormsg <- unlist(result$errors) |
| 243 | msg <- sprintf("%s: %s %s", msg, errormsg[5], errormsg[2]) |
Marc Kupietz | b7d8c27 | 2020-01-31 18:51:50 +0100 | [diff] [blame] | 244 | } |
Marc Kupietz | a467572 | 2022-02-23 23:55:15 +0100 | [diff] [blame] | 245 | message(msg) |
| 246 | return(invisible(NULL)) |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 247 | } |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 248 | if (!json) { |
| 249 | result <- content(resp, "text", encoding = "UTF-8") |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 250 | } |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 251 | if (cache) { |
Marc Kupietz | b49afa0 | 2020-06-04 15:50:29 +0200 | [diff] [blame] | 252 | R.cache::saveCache(result, key = list(url, kco@accessToken, kco@indexRevision), dir = KorAPCacheSubDir(), compress = TRUE) |
Marc Kupietz | b2b32a3 | 2020-03-24 13:56:50 +0100 | [diff] [blame] | 253 | } |
Marc Kupietz | b49afa0 | 2020-06-04 15:50:29 +0200 | [diff] [blame] | 254 | if (getHeaders) { |
| 255 | list(httr::headers(resp), result) |
| 256 | } else { |
| 257 | result |
| 258 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 259 | }) |
| 260 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 261 | setGeneric("clearCache", function(kco) standardGeneric("clearCache") ) |
| 262 | |
| 263 | #' @aliases clearCache |
| 264 | #' @rdname KorAPConnection-class |
| 265 | #' @export |
| 266 | setMethod("clearCache", "KorAPConnection", function(kco) { |
| 267 | R.cache::clearCache(dir=KorAPCacheSubDir()) |
| 268 | }) |
| 269 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 270 | #' @rdname KorAPConnection-class |
| 271 | #' @param object KorAPConnection object |
| 272 | #' @export |
| 273 | setMethod("show", "KorAPConnection", function(object) { |
| 274 | cat("<KorAPConnection>", "\n") |
| 275 | cat("apiUrl: ", object@apiUrl, "\n") |
| 276 | }) |
| 277 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 278 | ##' Funtion KorAPConnection() |
| 279 | ##' |
| 280 | ##' Wrappper function for new("KorAPConnection") |
| 281 | ##' |
| 282 | ##' @rdname KorAPConnection-constructor |
| 283 | ##' @name KorAPConnection-constructor |
| 284 | ##' @export |
| 285 | ## XKorAPConnection <- function(...) new("KorAPConnection", ...) |