Skip to content

Commit

Permalink
api_get() new parameter for parsing options of content (#236)
Browse files Browse the repository at this point in the history
* api_get() new parameter for parsing options of content
  • Loading branch information
berthetclement authored Feb 27, 2024
1 parent 505c75a commit 104074e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
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)
}

#' @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 104074e

Please sign in to comment.