Skip to content

Commit

Permalink
Outsourced common code
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPietzschmann committed Sep 2, 2024
1 parent a04f639 commit 406dce3
Show file tree
Hide file tree
Showing 4 changed files with 32 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))
}
29 changes: 29 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 406dce3

Please sign in to comment.