Skip to content

Commit

Permalink
ImportZipStudyWeb to upload a local study in Antares Web (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekmek7 authored Mar 15, 2024
1 parent fb62de7 commit 7f5b011
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export(getJobLogs)
export(getJobs)
export(getPlaylist)
export(getVariantCommands)
export(importZipStudyWeb)
export(is_antares_v7)
export(is_antares_v820)
export(list_pollutants_values)
Expand Down Expand Up @@ -140,6 +141,7 @@ importFrom(httr,content)
importFrom(httr,content_type_json)
importFrom(httr,status_code)
importFrom(httr,stop_for_status)
importFrom(httr,upload_file)
importFrom(jsonlite,toJSON)
importFrom(jsonlite,write_json)
importFrom(memuse,Sys.meminfo)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ NEW FEATURES :

* Complete function `deleteStudy()` with new parameter `simulation` to delete a simulation in an Antares study.
* New parameter `geographic.trimming` in `updateGeneralSettings()`to activate or deactivate this general parameter.
* Add `importZipStudyWeb()` to allow the user to import a local study in Antares Web

### Breaking changes

Expand Down
62 changes: 36 additions & 26 deletions R/importStudyAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,39 @@ copyStudyWeb <- function(opts = antaresRead::simOptions(), host, token,



# importZipStudyWeb <- function(studyPath, opts = antaresRead::simOptions(), host, token) {
#
# if (missing(host)) stop("Please specify an url to antares API host.")
# if (missing(token)) stop("Please specify your access token.")
# if (missing(studyPath)) studyPath <- opts$studyPath
#
# if (!file.exists(studyPath)) stop("Study not found.")
# #Check if study is already zipped
# if (length(grep("*\\.zip$", studyPath)) == 0){
# backupStudy(opts)
# studyPath <- paste0(studyPath, ".zip")
# }
#
# toto = base64enc::base64encode(studyPath)
# con_toto <- file(studyPath, "rb")
# toto = readBin(studyPath, "raw", file.info(studyPath)$size)
# studyId <- api_post(
# opts = list(host = host, token = token),
# endpoint = "_import",
# encode = "raw",
# query = list(
# study = toto
# )
# )
#
# }
#' @title Import a local study to Antares Web
#'
#' @param host Host of AntaREST server API.
#' @param token API personnal access token.
#' @param zipfile_name Name of the zipfile of the study.
#'
#' @template opts
#'
#' @importFrom antaresRead setSimulationPathAPI api_post simOptions
#' @importFrom httr upload_file
#'
#' @export
#'
importZipStudyWeb <- function(host, token, zipfile_name, opts = antaresRead::simOptions()) {

# Build the destination folder
dir_study <- unlist(strsplit(opts$studyPath, split = .Platform$file.sep))
dir_study <- dir_study[seq(length(dir_study) - 1)]
dir_study <- do.call("file.path", as.list(dir_study))

# Zip the study
zipfile <- backupStudy(zipfile_name, what = "study", opts = opts, extension = ".zip")

# Import the study
studyId <- api_post(
opts = list(host = host, token = token),
endpoint = "_import",
default_endpoint = "v1/studies",
body = list(study = upload_file(file.path(dir_study, zipfile))),
encode = "multipart"
)

opts <- setSimulationPathAPI(host = host, token = token, study_id = studyId, simulation = "input")

return(invisible(opts))
}
24 changes: 24 additions & 0 deletions man/importZipStudyWeb.Rd

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

0 comments on commit 7f5b011

Please sign in to comment.