diff --git a/NEWS.md b/NEWS.md index 8b572cde..d22d25bf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,7 +12,9 @@ BUGFIXES : - returns the right column names for details-timeStep.txt and details-res-timeStep.txt * Correction in `.formatlist()`, read N-level list instead of 2. +BREAKING CHANGES : +* `api_get()` has a new parameter to control JSON file parsing # antaresRead 2.6.0 diff --git a/R/API-methods.R b/R/API-methods.R index 36ade82f..d97e0e98 100644 --- a/R/API-methods.R +++ b/R/API-methods.R @@ -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. @@ -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 @@ -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) } #' @export diff --git a/man/API-methods.Rd b/man/API-methods.Rd index 1993589a..0a15a5b8 100644 --- a/man/API-methods.Rd +++ b/man/API-methods.Rd @@ -8,7 +8,13 @@ \alias{api_delete} \title{API methods} \usage{ -api_get(opts, endpoint, ..., default_endpoint = "v1/studies") +api_get( + opts, + endpoint, + ..., + default_endpoint = "v1/studies", + parse_result = NULL +) api_post(opts, endpoint, ..., default_endpoint = "v1/studies") @@ -25,6 +31,8 @@ Can be a full URL (by wrapping ìn \code{\link[=I]{I()}}), in that case \code{de \item{...}{Additional arguments passed to API method.} \item{default_endpoint}{Default endpoint to use.} + +\item{parse_result}{\code{character} options for parameter \code{as} of function \code{\link[httr:content]{httr::content()}}} } \value{ Response from the API. @@ -36,10 +44,15 @@ API methods \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") } }