diff --git a/NAMESPACE b/NAMESPACE index d135f18a..a4d6bf9b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/NEWS.md b/NEWS.md index 4a2e1f18..4676e883 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/importStudyAPI.R b/R/importStudyAPI.R index 67354ace..840aadf9 100644 --- a/R/importStudyAPI.R +++ b/R/importStudyAPI.R @@ -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)) +} \ No newline at end of file diff --git a/man/importZipStudyWeb.Rd b/man/importZipStudyWeb.Rd new file mode 100644 index 00000000..8bdb7eec --- /dev/null +++ b/man/importZipStudyWeb.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/importStudyAPI.R +\name{importZipStudyWeb} +\alias{importZipStudyWeb} +\title{Import a local study to Antares Web} +\usage{ +importZipStudyWeb(host, token, zipfile_name, opts = antaresRead::simOptions()) +} +\arguments{ +\item{host}{Host of AntaREST server API.} + +\item{token}{API personnal access token.} + +\item{zipfile_name}{Name of the zipfile of the study.} + +\item{opts}{List of simulation parameters returned by the function +\code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationPath()}}} +} +\value{ +An updated list containing various information about the simulation. +} +\description{ +Import a local study to Antares Web +}