Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 1 | #' Class KorAPConnection |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 2 | #' |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 3 | #' \code{KorAPConnection} objects represent the connection to a KorAP server. |
| 4 | #' New \code{KorAPConnection} objects can be created by \code{KorAPConnection()}. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 5 | #' |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 6 | #' @import R.cache |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 7 | #' @import utils |
| 8 | #' @import methods |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 9 | #' @export |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 10 | KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", userAgent="character", timeout="numeric", verbose="logical", cache="logical")) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 11 | |
| 12 | #' @param .Object KorAPConnection object |
| 13 | #' @param KorAPUrl the URL of the KorAP server instance you want to access. |
| 14 | #' @param apiVersion which version of KorAP's API you want to connect to. |
| 15 | #' @param apiUrl URL of the KorAP web service. |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 16 | #' @param userAgent user agent string. |
| 17 | #' @param timeout time out in seconds. |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 18 | #' @param verbose logical. Decides whether following operations will default to be verbose. |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 19 | #' @param cache logical. Decides if API calls are cached locally. You can clear the cache with \code{\link{clearCache}()}. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 20 | #' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}} |
| 21 | #' |
| 22 | #' @examples |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 23 | #' kcon <- new("KorAPConnection", verbose = TRUE) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 24 | #' kq <- corpusQuery(kcon, "Ameisenplage") |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 25 | #' kq <- fetchAll(kq) |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 26 | #' |
| 27 | #' @note Currently it is not possible to authenticate the client |
| 28 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 29 | #' @rdname KorAPConnection-class |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 30 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 31 | setMethod("initialize", "KorAPConnection", |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 32 | function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, userAgent = "R-KorAP-Client", timeout=10, verbose = FALSE, cache = TRUE) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 33 | .Object <- callNextMethod() |
| 34 | m <- regexpr("https?://[^?]+", KorAPUrl, perl = TRUE) |
| 35 | .Object@KorAPUrl <- regmatches(KorAPUrl, m) |
| 36 | if (!endsWith(.Object@KorAPUrl, '/')) { |
| 37 | .Object@KorAPUrl <- paste0(.Object@KorAPUrl, "/") |
| 38 | } |
| 39 | if (missing(apiUrl)) { |
| 40 | .Object@apiUrl = paste0(.Object@KorAPUrl, 'api/', apiVersion, '/') |
| 41 | } else { |
| 42 | .Object@apiUrl = apiUrl |
| 43 | } |
| 44 | .Object@apiVersion = apiVersion |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 45 | .Object@userAgent = userAgent |
| 46 | .Object@timeout = timeout |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 47 | .Object@verbose = verbose |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 48 | .Object@cache = cache |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 49 | .Object |
| 50 | }) |
| 51 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 52 | |
| 53 | KorAPCacheSubDir <- function() { |
| 54 | paste0("RKorAPClient_", packageVersion("RKorAPClient")) |
| 55 | } |
| 56 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 57 | setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") ) |
| 58 | |
Marc Kupietz | 4de53ec | 2019-10-04 09:12:00 +0200 | [diff] [blame] | 59 | ## quiets concerns of R CMD check re: the .'s that appear in pipelines |
| 60 | if(getRversion() >= "2.15.1") utils::globalVariables(c(".")) |
| 61 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 62 | #' @aliases apiCall |
| 63 | #' @rdname KorAPConnection-class |
| 64 | #' @param kco KorAPConnection object |
| 65 | #' @param url request url |
Marc Kupietz | 69cc54a | 2019-09-30 12:06:54 +0200 | [diff] [blame] | 66 | #' @importFrom jsonlite fromJSON |
| 67 | #' @export |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 68 | setMethod("apiCall", "KorAPConnection", function(kco, url) { |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 69 | if (kco@cache) { |
| 70 | parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url)) |
| 71 | if (!is.null(parsed)) { |
| 72 | return(parsed) |
| 73 | } |
| 74 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 75 | resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout)) |
Marc Kupietz | 46a5767 | 2019-09-27 18:11:31 +0200 | [diff] [blame] | 76 | if (!http_type(resp) %in% c("application/json", "application/ld+json")) { |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 77 | stop("API did not return json", call. = FALSE) |
| 78 | } |
| 79 | parsed <- jsonlite::fromJSON(content(resp, "text")) |
| 80 | if (!is.null(parsed$warnings)) { |
| 81 | message <- ifelse (nrow(parsed$warnings) > 1, |
| 82 | sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")), |
| 83 | sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2])) |
| 84 | warning(message, call. = FALSE) |
| 85 | } |
| 86 | if (status_code(resp) != 200) { |
| 87 | message <- ifelse (!is.null(parsed$errors), |
Akron | 36def51 | 2019-09-27 17:30:51 +0200 | [diff] [blame] | 88 | sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))), |
| 89 | message <- sprintf("%s: KorAP API request failed.", status_code(resp))) |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 90 | stop(message, call. = FALSE) |
| 91 | } |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 92 | if (kco@cache) { |
| 93 | R.cache::saveCache(parsed, key = list(url), dir = KorAPCacheSubDir(), compress = TRUE) |
| 94 | } |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 95 | parsed |
| 96 | }) |
| 97 | |
Marc Kupietz | 0a96b28 | 2019-10-01 11:05:31 +0200 | [diff] [blame] | 98 | setGeneric("clearCache", function(kco) standardGeneric("clearCache") ) |
| 99 | |
| 100 | #' @aliases clearCache |
| 101 | #' @rdname KorAPConnection-class |
| 102 | #' @export |
| 103 | setMethod("clearCache", "KorAPConnection", function(kco) { |
| 104 | R.cache::clearCache(dir=KorAPCacheSubDir()) |
| 105 | }) |
| 106 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 107 | #' @rdname KorAPConnection-class |
| 108 | #' @param object KorAPConnection object |
| 109 | #' @export |
| 110 | setMethod("show", "KorAPConnection", function(object) { |
| 111 | cat("<KorAPConnection>", "\n") |
| 112 | cat("apiUrl: ", object@apiUrl, "\n") |
| 113 | }) |
| 114 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 115 | ##' Funtion KorAPConnection() |
| 116 | ##' |
| 117 | ##' Wrappper function for new("KorAPConnection") |
| 118 | ##' |
| 119 | ##' @rdname KorAPConnection-constructor |
| 120 | ##' @name KorAPConnection-constructor |
| 121 | ##' @export |
| 122 | ## XKorAPConnection <- function(...) new("KorAPConnection", ...) |