Skip to content

Commit

Permalink
api_get() new parameter for parsing options of content
Browse files Browse the repository at this point in the history
  • Loading branch information
BERTHET Clement (Externe) committed Feb 23, 2024
1 parent 505c75a commit 9560950
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
21 changes: 15 additions & 6 deletions R/API-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' Can be a full URL (by wrapping ìn [I()]), in that case `default_endpoint` is ignored.
#' @param ... Additional arguments passed to API method.
#' @param default_endpoint Default endpoint to use.
#' @param parse_result `character` options for parameter `as` of function [httr::content()]
#' @param opts Antares simulation options or a `list` with an `host = ` slot.
#'
#' @return Response from the API.
Expand All @@ -18,13 +19,21 @@
#' \dontrun{
#'
#' # List studies with local API
#' api_get(
#' opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL
#' )
#' # default result content in R object (auto parsed)
#' api_get(opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL,
#' parse_result = NULL)
#'
#' # you can force parse options as text
#' api_get(opts = list(host = "http://0.0.0.0:8080"),
#' endpoint = NULL,
#' parse_result = "text")
#'
#' }
api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
api_get <- function(opts,
endpoint, ...,
default_endpoint = "v1/studies",
parse_result = NULL) {
if (inherits(endpoint, "AsIs")) {
opts$host <- endpoint
endpoint <- NULL
Expand Down Expand Up @@ -65,7 +74,7 @@ api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
stop_for_status(result, task = mess_error)
}else
warn_for_status(result)
content(result)
content(result, as = parse_result)

Check warning on line 77 in R/API-methods.R

View check run for this annotation

Codecov / codecov/patch

R/API-methods.R#L77

Added line #L77 was not covered by tests
}

#' @export
Expand Down
23 changes: 18 additions & 5 deletions man/API-methods.Rd

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

0 comments on commit 9560950

Please sign in to comment.