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