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")) |
| 5 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 6 | #' Class KorAPConnection |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 7 | #' |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 8 | #' \code{KorAPConnection} objects represent the connection to a KorAP server. |
Marc Kupietz | 7715e9d | 2019-11-08 15:59:58 +0100 | [diff] [blame] | 9 | #' New \code{KorAPConnection} objects can be created by \code{new("KorAPConnection")}. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 10 | #' |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 11 | #' @import R.cache |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 12 | #' @import utils |
| 13 | #' @import methods |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 14 | #' @export |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 15 | KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", accessToken="characterOrNULL", userAgent="character", timeout="numeric", verbose="logical", cache="logical")) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 16 | |
| 17 | #' @param .Object KorAPConnection object |
| 18 | #' @param KorAPUrl the URL of the KorAP server instance you want to access. |
| 19 | #' @param apiVersion which version of KorAP's API you want to connect to. |
| 20 | #' @param apiUrl URL of the KorAP web service. |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 21 | #' @param accessToken OAuth2 access token. To use authorization based on an access token |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 22 | #' in subsequent queries, intialize your KorAP connection with |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 23 | #' \code{kco <- new("KorAPConnection", accessToken="<access token>")}. |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 24 | #' In order to make the API |
| 25 | #' token persistent for the currently used \code{KorAPUrl} (you can have one |
| 26 | #' token per KorAPUrl / KorAP server instance), use |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 27 | #' \code{persistAccessToken(kco)}. This will store it in your keyring using the |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 28 | #' \code{\link{keyring}} package. Subsequent new("KorAPConnection") calls will |
| 29 | #' then automatically retrieve the token from your keying. To stop using a |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 30 | #' persisted token, call \code{clearAccessToken(kco)}. Please note that for |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 31 | #' DeReKo, authorized queries will behave differently inside and outside the |
| 32 | #' IDS, because of the special license situation. This concerns also cached |
| 33 | #' results which do not take into account from where a request was issued. If |
| 34 | #' you experience problems or unexpected results, please try \code{kco <- |
| 35 | #' new("KorAPConnection", cache=FALSE)} or use |
| 36 | #' \code{\link{clearCache}} to clear the cache completely. |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 37 | #' @param userAgent user agent string. |
| 38 | #' @param timeout time out in seconds. |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 39 | #' @param verbose logical. Decides whether following operations will default to |
| 40 | #' be verbose. |
| 41 | #' @param cache logical. Decides if API calls are cached locally. You can clear |
| 42 | #' the cache with \code{\link{clearCache}()}. |
| 43 | #' @return \code{\link{KorAPConnection}} object that can be used e.g. with |
| 44 | #' \code{\link{corpusQuery}} |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 45 | #' |
| 46 | #' @examples |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 47 | #' kcon <- new("KorAPConnection", verbose = TRUE) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 48 | #' kq <- corpusQuery(kcon, "Ameisenplage") |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 49 | #' kq <- fetchAll(kq) |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 50 | #' |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 51 | #' \dontrun{ |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 52 | #' kcon <- new("KorAPConnection", verbose = TRUE, accessToken="e739u6eOzkwADQPdVChxFg") |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 53 | #' kq <- corpusQuery(kcon, "Ameisenplage", metadataOnly=FALSE) |
| 54 | #' kq <- fetchAll(kq) |
| 55 | #' kq@collectedMatches$snippet |
| 56 | #' } |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 57 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 58 | #' @rdname KorAPConnection-class |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 59 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 60 | setMethod("initialize", "KorAPConnection", |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 61 | function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, accessToken = getAccessToken(KorAPUrl), userAgent = "R-KorAP-Client", timeout=10, verbose = FALSE, cache = TRUE) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 62 | .Object <- callNextMethod() |
| 63 | m <- regexpr("https?://[^?]+", KorAPUrl, perl = TRUE) |
| 64 | .Object@KorAPUrl <- regmatches(KorAPUrl, m) |
| 65 | if (!endsWith(.Object@KorAPUrl, '/')) { |
| 66 | .Object@KorAPUrl <- paste0(.Object@KorAPUrl, "/") |
| 67 | } |
| 68 | if (missing(apiUrl)) { |
| 69 | .Object@apiUrl = paste0(.Object@KorAPUrl, 'api/', apiVersion, '/') |
| 70 | } else { |
| 71 | .Object@apiUrl = apiUrl |
| 72 | } |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 73 | .Object@accessToken = accessToken |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 74 | .Object@apiVersion = apiVersion |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 75 | .Object@userAgent = userAgent |
| 76 | .Object@timeout = timeout |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 77 | .Object@verbose = verbose |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 78 | .Object@cache = cache |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 79 | .Object |
| 80 | }) |
| 81 | |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 82 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 83 | accessTokenServiceName <- "RKorAPClientAccessToken" |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 84 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 85 | setGeneric("persistAccessToken", function(kco, ...) standardGeneric("persistAccessToken") ) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 86 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 87 | #' @aliases persistAccessToken |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 88 | #' @rdname KorAPConnection-class |
| 89 | #' @import keyring |
| 90 | #' @export |
| 91 | #' @examples |
| 92 | #' \dontrun{ |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 93 | #' kco <- new("KorAPConnection", accessToken="e739u6eOzkwADQPdVChxFg") |
| 94 | #' persistAccessToken(kco) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 95 | #' } |
| 96 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 97 | setMethod("persistAccessToken", "KorAPConnection", function(kco, accessToken = kco@accessToken) { |
| 98 | if (is.null(accessToken)) |
| 99 | 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] | 100 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 101 | kco@accessToken <- accessToken |
| 102 | key_set_with_value(accessTokenServiceName, kco@KorAPUrl, accessToken) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 103 | }) |
| 104 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 105 | setGeneric("clearAccessToken", function(kco) standardGeneric("clearAccessToken") ) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 106 | |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 107 | #' @aliases clearAccessToken |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 108 | #' @rdname KorAPConnection-class |
| 109 | #' @import keyring |
| 110 | #' @export |
| 111 | #' @examples |
| 112 | #' \dontrun{ |
| 113 | #' kco <- new("KorAPConnection") |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 114 | #' clearAccessToken(kco) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 115 | #' } |
| 116 | #' |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 117 | setMethod("clearAccessToken", "KorAPConnection", function(kco) { |
| 118 | key_delete(accessTokenServiceName, kco@KorAPUrl) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 119 | }) |
| 120 | |
| 121 | #' @import keyring |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 122 | getAccessToken <- function(KorAPUrl) { |
| 123 | keyList <- withCallingHandlers(key_list(service = accessTokenServiceName), |
Marc Kupietz | ddce556 | 2019-11-24 15:45:38 +0100 | [diff] [blame] | 124 | warning = function(w) invokeRestart("muffleWarning"), |
| 125 | error = function(e) return(NULL)) |
Marc Kupietz | b2870f2 | 2019-11-20 22:28:34 +0100 | [diff] [blame] | 126 | if (KorAPUrl %in% keyList) |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 127 | key_get(accessTokenServiceName, KorAPUrl) |
Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 128 | else |
| 129 | NULL |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 130 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 131 | |
| 132 | KorAPCacheSubDir <- function() { |
| 133 | paste0("RKorAPClient_", packageVersion("RKorAPClient")) |
| 134 | } |
| 135 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 136 | setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") ) |
| 137 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 138 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 139 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
| 140 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 141 | #' @aliases apiCall |
| 142 | #' @rdname KorAPConnection-class |
| 143 | #' @param kco KorAPConnection object |
| 144 | #' @param url request url |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 145 | #' @importFrom jsonlite fromJSON |
| 146 | #' @export |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 147 | setMethod("apiCall", "KorAPConnection", function(kco, url) { |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 148 | if (kco@cache) { |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 149 | parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url, kco@accessToken)) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 150 | if (!is.null(parsed)) { |
| 151 | return(parsed) |
| 152 | } |
| 153 | } |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 154 | if (!is.null(kco@accessToken)) |
| 155 | resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout), add_headers(Authorization = paste("Bearer", kco@accessToken))) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 156 | else |
| 157 | resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout)) |
Marc Kupietz | 46a5767 | 2019-09-27 18:11:31 +0200 | [diff] [blame] | 158 | if (!http_type(resp) %in% c("application/json", "application/ld+json")) { |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 159 | stop("API did not return json", call. = FALSE) |
| 160 | } |
| 161 | parsed <- jsonlite::fromJSON(content(resp, "text")) |
| 162 | if (!is.null(parsed$warnings)) { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 163 | message <- if (nrow(parsed$warnings) > 1) |
| 164 | sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")) |
| 165 | else |
| 166 | sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2]) |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 167 | warning(message, call. = FALSE) |
| 168 | } |
| 169 | if (status_code(resp) != 200) { |
Marc Kupietz | a96537f | 2019-11-09 23:07:44 +0100 | [diff] [blame] | 170 | message <- if (!is.null(parsed$errors)) |
| 171 | sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))) |
| 172 | else |
| 173 | message <- sprintf("%s: KorAP API request failed.", status_code(resp)) |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 174 | stop(message, call. = FALSE) |
| 175 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 176 | if (kco@cache) { |
Marc Kupietz | b956b81 | 2019-11-25 17:53:13 +0100 | [diff] [blame^] | 177 | R.cache::saveCache(parsed, key = list(url, kco@accessToken), dir = KorAPCacheSubDir(), compress = TRUE) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 178 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 179 | parsed |
| 180 | }) |
| 181 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 182 | setGeneric("clearCache", function(kco) standardGeneric("clearCache") ) |
| 183 | |
| 184 | #' @aliases clearCache |
| 185 | #' @rdname KorAPConnection-class |
| 186 | #' @export |
| 187 | setMethod("clearCache", "KorAPConnection", function(kco) { |
| 188 | R.cache::clearCache(dir=KorAPCacheSubDir()) |
| 189 | }) |
| 190 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 191 | #' @rdname KorAPConnection-class |
| 192 | #' @param object KorAPConnection object |
| 193 | #' @export |
| 194 | setMethod("show", "KorAPConnection", function(object) { |
| 195 | cat("<KorAPConnection>", "\n") |
| 196 | cat("apiUrl: ", object@apiUrl, "\n") |
| 197 | }) |
| 198 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 199 | ##' Funtion KorAPConnection() |
| 200 | ##' |
| 201 | ##' Wrappper function for new("KorAPConnection") |
| 202 | ##' |
| 203 | ##' @rdname KorAPConnection-constructor |
| 204 | ##' @name KorAPConnection-constructor |
| 205 | ##' @export |
| 206 | ## XKorAPConnection <- function(...) new("KorAPConnection", ...) |