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