blob: d511d1bf5ad59c5c1f338cbe6666d0fb02d51156 [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#'
6#' @import jsonlite
7#' @import utils
8#' @import methods
9#'
10#'
11
12#' @export
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020013KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", userAgent="character", timeout="numeric", verbose="logical"))
Marc Kupietze95108e2019-09-18 13:23:58 +020014
15#' @param .Object KorAPConnection object
16#' @param KorAPUrl the URL of the KorAP server instance you want to access.
17#' @param apiVersion which version of KorAP's API you want to connect to.
18#' @param apiUrl URL of the KorAP web service.
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020019#' @param userAgent user agent string.
20#' @param timeout time out in seconds.
Akron5e135462019-09-27 16:31:38 +020021#' @param verbose logical. Decides whether following operations will default to be verbose.
Marc Kupietze95108e2019-09-18 13:23:58 +020022#' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}}
23#'
24#' @examples
Marc Kupietz5a519822019-09-20 21:43:52 +020025#' kcon <- new("KorAPConnection", verbose = TRUE)
Marc Kupietze95108e2019-09-18 13:23:58 +020026#' kq <- corpusQuery(kcon, "Ameisenplage")
Marc Kupietz5a519822019-09-20 21:43:52 +020027#' kq <- fetchAll(kq)
Marc Kupietz7915dc42019-09-12 17:44:58 +020028#'
29#' @note Currently it is not possible to authenticate the client
30#'
Marc Kupietze95108e2019-09-18 13:23:58 +020031#' @rdname KorAPConnection-class
Marc Kupietz632cbd42019-09-06 16:04:51 +020032#' @export
Marc Kupietze95108e2019-09-18 13:23:58 +020033setMethod("initialize", "KorAPConnection",
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020034 function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, userAgent = "R-KorAP-Client", timeout=10, verbose = FALSE) {
Marc Kupietze95108e2019-09-18 13:23:58 +020035 .Object <- callNextMethod()
36 m <- regexpr("https?://[^?]+", KorAPUrl, perl = TRUE)
37 .Object@KorAPUrl <- regmatches(KorAPUrl, m)
38 if (!endsWith(.Object@KorAPUrl, '/')) {
39 .Object@KorAPUrl <- paste0(.Object@KorAPUrl, "/")
40 }
41 if (missing(apiUrl)) {
42 .Object@apiUrl = paste0(.Object@KorAPUrl, 'api/', apiVersion, '/')
43 } else {
44 .Object@apiUrl = apiUrl
45 }
46 .Object@apiVersion = apiVersion
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020047 .Object@userAgent = userAgent
48 .Object@timeout = timeout
Marc Kupietz5a519822019-09-20 21:43:52 +020049 .Object@verbose = verbose
Marc Kupietze95108e2019-09-18 13:23:58 +020050 .Object
51 })
52
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020053setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") )
54
55#' @aliases apiCall
56#' @rdname KorAPConnection-class
57#' @param kco KorAPConnection object
58#' @param url request url
59setMethod("apiCall", "KorAPConnection", function(kco, url) {
60 resp <- GET(url, user_agent(kco@userAgent), timeout(kco@timeout))
61 if (http_type(resp) != "application/json") {
62 stop("API did not return json", call. = FALSE)
63 }
64 parsed <- jsonlite::fromJSON(content(resp, "text"))
65 if (!is.null(parsed$warnings)) {
66 message <- ifelse (nrow(parsed$warnings) > 1,
67 sapply(parsed$warnings, function(warning) paste(sprintf("%s: %s", warning[1], warning[2]), sep="\n")),
68 sprintf("%s: %s", parsed$warnings[1], parsed$warnings[2]))
69 warning(message, call. = FALSE)
70 }
71 if (status_code(resp) != 200) {
72 message <- ifelse (!is.null(parsed$errors),
Akron36def512019-09-27 17:30:51 +020073 sapply(parsed$errors, function(error) paste0(sprintf("\n%s: KorAP API request failed: %s", error[1], error[2]))),
74 message <- sprintf("%s: KorAP API request failed.", status_code(resp)))
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020075 stop(message, call. = FALSE)
76 }
77 parsed
78})
79
Marc Kupietze95108e2019-09-18 13:23:58 +020080#' @rdname KorAPConnection-class
81#' @param object KorAPConnection object
82#' @export
83setMethod("show", "KorAPConnection", function(object) {
84 cat("<KorAPConnection>", "\n")
85 cat("apiUrl: ", object@apiUrl, "\n")
86})
87
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +020088##' Funtion KorAPConnection()
89##'
90##' Wrappper function for new("KorAPConnection")
91##'
92##' @rdname KorAPConnection-constructor
93##' @name KorAPConnection-constructor
94##' @export
95## XKorAPConnection <- function(...) new("KorAPConnection", ...)