blob: 013e5b8de143510b7f3833bf7359e6756bda9ee8 [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 Kupietze95108e2019-09-18 13:23:58 +02009#' @export
Marc Kupietz0a96b282019-10-01 11:05:31 +020010KorAPConnection <- 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 +020011
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 Kupietzd0d3e9b2019-09-24 17:36:03 +020016#' @param userAgent user agent string.
17#' @param timeout time out in seconds.
Akron5e135462019-09-27 16:31:38 +020018#' @param verbose logical. Decides whether following operations will default to be verbose.
Marc Kupietz0a96b282019-10-01 11:05:31 +020019#' @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 +020020#' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}}
21#'
22#' @examples
Marc Kupietz5a519822019-09-20 21:43:52 +020023#' kcon <- new("KorAPConnection", verbose = TRUE)
Marc Kupietze95108e2019-09-18 13:23:58 +020024#' kq <- corpusQuery(kcon, "Ameisenplage")
Marc Kupietz5a519822019-09-20 21:43:52 +020025#' kq <- fetchAll(kq)
Marc Kupietz7915dc42019-09-12 17:44:58 +020026#'
27#' @note Currently it is not possible to authenticate the client
28#'
Marc Kupietze95108e2019-09-18 13:23:58 +020029#' @rdname KorAPConnection-class
Marc Kupietz632cbd42019-09-06 16:04:51 +020030#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +020031setMethod("initialize", "KorAPConnection",
Marc Kupietz0a96b282019-10-01 11:05:31 +020032 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 +020033 .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 Kupietzd0d3e9b2019-09-24 17:36:03 +020045 .Object@userAgent = userAgent
46 .Object@timeout = timeout
Marc Kupietz5a519822019-09-20 21:43:52 +020047 .Object@verbose = verbose
Marc Kupietz0a96b282019-10-01 11:05:31 +020048 .Object@cache = cache
Marc Kupietze95108e2019-09-18 13:23:58 +020049 .Object
50 })
51
Marc Kupietz0a96b282019-10-01 11:05:31 +020052
53KorAPCacheSubDir <- function() {
54 paste0("RKorAPClient_", packageVersion("RKorAPClient"))
55}
56
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020057setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") )
58
Marc Kupietz4de53ec2019-10-04 09:12:00 +020059## quiets concerns of R CMD check re: the .'s that appear in pipelines
60if(getRversion() >= "2.15.1") utils::globalVariables(c("."))
61
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020062#' @aliases apiCall
63#' @rdname KorAPConnection-class
64#' @param kco KorAPConnection object
65#' @param url request url
Marc Kupietz69cc54a2019-09-30 12:06:54 +020066#' @importFrom jsonlite fromJSON
67#' @export
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020068setMethod("apiCall", "KorAPConnection", function(kco, url) {
Marc Kupietz0a96b282019-10-01 11:05:31 +020069 if (kco@cache) {
70 parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url))
71 if (!is.null(parsed)) {
72 return(parsed)
73 }
74 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020075 resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
Marc Kupietz46a57672019-09-27 18:11:31 +020076 if (!http_type(resp) %in% c("application/json", "application/ld+json")) {
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020077 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),
Akron36def512019-09-27 17:30:51 +020088 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 Kupietzd0d3e9b2019-09-24 17:36:03 +020090 stop(message, call. = FALSE)
91 }
Marc Kupietz0a96b282019-10-01 11:05:31 +020092 if (kco@cache) {
93 R.cache::saveCache(parsed, key = list(url), dir = KorAPCacheSubDir(), compress = TRUE)
94 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020095 parsed
96})
97
Marc Kupietz0a96b282019-10-01 11:05:31 +020098setGeneric("clearCache", function(kco) standardGeneric("clearCache") )
99
100#' @aliases clearCache
101#' @rdname KorAPConnection-class
102#' @export
103setMethod("clearCache", "KorAPConnection", function(kco) {
104 R.cache::clearCache(dir=KorAPCacheSubDir())
105})
106
Marc Kupietze95108e2019-09-18 13:23:58 +0200107#' @rdname KorAPConnection-class
108#' @param object KorAPConnection object
109#' @export
110setMethod("show", "KorAPConnection", function(object) {
111 cat("<KorAPConnection>", "\n")
112 cat("apiUrl: ", object@apiUrl, "\n")
113})
114
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200115##' 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", ...)