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