blob: 7eface59a6b2c70ec6e5c83501cfe529ea7823be [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 jsonlite
8#' @import utils
9#' @import methods
10#'
11#'
12
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
63#' @aliases apiCall
64#' @rdname KorAPConnection-class
65#' @param kco KorAPConnection object
66#' @param url request url
67setMethod("apiCall", "KorAPConnection", function(kco, url) {
Marc Kupietz0a96b282019-10-01 11:05:31 +020068 if (kco@cache) {
69 parsed <- R.cache::loadCache(dir=KorAPCacheSubDir(), key=list(url))
70 if (!is.null(parsed)) {
71 return(parsed)
72 }
73 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020074 resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
Marc Kupietz46a57672019-09-27 18:11:31 +020075 if (!http_type(resp) %in% c("application/json", "application/ld+json")) {
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020076 stop("API did not return json", call. = FALSE)
77 }
78 parsed <- jsonlite::fromJSON(content(resp, "text"))
79 if (!is.null(parsed$warnings)) {
80 message <- ifelse (nrow(parsed$warnings) > 1,
81 sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")),
82 sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2]))
83 warning(message, call. = FALSE)
84 }
85 if (status_code(resp) != 200) {
86 message <- ifelse (!is.null(parsed$errors),
Akron36def512019-09-27 17:30:51 +020087 sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))),
88 message <- sprintf("%s: KorAP API request failed.", status_code(resp)))
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020089 stop(message, call. = FALSE)
90 }
Marc Kupietz0a96b282019-10-01 11:05:31 +020091 if (kco@cache) {
92 R.cache::saveCache(parsed, key = list(url), dir = KorAPCacheSubDir(), compress = TRUE)
93 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020094 parsed
95})
96
Marc Kupietz0a96b282019-10-01 11:05:31 +020097setGeneric("clearCache", function(kco) standardGeneric("clearCache") )
98
99#' @aliases clearCache
100#' @rdname KorAPConnection-class
101#' @export
102setMethod("clearCache", "KorAPConnection", function(kco) {
103 R.cache::clearCache(dir=KorAPCacheSubDir())
104})
105
Marc Kupietze95108e2019-09-18 13:23:58 +0200106#' @rdname KorAPConnection-class
107#' @param object KorAPConnection object
108#' @export
109setMethod("show", "KorAPConnection", function(object) {
110 cat("<KorAPConnection>", "\n")
111 cat("apiUrl: ", object@apiUrl, "\n")
112})
113
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200114##' Funtion KorAPConnection()
115##'
116##' Wrappper function for new("KorAPConnection")
117##'
118##' @rdname KorAPConnection-constructor
119##' @name KorAPConnection-constructor
120##' @export
121## XKorAPConnection <- function(...) new("KorAPConnection", ...)