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 | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 15 | KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", apiToken="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 | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 21 | #' @param apiToken OAuth2 API token. To use authorization based on an API token |
| 22 | #' in subsequent queries, intialize your KorAP connection with |
| 23 | #' \code{kco <- new("KorAPConnection", apiToken="<API Token>")}. |
| 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 |
| 27 | #' \code{persistApiToken(kco)}. This will store it in your keyring using the |
| 28 | #' \code{\link{keyring}} package. Subsequent new("KorAPConnection") calls will |
| 29 | #' then automatically retrieve the token from your keying. To stop using a |
| 30 | #' persisted token, call \code{clearApiToken(kco)}. Please note that for |
| 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{ |
| 52 | #' kcon <- new("KorAPConnection", verbose = TRUE, apiToken="e739u6eOzkwADQPdVChxFg") |
| 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 | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 61 | function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, apiToken = getApiToken(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 | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 73 | .Object@apiToken = apiToken |
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 | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 82 | apiTokenServiceName <- "RKorAPClientAPIToken" |
| 83 | |
| 84 | setGeneric("persistApiToken", function(kco, apiToken) standardGeneric("persistApiToken") ) |
| 85 | |
| 86 | #' @aliases persistApiToken |
| 87 | #' @rdname KorAPConnection-class |
| 88 | #' @import keyring |
| 89 | #' @export |
| 90 | #' @examples |
| 91 | #' \dontrun{ |
| 92 | #' kco <- new("KorAPConnection", apiToken="e739u6eOzkwADQPdVChxFg") |
| 93 | #' persistApiToken(kco) |
| 94 | #' } |
| 95 | #' |
| 96 | setMethod("persistApiToken", "KorAPConnection", function(kco, apiToken = kco@apiToken) { |
| 97 | if (is.null(apiToken)) |
| 98 | stop("It seems that you have not supplied any API token that could be persisted.", call. = FALSE) |
| 99 | |
| 100 | kco@apiToken <- apiToken |
| 101 | key_set_with_value(apiTokenServiceName, kco@KorAPUrl, apiToken) |
| 102 | }) |
| 103 | |
| 104 | setGeneric("clearApiToken", function(kco) standardGeneric("clearApiToken") ) |
| 105 | |
| 106 | #' @aliases clearApiToken |
| 107 | #' @rdname KorAPConnection-class |
| 108 | #' @import keyring |
| 109 | #' @export |
| 110 | #' @examples |
| 111 | #' \dontrun{ |
| 112 | #' kco <- new("KorAPConnection") |
| 113 | #' clearApiToken(kco) |
| 114 | #' } |
| 115 | #' |
| 116 | setMethod("clearApiToken", "KorAPConnection", function(kco) { |
Marc Kupietz | 1057b6e | 2019-11-08 16:01:12 +0100 | [diff] [blame] | 117 | key_delete(apiTokenServiceName, kco@KorAPUrl) |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 118 | }) |
| 119 | |
| 120 | #' @import keyring |
| 121 | getApiToken <- function(KorAPUrl) { |
Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 122 | if ("keyring" %in% installed.packages()[,1 ]&& has_keyring_support() |
Marc Kupietz | 1057b6e | 2019-11-08 16:01:12 +0100 | [diff] [blame] | 123 | && KorAPUrl %in% key_list(service = apiTokenServiceName)) |
| 124 | key_get(apiTokenServiceName, KorAPUrl) |
Marc Kupietz | fd9e749 | 2019-11-08 15:45:18 +0100 | [diff] [blame] | 125 | else |
| 126 | NULL |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 127 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 128 | |
| 129 | KorAPCacheSubDir <- function() { |
| 130 | paste0("RKorAPClient_", packageVersion("RKorAPClient")) |
| 131 | } |
| 132 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 133 | setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") ) |
| 134 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 135 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 136 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
| 137 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 138 | #' @aliases apiCall |
| 139 | #' @rdname KorAPConnection-class |
| 140 | #' @param kco KorAPConnection object |
| 141 | #' @param url request url |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 142 | #' @importFrom jsonlite fromJSON |
| 143 | #' @export |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 144 | setMethod("apiCall", "KorAPConnection", function(kco, url) { |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 145 | if (kco@cache) { |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 146 | parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url, kco@apiToken)) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 147 | if (!is.null(parsed)) { |
| 148 | return(parsed) |
| 149 | } |
| 150 | } |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 151 | if (!is.null(kco@apiToken)) |
| 152 | resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout), add_headers(Authorization = paste("Bearer", kco@apiToken))) |
| 153 | else |
| 154 | resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout)) |
Marc Kupietz | 46a5767 | 2019-09-27 18:11:31 +0200 | [diff] [blame] | 155 | if (!http_type(resp) %in% c("application/json", "application/ld+json")) { |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 156 | stop("API did not return json", call. = FALSE) |
| 157 | } |
| 158 | parsed <- jsonlite::fromJSON(content(resp, "text")) |
| 159 | if (!is.null(parsed$warnings)) { |
| 160 | message <- ifelse (nrow(parsed$warnings) > 1, |
| 161 | sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")), |
| 162 | sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2])) |
| 163 | warning(message, call. = FALSE) |
| 164 | } |
| 165 | if (status_code(resp) != 200) { |
| 166 | message <- ifelse (!is.null(parsed$errors), |
Akron | 36def51 | 2019-09-27 17:30:51 +0200 | [diff] [blame] | 167 | sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))), |
| 168 | message <- sprintf("%s: KorAP API request failed.", status_code(resp))) |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 169 | stop(message, call. = FALSE) |
| 170 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 171 | if (kco@cache) { |
Marc Kupietz | 4862b86 | 2019-11-07 10:13:53 +0100 | [diff] [blame] | 172 | R.cache::saveCache(parsed, key = list(url, kco@apiToken), dir = KorAPCacheSubDir(), compress = TRUE) |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 173 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 174 | parsed |
| 175 | }) |
| 176 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 177 | setGeneric("clearCache", function(kco) standardGeneric("clearCache") ) |
| 178 | |
| 179 | #' @aliases clearCache |
| 180 | #' @rdname KorAPConnection-class |
| 181 | #' @export |
| 182 | setMethod("clearCache", "KorAPConnection", function(kco) { |
| 183 | R.cache::clearCache(dir=KorAPCacheSubDir()) |
| 184 | }) |
| 185 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 186 | #' @rdname KorAPConnection-class |
| 187 | #' @param object KorAPConnection object |
| 188 | #' @export |
| 189 | setMethod("show", "KorAPConnection", function(object) { |
| 190 | cat("<KorAPConnection>", "\n") |
| 191 | cat("apiUrl: ", object@apiUrl, "\n") |
| 192 | }) |
| 193 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 194 | ##' Funtion KorAPConnection() |
| 195 | ##' |
| 196 | ##' Wrappper function for new("KorAPConnection") |
| 197 | ##' |
| 198 | ##' @rdname KorAPConnection-constructor |
| 199 | ##' @name KorAPConnection-constructor |
| 200 | ##' @export |
| 201 | ## XKorAPConnection <- function(...) new("KorAPConnection", ...) |