blob: 2f1a809845c9a65dcff1811303ef8a4cf1ca990e [file] [log] [blame]
Marc Kupietzfd9e7492019-11-08 15:45:18 +01001################################################################################
2# Use setClassUnion to define the unholy NULL-data union as a virtual class.
3################################################################################
4setClassUnion("characterOrNULL", c("character", "NULL"))
Marc Kupietza4675722022-02-23 23:55:15 +01005setClassUnion("listOrNULL", c("list", "NULL"))
Marc Kupietzf83d59a2025-02-01 14:48:30 +01006# setOldClass("httr2_oauth_client")
Marc Kupietzfd9e7492019-11-08 15:45:18 +01007
Marc Kupietza8c40f42025-06-24 15:49:52 +02008#' Connect to KorAP Server
Marc Kupietz25aebc32019-09-16 18:40:50 +02009#'
Marc Kupietza8c40f42025-06-24 15:49:52 +020010#' `KorAPConnection()` creates a connection to a KorAP server for corpus queries.
11#' This is your starting point for all corpus analysis tasks.
Marc Kupietze95108e2019-09-18 13:23:58 +020012#'
Marc Kupietza8c40f42025-06-24 15:49:52 +020013#' Use `KorAPConnection()` to connect, then `corpusQuery()` to search, and
14#' `fetchAll()` to retrieve results. For authorized access to restricted corpora,
15#' use `auth()` or provide an `accessToken`.
16#'
17#' @section Basic Workflow:
18#' ```r
19#' # Connect to KorAP
20#' kcon <- KorAPConnection()
21#'
22#' # Search for a term
23#' query <- corpusQuery(kcon, "Ameisenplage")
24#'
25#' # Get all results
26#' results <- fetchAll(query)
27#' ```
28#'
29#' @section Authorization:
30#' For access to restricted corpora, authorize your connection:
31#' ```r
32#' kcon <- KorAPConnection() |> auth()
33#' ```
34#'
Marc Kupietzf9914bb2025-06-25 09:57:55 +020035#' @param KorAPUrl URL of the web user interface of the KorAP server instance you want to access.
36#' Defaults to the environment variable `KORAP_URL` if set and to the IDS Mannheim KorAP main instance
Marc Kupietz36117de2025-06-25 12:46:10 +020037#' to query DeReKo, otherwise. In order to access the KorAP instance at the German
38#' National Library (DNB) to query the contemporary fiction corpus DeLiKo@@DNB,
39#' for example, set `KorAPUrl` to <https://korap.dnb.de/>.
Marc Kupietzf9914bb2025-06-25 09:57:55 +020040#' @param apiVersion which version of KorAP's API you want to connect to. Defaults to "v1.0".
41#' @param apiUrl URL of the KorAP web service. If not provided, it will be constructed from KorAPUrl and apiVersion.
42#' @param accessToken OAuth2 access token. For queries on corpus parts with restricted
43#' access (e.g. textual queries on IPR protected data), you need to authorize
44#' your application with an access token.
45#' You can obtain an access token in the OAuth settings of your KorAP web interface.
46#'
47#' More details are explained in the
48#' [authorization section](https://github.com/KorAP/RKorAPClient#authorization)
49#' of the RKorAPClient Readme on GitHub.
50#'
51#' To use authorization based on an access token
52#' in subsequent queries, initialize your KorAP connection with:
53#'
54#' ```
55#' kco <- KorAPConnection(accessToken="<access token>")
56#' ```
57#'
58#' In order to make the API
59#' token persistent for the currently used `KorAPUrl` (you can have one
60#' token per KorAPUrl / KorAP server instance), use:
61#'
62#' ```
63#' persistAccessToken(kco)
64#' ```
65#'
66#' This will store it in your keyring using the
67#' [keyring::keyring-package]. Subsequent KorAPConnection() calls will
68#' then automatically retrieve the token from your keying. To stop using a
69#' persisted token, call `clearAccessToken(kco)`. Please note that for
70#' DeReKo, authorized queries will behave differently inside and outside the
71#' IDS, because of the special license situation. This concerns also cached
72#' results which do not take into account from where a request was issued. If
73#' you experience problems or unexpected results, please try `kco <-
74#' KorAPConnection(cache=FALSE)` or use
75#' [clearCache()] to clear the cache completely.
76#'
77#' An alternative to using an access token is to use a browser-based oauth2 workflow
78#' to obtain an access token. This can be done with the [auth()] method.
Marc Kupietz36117de2025-06-25 12:46:10 +020079#' @param oauthClient OAuth2 client object.
Marc Kupietzf9914bb2025-06-25 09:57:55 +020080#' @param oauthScope OAuth2 scope. Defaults to "search match_info".
81#' @param authorizationSupported logical that indicates if authorization is supported/necessary for the current KorAP instance. Automatically set during initialization.
82#' @param userAgent user agent string. Defaults to "R-KorAP-Client".
83#' @param timeout timeout in seconds for API requests (this does not influence server internal timeouts). Defaults to 240 seconds.
84#' @param verbose logical that decides whether following operations will default to
85#' be verbose. Defaults to FALSE.
86#' @param cache logical that decides if API calls are cached locally. You can clear
87#' the cache with [clearCache()]. Defaults to TRUE.
88#'
89#' @return [KorAPConnection()] object that can be used e.g. with [corpusQuery()]
90#'
Marc Kupietza8c40f42025-06-24 15:49:52 +020091#' @details
92#' The KorAPConnection object contains various configuration slots for advanced users:
93#' KorAPUrl (server URL), apiVersion, accessToken (OAuth2 token),
94#' timeout (request timeout), verbose (logging), cache (local caching),
95#' and other technical parameters. Most users can ignore these implementation details.
96#'
97#' @family initialization functions
Marc Kupietz0a96b282019-10-01 11:05:31 +020098#' @import R.cache
Marc Kupietze95108e2019-09-18 13:23:58 +020099#' @import utils
100#' @import methods
Marc Kupietz6dfeed92025-06-03 11:58:06 +0200101#' @include logging.R
Marc Kupietza81343d2022-09-06 12:32:10 +0200102
Marc Kupietze95108e2019-09-18 13:23:58 +0200103#' @export
Marc Kupietza824d502025-05-02 15:40:23 +0200104KorAPConnection <- setClass("KorAPConnection", slots = c(KorAPUrl = "character", apiVersion = "character", indexRevision = "characterOrNULL", apiUrl = "character", accessToken = "characterOrNULL", oauthClient = "ANY", oauthScope = "characterOrNULL", authorizationSupported = "logical", userAgent = "character", timeout = "numeric", verbose = "logical", cache = "logical", welcome = "listOrNULL"))
Marc Kupietz62b17892025-02-01 18:26:45 +0100105
Marc Kupietza824d502025-05-02 15:40:23 +0200106generic_kor_app_id <- "99FbPHH7RrN36hbndF7b6f"
Marc Kupietz62b17892025-02-01 18:26:45 +0100107
Marc Kupietza824d502025-05-02 15:40:23 +0200108kustvakt_redirect_uri <- "http://localhost:1410/"
109kustvakt_auth_path <- "settings/oauth/authorize"
Marc Kupietz62b17892025-02-01 18:26:45 +0100110
Marc Kupietze95108e2019-09-18 13:23:58 +0200111
Marc Kupietza8c40f42025-06-24 15:49:52 +0200112#' Initialize KorAPConnection object
113#' @keywords internal
Marc Kupietz632cbd42019-09-06 16:04:51 +0200114#' @export
Marc Kupietz36117de2025-06-25 12:46:10 +0200115#'
Marc Kupietzb79fd442025-03-26 10:25:03 +0100116setMethod("initialize", "KorAPConnection", function(.Object,
117 KorAPUrl = if (is.null(Sys.getenv("KORAP_URL")) |
Marc Kupietza824d502025-05-02 15:40:23 +0200118 Sys.getenv("KORAP_URL") == "") {
Marc Kupietzb79fd442025-03-26 10:25:03 +0100119 "https://korap.ids-mannheim.de/"
Marc Kupietza824d502025-05-02 15:40:23 +0200120 } else {
121 Sys.getenv("KORAP_URL")
122 },
123 apiVersion = "v1.0",
Marc Kupietzb79fd442025-03-26 10:25:03 +0100124 apiUrl,
125 accessToken = getAccessToken(KorAPUrl),
126 oauthClient = NULL,
127 oauthScope = "search match_info",
128 authorizationSupported = TRUE,
129 userAgent = "R-KorAP-Client",
130 timeout = 240,
131 verbose = FALSE,
132 cache = TRUE) {
133 .Object <- callNextMethod()
134 m <- regexpr("https?://[^?]+", KorAPUrl, perl = TRUE)
135 .Object@KorAPUrl <- regmatches(KorAPUrl, m)
Marc Kupietza824d502025-05-02 15:40:23 +0200136 if (!endsWith(.Object@KorAPUrl, "/")) {
Marc Kupietzb79fd442025-03-26 10:25:03 +0100137 .Object@KorAPUrl <- paste0(.Object@KorAPUrl, "/")
138 }
139 if (missing(apiUrl)) {
Marc Kupietza824d502025-05-02 15:40:23 +0200140 .Object@apiUrl <- paste0(.Object@KorAPUrl, "api/", apiVersion, "/")
141 } else {
142 .Object@apiUrl <- apiUrl
143 }
144 .Object@accessToken <- accessToken
145 .Object@oauthClient <- oauthClient
146 .Object@apiVersion <- apiVersion
147 .Object@userAgent <- userAgent
148 .Object@oauthScope <- oauthScope
149 .Object@authorizationSupported <- authorizationSupported
150 .Object@timeout <- timeout
151 .Object@verbose <- verbose
152 .Object@cache <- cache
153 .Object@welcome <- apiCall(.Object, .Object@apiUrl, json = FALSE, cache = FALSE, getHeaders = TRUE)
154 if (!is.null(.Object@welcome)) {
155 message(.Object@welcome[[2]])
156 resp <- httr2::request(.Object@KorAPUrl) |>
157 httr2::req_url_path_append(kustvakt_auth_path) |>
158 httr2::req_error(is_error = \(resp) FALSE) |>
159 httr2::req_perform()
160 .Object@authorizationSupported <- (httr2::resp_status(resp) == 200)
Marc Kupietz62b17892025-02-01 18:26:45 +0100161
Marc Kupietza824d502025-05-02 15:40:23 +0200162 .Object@indexRevision <- .Object@welcome[[1]][["x-index-revision"]]
163 } else {
164 if (grepl(.Object@KorAPUrl, .Object@apiUrl)) {
165 message("Could not connect to KorAP instance ", .Object@KorAPUrl)
166 } else {
167 message("Could not connect to KorAP API at ", .Object@apiUrl)
168 }
169 }
170 .Object
171})
Marc Kupietze95108e2019-09-18 13:23:58 +0200172
Marc Kupietza96537f2019-11-09 23:07:44 +0100173
Marc Kupietzb956b812019-11-25 17:53:13 +0100174accessTokenServiceName <- "RKorAPClientAccessToken"
Marc Kupietz4862b862019-11-07 10:13:53 +0100175
Marc Kupietza824d502025-05-02 15:40:23 +0200176setGeneric("persistAccessToken", function(kco, ...) standardGeneric("persistAccessToken"))
Marc Kupietz4862b862019-11-07 10:13:53 +0100177
Marc Kupietza4f51d72025-01-25 16:23:18 +0100178#' Persist current access token in keyring
179#'
Marc Kupietza8c40f42025-06-24 15:49:52 +0200180#' @family initialization functions
Marc Kupietza4f51d72025-01-25 16:23:18 +0100181#' @param kco KorAPConnection object
182#' @param accessToken access token to be persisted. If not supplied, the current access token of the KorAPConnection object will be used.
183#' @return KorAPConnection object.
184#'
Marc Kupietzb956b812019-11-25 17:53:13 +0100185#' @aliases persistAccessToken
Marc Kupietza4f51d72025-01-25 16:23:18 +0100186#'
Marc Kupietz4862b862019-11-07 10:13:53 +0100187#' @import keyring
188#' @export
Marc Kupietza4f51d72025-01-25 16:23:18 +0100189#'
Marc Kupietz4862b862019-11-07 10:13:53 +0100190#' @examples
191#' \dontrun{
Marc Kupietza824d502025-05-02 15:40:23 +0200192#' kco <- KorAPConnection(accessToken = "e739u6eOzkwADQPdVChxFg")
Marc Kupietzb956b812019-11-25 17:53:13 +0100193#' persistAccessToken(kco)
Marc Kupietza4f51d72025-01-25 16:23:18 +0100194#'
Marc Kupietza824d502025-05-02 15:40:23 +0200195#' kco <- KorAPConnection() %>%
196#' auth(app_id = "<my application id>") %>%
197#' persistAccessToken()
Marc Kupietz4862b862019-11-07 10:13:53 +0100198#' }
199#'
Marc Kupietza4f51d72025-01-25 16:23:18 +0100200#' @seealso [clearAccessToken()], [auth()]
201#'
Marc Kupietza824d502025-05-02 15:40:23 +0200202setMethod("persistAccessToken", "KorAPConnection", function(kco, accessToken = kco@accessToken) {
203 if (!is.null(kco@oauthClient)) {
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100204 warning("Short lived access tokens from a confidential application cannot be persisted.")
205 return(kco)
206 }
Marc Kupietza824d502025-05-02 15:40:23 +0200207 if (is.null(accessToken)) {
Marc Kupietzb956b812019-11-25 17:53:13 +0100208 stop("It seems that you have not supplied any access token that could be persisted.", call. = FALSE)
Marc Kupietza824d502025-05-02 15:40:23 +0200209 }
Marc Kupietz4862b862019-11-07 10:13:53 +0100210
Marc Kupietzb956b812019-11-25 17:53:13 +0100211 kco@accessToken <- accessToken
212 key_set_with_value(accessTokenServiceName, kco@KorAPUrl, accessToken)
Marc Kupietza4f51d72025-01-25 16:23:18 +0100213 return(kco)
Marc Kupietz4862b862019-11-07 10:13:53 +0100214})
215
Marc Kupietza824d502025-05-02 15:40:23 +0200216setGeneric("clearAccessToken", function(kco) standardGeneric("clearAccessToken"))
Marc Kupietz4862b862019-11-07 10:13:53 +0100217
Marc Kupietza4f51d72025-01-25 16:23:18 +0100218#' Clear access token from keyring and KorAPConnection object
219#'
Marc Kupietza8c40f42025-06-24 15:49:52 +0200220#' @family initialization functions
Marc Kupietzb956b812019-11-25 17:53:13 +0100221#' @aliases clearAccessToken
Marc Kupietz4862b862019-11-07 10:13:53 +0100222#' @import keyring
Marc Kupietza4f51d72025-01-25 16:23:18 +0100223#' @param kco KorAPConnection object
224#' @return KorAPConnection object with access token set to `NULL`.
Marc Kupietz4862b862019-11-07 10:13:53 +0100225#' @export
226#' @examples
Marc Kupietza4f51d72025-01-25 16:23:18 +0100227#' \dontrun{
Marc Kupietz617266d2025-02-27 10:43:07 +0100228#' kco <- KorAPConnection()
Marc Kupietza4f51d72025-01-25 16:23:18 +0100229#' kco <- clearAccessToken(kco)
Marc Kupietz4862b862019-11-07 10:13:53 +0100230#' }
231#'
Marc Kupietza4f51d72025-01-25 16:23:18 +0100232#' @seealso [persistAccessToken()]
233#'
Marc Kupietza824d502025-05-02 15:40:23 +0200234setMethod("clearAccessToken", "KorAPConnection", function(kco) {
Marc Kupietzb956b812019-11-25 17:53:13 +0100235 key_delete(accessTokenServiceName, kco@KorAPUrl)
Marc Kupietza4f51d72025-01-25 16:23:18 +0100236 kco@accessToken <- NULL
237 kco
Marc Kupietz4862b862019-11-07 10:13:53 +0100238})
239
Marc Kupietza4f51d72025-01-25 16:23:18 +0100240
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100241oauthRefresh <- function(req, client, scope, kco) {
Marc Kupietza824d502025-05-02 15:40:23 +0200242 httr2::req_oauth_auth_code(req, client,
243 scope = scope,
244 auth_url = paste0(kco@KorAPUrl, kustvakt_auth_path),
245 redirect_uri = kustvakt_redirect_uri,
246 cache_key = kco@KorAPUrl
247 )
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100248}
249
Marc Kupietza824d502025-05-02 15:40:23 +0200250setGeneric("auth", function(kco, app_id = generic_kor_app_id, app_secret = NULL, scope = kco@oauthScope) standardGeneric("auth"))
Marc Kupietza4f51d72025-01-25 16:23:18 +0100251
252#' Authorize RKorAPClient
253#'
Marc Kupietza8c40f42025-06-24 15:49:52 +0200254#' @family initialization functions
Marc Kupietza4f51d72025-01-25 16:23:18 +0100255#' @aliases auth
256#'
257#' @description
Marc Kupietza4f51d72025-01-25 16:23:18 +0100258#'
259#' Authorize RKorAPClient to make KorAP queries and download results on behalf of the user.
260#'
261#' @param kco KorAPConnection object
262#' @param app_id OAuth2 application id. Defaults to the generic KorAP client application id.
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100263#' @param app_secret OAuth2 application secret. Used with confidential client applications. Defaults to `NULL`.
Marc Kupietza4f51d72025-01-25 16:23:18 +0100264#' @param scope OAuth2 scope. Defaults to "search match_info".
265#' @return KorAPConnection object with access token set in `@accessToken`.
266#'
267#' @importFrom httr2 oauth_client oauth_flow_auth_code
268#' @examples
269#' \dontrun{
Marc Kupietz617266d2025-02-27 10:43:07 +0100270#' kco <- KorAPConnection(verbose = TRUE) %>% auth()
Marc Kupietza5501652025-01-28 20:25:42 +0100271#' df <- collocationAnalysis(kco, "focus([marmot/p=ADJA] {Ameisenplage})",
Marc Kupietza824d502025-05-02 15:40:23 +0200272#' leftContextSize = 1, rightContextSize = 0
273#' )
Marc Kupietza4f51d72025-01-25 16:23:18 +0100274#' }
275#'
276#' @seealso [persistAccessToken()], [clearAccessToken()]
277#'
278#' @export
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100279setMethod("auth", "KorAPConnection", function(kco, app_id = generic_kor_app_id, app_secret = NULL, scope = kco@oauthScope) {
Marc Kupietz62b17892025-02-01 18:26:45 +0100280 if (kco@authorizationSupported == FALSE) {
281 log_info(kco@verbose, "Authorization is not supported by this KorAP instance.")
282 return(kco)
283 }
Marc Kupietza824d502025-05-02 15:40:23 +0200284 if (kco@KorAPUrl != "https://korap.ids-mannheim.de/" & app_id == generic_kor_app_id) {
Marc Kupietza4f51d72025-01-25 16:23:18 +0100285 warning(paste("You can use the default app_id only for the IDS Mannheim KorAP main instance for querying DeReKo. Please provide your own app_id for accesing", kco@KorAPUrl))
286 return(kco)
287 }
288 if (is.null(kco@accessToken) || is.null(kco@welcome)) { # if access token is not set or invalid
Marc Kupietza824d502025-05-02 15:40:23 +0200289 client <- if (!is.null(kco@oauthClient)) {
290 kco@oauthClient
291 } else {
Marc Kupietza4f51d72025-01-25 16:23:18 +0100292 httr2::oauth_client(
Marc Kupietza824d502025-05-02 15:40:23 +0200293 id = app_id,
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100294 secret = app_secret,
Marc Kupietza4f51d72025-01-25 16:23:18 +0100295 token_url = paste0(kco@apiUrl, "oauth2/token")
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100296 )
Marc Kupietza824d502025-05-02 15:40:23 +0200297 }
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100298 if (is.null(app_secret)) {
Marc Kupietza824d502025-05-02 15:40:23 +0200299 kco@accessToken <- (client |>
Marc Kupietza4f51d72025-01-25 16:23:18 +0100300 httr2::oauth_flow_auth_code(
301 scope = scope,
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100302 auth_url = paste0(kco@KorAPUrl, kustvakt_auth_path),
Marc Kupietz62b17892025-02-01 18:26:45 +0100303 redirect_uri = kustvakt_redirect_uri
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100304 ))$access_token
305 log_info(kco@verbose, "Client authorized. New access token set.")
306 } else {
307 kco@oauthClient <- client
308 kco@oauthScope <- scope
309 req <- request(kco@apiUrl) |>
310 oauthRefresh(client, scope, kco) |>
311 req_perform()
312 log_info(kco@verbose, "Client authorized. Short lived access token will be refreshed automatically.")
313 }
Marc Kupietza4f51d72025-01-25 16:23:18 +0100314 } else {
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100315 log_info(kco@verbose, "Access token already set.")
Marc Kupietza4f51d72025-01-25 16:23:18 +0100316 }
317 return(kco)
318})
319
320
321
Marc Kupietz4862b862019-11-07 10:13:53 +0100322#' @import keyring
Marc Kupietzb956b812019-11-25 17:53:13 +0100323getAccessToken <- function(KorAPUrl) {
Marc Kupietza824d502025-05-02 15:40:23 +0200324 keyList <- tryCatch(
325 withCallingHandlers(key_list(service = accessTokenServiceName),
326 warning = function(w) invokeRestart("muffleWarning"),
327 error = function(e) {
328 return(NULL)
329 }
330 ),
331 error = function(e) { }
332 )
333 if (KorAPUrl %in% keyList$username) {
Marc Kupietzb956b812019-11-25 17:53:13 +0100334 key_get(accessTokenServiceName, KorAPUrl)
Marc Kupietza824d502025-05-02 15:40:23 +0200335 } else {
Marc Kupietzfd9e7492019-11-08 15:45:18 +0100336 NULL
Marc Kupietza824d502025-05-02 15:40:23 +0200337 }
Marc Kupietz4862b862019-11-07 10:13:53 +0100338}
Marc Kupietz0a96b282019-10-01 11:05:31 +0200339
Marc Kupietz581a29b2021-09-04 20:51:04 +0200340
Marc Kupietz62b17892025-02-01 18:26:45 +0100341warnIfNotAuthorized <- function(kco) {
342 if (kco@authorizationSupported & is.null(kco@accessToken) & is.null(kco@oauthClient)) {
Marc Kupietz581a29b2021-09-04 20:51:04 +0200343 warning(
344 paste0(
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100345 "In order to receive KWICSs also from corpora with restricted licenses, you may need to\n",
346 "authorize your application with an access token or the auth() method.\n",
347 "To generate an access token, login to KorAP and navigate to KorAP's OAuth settings <",
Marc Kupietz581a29b2021-09-04 20:51:04 +0200348 kco@KorAPUrl,
349 "settings/oauth#page-top>"
350 )
351 )
352 }
353}
354
Marc Kupietz0a96b282019-10-01 11:05:31 +0200355KorAPCacheSubDir <- function() {
Marc Kupietza824d502025-05-02 15:40:23 +0200356 paste0(
357 "RKorAPClient_",
358 gsub(
359 "^([0-9]+\\.[0-9]+).*",
360 "\\1",
361 packageVersion("RKorAPClient"),
362 perl = TRUE
363 )
364 )
Marc Kupietz0a96b282019-10-01 11:05:31 +0200365}
366
Marc Kupietza824d502025-05-02 15:40:23 +0200367setGeneric("apiCall", function(kco, ...) standardGeneric("apiCall"))
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200368
Marc Kupietz4de53ec2019-10-04 09:12:00 +0200369## quiets concerns of R CMD check re: the .'s that appear in pipelines
Marc Kupietzef1ef4a2025-02-19 12:12:40 +0100370utils::globalVariables(c("."))
Marc Kupietz4de53ec2019-10-04 09:12:00 +0200371
Marc Kupietza8c40f42025-06-24 15:49:52 +0200372#' Internal API call method
373#' @keywords internal
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200374#' @aliases apiCall
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200375#' @param kco KorAPConnection object
376#' @param url request url
Marc Kupietzf9129592025-01-26 19:17:54 +0100377#' @param json logical that determines if JSON result is expected
Marc Kupietzb49afa02020-06-04 15:50:29 +0200378#' @param getHeaders logical that determines if headers and content should be returned (as a list)
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200379#' @importFrom jsonlite fromJSON
Marc Kupietza4675722022-02-23 23:55:15 +0100380#' @importFrom curl has_internet
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100381#' @import httr2
Marc Kupietz69cc54a2019-09-30 12:06:54 +0200382#' @export
Marc Kupietzf9129592025-01-26 19:17:54 +0100383setMethod("apiCall", "KorAPConnection", function(kco, url, json = TRUE, getHeaders = FALSE, cache = kco@cache, timeout = kco@timeout) {
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100384 result <- ""
Marc Kupietzf9129592025-01-26 19:17:54 +0100385
386 # Handle caching if enabled
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100387 if (cache) {
Marc Kupietzf9129592025-01-26 19:17:54 +0100388 result <- R.cache::loadCache(dir = KorAPCacheSubDir(), key = list(url, kco@accessToken, kco@indexRevision))
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100389 if (!is.null(result)) {
Marc Kupietzf9129592025-01-26 19:17:54 +0100390 if (!is.null(result$meta)) result$meta$cached <- "local"
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100391 return(result)
Marc Kupietz0a96b282019-10-01 11:05:31 +0200392 }
393 }
Marc Kupietza4675722022-02-23 23:55:15 +0100394
Marc Kupietzf9129592025-01-26 19:17:54 +0100395 # Check for internet connection
Marc Kupietza4675722022-02-23 23:55:15 +0100396 if (!curl::has_internet()) {
397 message("No internet connection.")
398 return(invisible(NULL))
399 }
400
Marc Kupietzf9129592025-01-26 19:17:54 +0100401 # Create the request
402 req <- httr2::request(url) |>
403 httr2::req_user_agent(kco@userAgent) |>
404 httr2::req_timeout(timeout)
Marc Kupietza4675722022-02-23 23:55:15 +0100405
Marc Kupietz03402e72025-05-02 15:39:40 +0200406 if (!is.null(kco@oauthClient)) {
407 req <- req |> oauthRefresh(kco@oauthClient, scope = kco@oauthScope, kco)
Marc Kupietzf83d59a2025-02-01 14:48:30 +0100408 } else if (!is.null(kco@accessToken)) {
409 req <- req |> httr2::req_auth_bearer_token(kco@accessToken)
Marc Kupietzf9129592025-01-26 19:17:54 +0100410 }
411
Marc Kupietzd36ee552025-05-02 20:42:50 +0200412 resp <- tryCatch(req |> httr2::req_perform(),
413 error = function(e) {
414 if (is.null(e$resp)) {
415 message(paste("Error: ", e$message, collapse = " "), if ("parent" %in% names(e)) paste0("\n", e$parent$message) else "")
416 return(invisible(NULL))
417 }
418 return(e$resp)
419 }
420 )
Marc Kupietz03402e72025-05-02 15:39:40 +0200421
422 if (is.null(resp)) {
Marc Kupietz03402e72025-05-02 15:39:40 +0200423 return(invisible(NULL))
424 }
Marc Kupietz62b17892025-02-01 18:26:45 +0100425
Marc Kupietzf9129592025-01-26 19:17:54 +0100426 if (resp |> httr2::resp_status() != 200) {
Marc Kupietzd36ee552025-05-02 20:42:50 +0200427 message("Error: Request failed with status ", resp |> httr2::resp_status(), ": ", resp |> httr2::resp_status_desc())
Marc Kupietz62b17892025-02-01 18:26:45 +0100428 if (resp |> httr2::resp_content_type() == "application/json") {
429 result <- tryCatch(
430 resp |> httr2::resp_body_json(),
431 error = function(e) {
432 message("Failed to parse json with error details: ", e$message)
433 return(NULL)
434 }
435 )
436 # Handle errors in the response (if any)
437 if (!is.null(result$errors)) {
438 errors <- result$errors
439 warning_msgs <- if (is.data.frame(errors)) {
440 apply(errors, 1, function(warning) paste(warning[1], ": ", warning[2]))
441 } else {
442 lapply(errors, function(error) paste(error, collapse = " "))
443 }
Marc Kupietz03402e72025-05-02 15:39:40 +0200444 message(paste("Warning: ", warning_msgs, collapse = "\n"))
Marc Kupietzf9129592025-01-26 19:17:54 +0100445 }
Marc Kupietzf9129592025-01-26 19:17:54 +0100446 }
Marc Kupietza4675722022-02-23 23:55:15 +0100447 return(invisible(NULL))
448 }
Marc Kupietzf9129592025-01-26 19:17:54 +0100449
450 # Process JSON response or raw text based on `json` parameter
451 if (json) {
452 content_type <- resp |> httr2::resp_content_type()
453 if (!content_type %in% c("application/json", "application/ld+json")) {
454 message("API did not return JSON")
Marc Kupietza4675722022-02-23 23:55:15 +0100455 return(invisible(NULL))
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100456 }
Marc Kupietz04814f22023-04-16 17:13:27 +0200457
Marc Kupietzf9129592025-01-26 19:17:54 +0100458 result <- tryCatch(
459 resp |> httr2::resp_body_string() |> jsonlite::fromJSON(),
460 error = function(e) {
461 message("Failed to parse JSON: ", e$message)
462 return(NULL)
463 }
464 )
465
466 # Handle warnings in the response (if any)
467 if (!is.null(result$warnings)) {
468 warnings <- result$warnings
469 warning_msgs <- if (is.data.frame(warnings)) {
470 apply(warnings, 1, function(warning) paste(warning[1], ": ", warning[2]))
471 } else {
472 lapply(warnings, function(warning) paste(warning, collapse = " "))
473 }
Marc Kupietz03402e72025-05-02 15:39:40 +0200474 message(paste0("\nWarning: ", paste(warning_msgs, collapse = " ")))
475 if (cache & any(grepl("682", warning_msgs))) {
476 cache <- FALSE
Marc Kupietzd36ee552025-05-02 20:42:50 +0200477 log_info(kco@verbose, "Caching will be skipped because of warnings ")
Marc Kupietz03402e72025-05-02 15:39:40 +0200478 }
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100479 }
Marc Kupietzf9129592025-01-26 19:17:54 +0100480 } else {
481 result <- resp |> httr2::resp_body_string()
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200482 }
Marc Kupietzf9129592025-01-26 19:17:54 +0100483
484 # Save to cache if enabled
Marc Kupietz03402e72025-05-02 15:39:40 +0200485 if (cache && resp |> httr2::resp_status() == 200) {
Marc Kupietzb49afa02020-06-04 15:50:29 +0200486 R.cache::saveCache(result, key = list(url, kco@accessToken, kco@indexRevision), dir = KorAPCacheSubDir(), compress = TRUE)
Marc Kupietzb2b32a32020-03-24 13:56:50 +0100487 }
Marc Kupietzf9129592025-01-26 19:17:54 +0100488
489 # Return headers and content as a list if `getHeaders` is TRUE
Marc Kupietzb49afa02020-06-04 15:50:29 +0200490 if (getHeaders) {
Marc Kupietzf9129592025-01-26 19:17:54 +0100491 list(headers = resp |> httr2::resp_headers(), content = result)
Marc Kupietzb49afa02020-06-04 15:50:29 +0200492 } else {
493 result
494 }
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200495})
496
Marc Kupietza824d502025-05-02 15:40:23 +0200497setGeneric("clearCache", function(kco) standardGeneric("clearCache"))
Marc Kupietz0a96b282019-10-01 11:05:31 +0200498
Marc Kupietzdc880ac2025-06-24 20:34:43 +0200499#' Clear local cache
500#'
501#' Clears the local cache of API responses for the current RKorAPClient version.
502#' Useful when you want to force fresh data retrieval or free up disk space.
503#'
504#' @family connection-initialization
505#' @param kco KorAPConnection object
506#' @return Invisible NULL (function called for side effects)
507#' @examples
508#' \dontrun{
509#' kco <- KorAPConnection()
510#' clearCache(kco)
511#' }
Marc Kupietzf9914bb2025-06-25 09:57:55 +0200512#'
Marc Kupietz0a96b282019-10-01 11:05:31 +0200513#' @aliases clearCache
Marc Kupietz0a96b282019-10-01 11:05:31 +0200514#' @export
Marc Kupietza824d502025-05-02 15:40:23 +0200515setMethod("clearCache", "KorAPConnection", function(kco) {
516 R.cache::clearCache(dir = KorAPCacheSubDir())
Marc Kupietz0a96b282019-10-01 11:05:31 +0200517})
518
Marc Kupietza8c40f42025-06-24 15:49:52 +0200519#' Display KorAPConnection object
520#' @keywords internal
Marc Kupietze95108e2019-09-18 13:23:58 +0200521#' @param object KorAPConnection object
522#' @export
523setMethod("show", "KorAPConnection", function(object) {
524 cat("<KorAPConnection>", "\n")
525 cat("apiUrl: ", object@apiUrl, "\n")
526})
527
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200528##' Funtion KorAPConnection()
529##'
Marc Kupietz617266d2025-02-27 10:43:07 +0100530##' Wrappper function for KorAPConnection()
Marc Kupietzd0d3e9b2019-09-24 17:36:03 +0200531##'
532##' @rdname KorAPConnection-constructor
533##' @name KorAPConnection-constructor
534##' @export
Marc Kupietz617266d2025-02-27 10:43:07 +0100535## XKorAPConnection <- function(...) KorAPConnection(...)