Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract common code from request function #23

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
LukasPietzschmann marked this conversation as resolved.
Show resolved Hide resolved
#' \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.