Skip to content

Commit

Permalink
Merge pull request #23 from flowr-analysis/20-add-function-for-lineag…
Browse files Browse the repository at this point in the history
…e-request

 Extract common code from request function
  • Loading branch information
EagleoutIce authored Sep 5, 2024
2 parents 7d8cbc7 + acfbe6c commit d396711
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 22 deletions.
9 changes: 1 addition & 8 deletions R/file_analysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 1 addition & 7 deletions R/lineage.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 1 addition & 7 deletions R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
37 changes: 37 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,40 @@ 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
#' \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) {
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))
}
30 changes: 30 additions & 0 deletions man/handle_response.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/send_request_handle_response.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d396711

Please sign in to comment.