From 406dce3a4c74adfc778b8379e8503f6aebf501c6 Mon Sep 17 00:00:00 2001 From: Lukas Pietzschmann Date: Mon, 2 Sep 2024 15:17:54 +0200 Subject: [PATCH 1/3] Outsourced common code --- R/file_analysis.R | 9 +-------- R/lineage.R | 8 +------- R/slice.R | 8 +------- R/utils.R | 29 +++++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/R/file_analysis.R b/R/file_analysis.R index 445c73d..7b15eb1 100644 --- a/R/file_analysis.R +++ b/R/file_analysis.R @@ -42,12 +42,5 @@ request_file_analysis <- function(con, cfg = cfg ) } - - res <- send_request(con, request) - - if (res$type == "error") { - return(list(error = res$reason)) - } - - return(list(id = id, filetoken = filetoken, res = res)) + return(send_request_handle_response(con, request)) } diff --git a/R/lineage.R b/R/lineage.R index 2e2ac94..ec8d199 100644 --- a/R/lineage.R +++ b/R/lineage.R @@ -19,11 +19,5 @@ request_lineage <- function(con, filetoken, criterion, id = get_new_id()) { filetoken = filetoken, criterion = criterion ) - res <- send_request(con, request) - - if (res$type == "error") { - return(list(error = res$reason)) - } - - return(list(id = id, res = res)) + return(send_request_handle_response(con, request)) } diff --git a/R/slice.R b/R/slice.R index bbf75cc..ed03d6e 100644 --- a/R/slice.R +++ b/R/slice.R @@ -18,11 +18,5 @@ request_slice <- function(con, filetoken, criteria, id = get_new_id()) { filetoken = filetoken, criterion = I(criteria) ) - res <- send_request(con, request) - - if (res$type == "error") { - return(list(error = res$reason)) - } - - return(list(id = id, res = res)) + return(send_request_handle_response(con, request)) } diff --git a/R/utils.R b/R/utils.R index 27e5145..694878c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -30,3 +30,32 @@ get_filetoken <- function(filepath = rlang::missing_arg(), content = rlang::miss } return(token) } + +#' Checks if the given response was successful or not. +#' +#' The function always returns a list with the response's ID. In case of an +#' error, the list also contains the errors reason. In case of a successful +#' response, the list also contains the complete response. +#' +#' @param res The response to handle +#' +#' @return A list with either the error's reason or the complete response +#' +#' @seealso [send_request()] +handle_response <- function(res) { + if (res$type == "error") { + return(list(error = res$reason)) + } + return(list(id = res$id, res = res)) +} + +#' Send the given request over the given connection and handle a possible error +#' +#' @param con The connection to use +#' @param request The request to send +#' +#' @return See [handle_response()] for possible return values +send_request_handle_response <- function(con, request) { + res <- send_request(con, request) + return(handle_response(res)) +} From b8d2be3e602969f0cdabcb3188096749bad5390e Mon Sep 17 00:00:00 2001 From: Lukas Pietzschmann Date: Mon, 2 Sep 2024 15:22:09 +0200 Subject: [PATCH 2/3] Added doc --- man/handle_response.Rd | 22 ++++++++++++++++++++++ man/send_request_handle_response.Rd | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 man/handle_response.Rd create mode 100644 man/send_request_handle_response.Rd diff --git a/man/handle_response.Rd b/man/handle_response.Rd new file mode 100644 index 0000000..db98b05 --- /dev/null +++ b/man/handle_response.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{handle_response} +\alias{handle_response} +\title{Checks if the given response was successful or not.} +\usage{ +handle_response(res) +} +\arguments{ +\item{res}{The response to handle} +} +\value{ +A list with either the error's reason or the complete response +} +\description{ +The function always returns a list with the response's ID. In case of an +error, the list also contains the errors reason. In case of a successful +response, the list also contains the complete response. +} +\seealso{ +\code{\link[=send_request]{send_request()}} +} diff --git a/man/send_request_handle_response.Rd b/man/send_request_handle_response.Rd new file mode 100644 index 0000000..e490eca --- /dev/null +++ b/man/send_request_handle_response.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{send_request_handle_response} +\alias{send_request_handle_response} +\title{Send the given request over the given connection and handle a possible error} +\usage{ +send_request_handle_response(con, request) +} +\arguments{ +\item{con}{The connection to use} + +\item{request}{The request to send} +} +\value{ +See \code{\link[=handle_response]{handle_response()}} for possible return values +} +\description{ +Send the given request over the given connection and handle a possible error +} From acfbe6c3c52db8cefe0ded4ff1e555ecb596e994 Mon Sep 17 00:00:00 2001 From: Lukas Pietzschmann Date: Thu, 5 Sep 2024 09:49:36 +0200 Subject: [PATCH 3/3] Added better documentation for the return value of handle_response --- R/utils.R | 8 ++++++++ man/handle_response.Rd | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/R/utils.R b/R/utils.R index 694878c..2b104cb 100644 --- a/R/utils.R +++ b/R/utils.R @@ -40,6 +40,14 @@ get_filetoken <- function(filepath = rlang::missing_arg(), content = rlang::miss #' @param res The response to handle #' #' @return A list with either the error's reason or the complete response +#' \describe{ +#' \item{id}{The ID of the response (only in case of success).} +#' \item{res}{The complete response object (only in case of success).} +#' } +#' or +#' \describe{ +#' \item{error}{The reason for the error (only present in case of an error).} +#' } #' #' @seealso [send_request()] handle_response <- function(res) { diff --git a/man/handle_response.Rd b/man/handle_response.Rd index db98b05..0480941 100644 --- a/man/handle_response.Rd +++ b/man/handle_response.Rd @@ -11,6 +11,14 @@ handle_response(res) } \value{ A list with either the error's reason or the complete response +\describe{ +\item{id}{The ID of the response (only in case of success).} +\item{res}{The complete response object (only in case of success).} +} +or +\describe{ +\item{error}{The reason for the error (only present in case of an error).} +} } \description{ The function always returns a list with the response's ID. In case of an