blob: fa8f1f5aed0a8ec3a6cbeed76b7ac8f4bccd9d59 [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
14#' @export
Marc Kupietz0a96b282019-10-01 11:05:31 +020015KorAPConnection <- 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 +020016
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 Kupietzd0d3e9b2019-09-24 17:36:03 +020021#' @param userAgent user agent string.
22#' @param timeout time out in seconds.
Akron5e135462019-09-27 16:31:38 +020023#' @param verbose logical. Decides whether following operations will default to be verbose.
Marc Kupietz0a96b282019-10-01 11:05:31 +020024#' @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 +020025#' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}}
26#'
27#' @examples
Marc Kupietz5a519822019-09-20 21:43:52 +020028#' kcon <- new("KorAPConnection", verbose = TRUE)
Marc Kupietze95108e2019-09-18 13:23:58 +020029#' kq <- corpusQuery(kcon, "Ameisenplage")
Marc Kupietz5a519822019-09-20 21:43:52 +020030#' kq <- fetchAll(kq)
Marc Kupietz7915dc42019-09-12 17:44:58 +020031#'
32#' @note Currently it is not possible to authenticate the client
33#'
Marc Kupietze95108e2019-09-18 13:23:58 +020034#' @rdname KorAPConnection-class
Marc Kupietz632cbd42019-09-06 16:04:51 +020035#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +020036setMethod("initialize", "KorAPConnection",
Marc Kupietz0a96b282019-10-01 11:05:31 +020037 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 +020038 .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 Kupietzd0d3e9b2019-09-24 17:36:03 +020050 .Object@userAgent = userAgent
51 .Object@timeout = timeout
Marc Kupietz5a519822019-09-20 21:43:52 +020052 .Object@verbose = verbose
Marc Kupietz0a96b282019-10-01 11:05:31 +020053 .Object@cache = cache
Marc Kupietze95108e2019-09-18 13:23:58 +020054 .Object
55 })
56
Marc Kupietz0a96b282019-10-01 11:05:31 +020057
58KorAPCacheSubDir <- function() {
59 paste0("RKorAPClient_", packageVersion("RKorAPClient"))
60}
61
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020062setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") )
63
64#' @aliases apiCall
65#' @rdname KorAPConnection-class
66#' @param kco KorAPConnection object
67#' @param url request url
Marc Kupietz69cc54a2019-09-30 12:06:54 +020068#' @importFrom jsonlite fromJSON
69#' @export
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020070setMethod("apiCall", "KorAPConnection", function(kco, url) {
Marc Kupietz0a96b282019-10-01 11:05:31 +020071 if (kco@cache) {
72 parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url))
73 if (!is.null(parsed)) {
74 return(parsed)
75 }
76 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020077 resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
Marc Kupietz46a57672019-09-27 18:11:31 +020078 if (!http_type(resp) %in% c("application/json", "application/ld+json")) {
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020079 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),
Akron36def512019-09-27 17:30:51 +020090 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 Kupietzd0d3e9b2019-09-24 17:36:03 +020092 stop(message, call. = FALSE)
93 }
Marc Kupietz0a96b282019-10-01 11:05:31 +020094 if (kco@cache) {
95 R.cache::saveCache(parsed, key = list(url), dir = KorAPCacheSubDir(), compress = TRUE)
96 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020097 parsed
98})
99
Marc Kupietz0a96b282019-10-01 11:05:31 +0200100setGeneric("clearCache", function(kco) standardGeneric("clearCache") )
101
102#' @aliases clearCache
103#' @rdname KorAPConnection-class
104#' @export
105setMethod("clearCache", "KorAPConnection", function(kco) {
106 R.cache::clearCache(dir=KorAPCacheSubDir())
107})
108
Marc Kupietze95108e2019-09-18 13:23:58 +0200109#' @rdname KorAPConnection-class
110#' @param object KorAPConnection object
111#' @export
112setMethod("show", "KorAPConnection", function(object) {
113 cat("<KorAPConnection>", "\n")
114 cat("apiUrl: ", object@apiUrl, "\n")
115})
116
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200117##' 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", ...)