Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 1 | #' Class KorAPConnection |
Marc Kupietz | 25aebc3 | 2019-09-16 18:40:50 +0200 | [diff] [blame] | 2 | #' |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 3 | #' \code{KorAPConnection} objects represent the connection to a KorAP server. |
| 4 | #' New \code{KorAPConnection} objects can be created by \code{KorAPConnection()}. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 5 | #' |
| 6 | #' @import jsonlite |
| 7 | #' @import utils |
| 8 | #' @import methods |
| 9 | #' |
| 10 | #' |
| 11 | |
| 12 | #' @export |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 13 | KorAPConnection <- setClass("KorAPConnection", slots=c(KorAPUrl="character", apiVersion="character", apiUrl="character", userAgent="character", timeout="numeric", verbose="logical")) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 14 | |
| 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 Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 19 | #' @param userAgent user agent string. |
| 20 | #' @param timeout time out in seconds. |
Akron | 5e13546 | 2019-09-27 16:31:38 +0200 | [diff] [blame] | 21 | #' @param verbose logical. Decides whether following operations will default to be verbose. |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 22 | #' @return \code{\link{KorAPConnection}} object that can be used e.g. with \code{\link{corpusQuery}} |
| 23 | #' |
| 24 | #' @examples |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 25 | #' kcon <- new("KorAPConnection", verbose = TRUE) |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 26 | #' kq <- corpusQuery(kcon, "Ameisenplage") |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 27 | #' kq <- fetchAll(kq) |
Marc Kupietz | 7915dc4 | 2019-09-12 17:44:58 +0200 | [diff] [blame] | 28 | #' |
| 29 | #' @note Currently it is not possible to authenticate the client |
| 30 | #' |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 31 | #' @rdname KorAPConnection-class |
Marc Kupietz | 632cbd4 | 2019-09-06 16:04:51 +0200 | [diff] [blame] | 32 | #' @export |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 33 | setMethod("initialize", "KorAPConnection", |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 34 | function(.Object, KorAPUrl = "https://korap.ids-mannheim.de/", apiVersion = 'v1.0', apiUrl, userAgent = "R-KorAP-Client", timeout=10, verbose = FALSE) { |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 35 | .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 Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 47 | .Object@userAgent = userAgent |
| 48 | .Object@timeout = timeout |
Marc Kupietz | 5a51982 | 2019-09-20 21:43:52 +0200 | [diff] [blame] | 49 | .Object@verbose = verbose |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 50 | .Object |
| 51 | }) |
| 52 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 53 | setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall") ) |
| 54 | |
| 55 | #' @aliases apiCall |
| 56 | #' @rdname KorAPConnection-class |
| 57 | #' @param kco KorAPConnection object |
| 58 | #' @param url request url |
| 59 | setMethod("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), |
| 73 | 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))) |
| 75 | stop(message, call. = FALSE) |
| 76 | } |
| 77 | parsed |
| 78 | }) |
| 79 | |
Marc Kupietz | e95108e | 2019-09-18 13:23:58 +0200 | [diff] [blame] | 80 | #' @rdname KorAPConnection-class |
| 81 | #' @param object KorAPConnection object |
| 82 | #' @export |
| 83 | setMethod("show", "KorAPConnection", function(object) { |
| 84 | cat("<KorAPConnection>", "\n") |
| 85 | cat("apiUrl: ", object@apiUrl, "\n") |
| 86 | }) |
| 87 | |
Marc Kupietz | d0d3e9b | 2019-09-24 17:36:03 +0200 | [diff] [blame] | 88 | ##' 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", ...) |