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