blob: 85d899739f05822d103f0c1e3f72c48523121c49 [file] [log] [blame]
Marc Kupietze95108e2019-09-18 13:23:58 +02001#' Class KorAPConnection
Marc Kupietz25aebc32019-09-16 18:40:50 +02002#'
Akron5e135462019-09-27 16:31:38 +02003#' \code{KorAPConnection} objects represent the connection to a KorAP server.
4#' New \code{KorAPConnection} objects can be created by \code{KorAPConnection()}.
Marc Kupietze95108e2019-09-18 13:23:58 +02005#'
Marc Kupietz0a96b282019-10-01 11:05:31 +02006#' @import R.cache
Marc Kupietze95108e2019-09-18 13:23:58 +02007#' @import utils
8#' @import methods
Marc Kupietz69cc54a2019-09-30 12:06:54 +02009#' @import dplyr
10#' @import purrr
11#' @import tidyr
Marc Kupietze95108e2019-09-18 13:23:58 +020012
13#' @export
Marc Kupietz0a96b282019-10-01 11:05:31 +020014KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", userAgent="character", timeout="numeric", verbose="logical", cache="logical"))
Marc Kupietze95108e2019-09-18 13:23:58 +020015
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 Kupietzd0d3e9b2019-09-24 17:36:03 +020020#' @param userAgent user agent string.
21#' @param timeout time out in seconds.
Akron5e135462019-09-27 16:31:38 +020022#' @param verbose logical. Decides whether following operations will default to be verbose.
Marc Kupietz0a96b282019-10-01 11:05:31 +020023#' @param cache logical. Decides if API calls are cached locally. You can clear the cache with \code{\link{clearCache}()}.
Marc Kupietze95108e2019-09-18 13:23:58 +020024#' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}}
25#'
26#' @examples
Marc Kupietz5a519822019-09-20 21:43:52 +020027#' kcon <- new("KorAPConnection", verbose = TRUE)
Marc Kupietze95108e2019-09-18 13:23:58 +020028#' kq <- corpusQuery(kcon, "Ameisenplage")
Marc Kupietz5a519822019-09-20 21:43:52 +020029#' kq <- fetchAll(kq)
Marc Kupietz7915dc42019-09-12 17:44:58 +020030#'
31#' @note Currently it is not possible to authenticate the client
32#'
Marc Kupietze95108e2019-09-18 13:23:58 +020033#' @rdname KorAPConnection-class
Marc Kupietz632cbd42019-09-06 16:04:51 +020034#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +020035setMethod("initialize", "KorAPConnection",
Marc Kupietz0a96b282019-10-01 11:05:31 +020036 function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, userAgent = "R-KorAP-Client", timeout=10, verbose = FALSE, cache = TRUE) {
Marc Kupietze95108e2019-09-18 13:23:58 +020037 .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 Kupietzd0d3e9b2019-09-24 17:36:03 +020049 .Object@userAgent = userAgent
50 .Object@timeout = timeout
Marc Kupietz5a519822019-09-20 21:43:52 +020051 .Object@verbose = verbose
Marc Kupietz0a96b282019-10-01 11:05:31 +020052 .Object@cache = cache
Marc Kupietze95108e2019-09-18 13:23:58 +020053 .Object
54 })
55
Marc Kupietz0a96b282019-10-01 11:05:31 +020056
57KorAPCacheSubDir <- function() {
58 paste0("RKorAPClient_", packageVersion("RKorAPClient"))
59}
60
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020061setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") )
62
Marc Kupietz4de53ec2019-10-04 09:12:00 +020063## quiets concerns of R CMD check re: the .'s that appear in pipelines
64if(getRversion() >= "2.15.1") utils::globalVariables(c("."))
65
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020066#' @aliases apiCall
67#' @rdname KorAPConnection-class
68#' @param kco KorAPConnection object
69#' @param url request url
Marc Kupietz69cc54a2019-09-30 12:06:54 +020070#' @importFrom jsonlite fromJSON
71#' @export
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020072setMethod("apiCall", "KorAPConnection", function(kco, url) {
Marc Kupietz0a96b282019-10-01 11:05:31 +020073 if (kco@cache) {
74 parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url))
75 if (!is.null(parsed)) {
76 return(parsed)
77 }
78 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020079 resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
Marc Kupietz46a57672019-09-27 18:11:31 +020080 if (!http_type(resp) %in% c("application/json", "application/ld+json")) {
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020081 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),
Akron36def512019-09-27 17:30:51 +020092 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 Kupietzd0d3e9b2019-09-24 17:36:03 +020094 stop(message, call. = FALSE)
95 }
Marc Kupietz0a96b282019-10-01 11:05:31 +020096 if (kco@cache) {
97 R.cache::saveCache(parsed, key = list(url), dir = KorAPCacheSubDir(), compress = TRUE)
98 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020099 parsed
100})
101
Marc Kupietz0a96b282019-10-01 11:05:31 +0200102setGeneric("clearCache", function(kco) standardGeneric("clearCache") )
103
104#' @aliases clearCache
105#' @rdname KorAPConnection-class
106#' @export
107setMethod("clearCache", "KorAPConnection", function(kco) {
108 R.cache::clearCache(dir=KorAPCacheSubDir())
109})
110
Marc Kupietze95108e2019-09-18 13:23:58 +0200111#' @rdname KorAPConnection-class
112#' @param object KorAPConnection object
113#' @export
114setMethod("show", "KorAPConnection", function(object) {
115 cat("<KorAPConnection>", "\n")
116 cat("apiUrl: ", object@apiUrl, "\n")
117})
118
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200119##' 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", ...)