Commit e33bb826 authored by Matija Obreza's avatar Matija Obreza

Download MCPD and PDCI data in Excel for the specified institute

parent e2abcf98
Package: genesysr
Version: 0.9.2
Version: 0.9.3
Title: Genesys PGR Client
Description: Access data on plant genetic resources from genebanks around the world published on Genesys (<https://www.genesys-pgr.org>).
Your use of data is subject to terms and conditions available at <https://www.genesys-pgr.org/content/legal/terms>.
......
# Generated by roxygen2: do not edit by hand
export(api1_url)
export(api_url)
export(authorization)
export(client_login)
export(download_mcpd)
export(download_pdci)
export(fetch_accessions)
export(filter_DOI)
export(filter_ORIGCTY)
......
......@@ -18,6 +18,7 @@
#'
#' @param libname Library name
#' @param pkgname Package name
#' @keywords internal
.onLoad <- function(libname, pkgname) {
setup_production()
}
......@@ -80,6 +81,7 @@ authorization <- function(authorization) {
}
#' Ensure that environment has OAuth token
#' @keywords internal
.check_auth <- function() {
if (is.null(.genesysEnv$Authorization)) {
warning("You must first authorize with Genesys with user_login() or client_login(...).");
......@@ -158,6 +160,7 @@ client_login <- function() {
}
#' @keywords internal
.api_call <- function(path, method = "get") {
.check_auth()
resp <- httr::GET(api_url(path), httr::add_headers(
......@@ -187,6 +190,21 @@ api_url <- function(path) {
paste0(.genesysEnv$server, "/api/v0", path)
}
#' Get full Genesys API v1 URL for a specific path
#'
#' @param path relative path of the API v1 endpoint (e.g. \code{/me})
#'
#' @return Absolute URL to an API call
#' @export
#'
#' @examples
#' api1_url("/me")
api1_url <- function(path) {
paste0(.genesysEnv$server, "/api/v1", path)
}
#' @keywords internal
.get <- function(path, query = NULL) {
.check_auth()
resp <- httr::GET(api_url(path), query = query, httr::add_headers(
......@@ -206,6 +224,7 @@ api_url <- function(path) {
#' @param content.type Content-Type of the body
#'
#' @return httr response
#' @keywords internal
.post <- function(path, query = NULL, body = NULL, content.type = "application/json") {
.check_auth()
content <- jsonlite::toJSON(body)
......
......@@ -13,6 +13,7 @@
# limitations under the License.
#' Max pages to retrieve
#' @keywords internal
.MAX_ALLOWED_PAGES <- 500
#' Who am i?
......@@ -40,6 +41,7 @@ me <- function() {
#' }
#'
#' @return Paged data structure
#' @keywords internal
.fetch_accessions_page <- function(filters = list(), page = 0, size = 1000, selector = NULL) {
start_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15)
resp <- .post(path = "/acn/filter", query=list(p = page, l = size), body = filters)
......@@ -115,3 +117,76 @@ fetch_accessions <- function(filters = list(), page = NULL, size = 1000, selecto
paged
}
#' Download all passport data for one genebank in Excel format and save it to disk
#'
#' @param instituteCode FAO WIEWS institute code
#' @param file Target file name. Defaults to Genesys-provided file name in the current working directory.
#'
#' @examples
#' \dontrun{
#' # Download MCPD passport data for NGA039
#' excelData <- download_mcpd("NGA039")
#' }
#'
#' @export
#' @return HTTP response data
download_mcpd <- function(instituteCode, file = NULL) {
if (is.na(instituteCode)) {
stop("instituteCode parameter is required")
}
if (is.null(file)) {
file <- paste0("genesys-accessions-", instituteCode, ".xlsx")
}
resp <- httr::POST(
api1_url(paste0("/wiews/", instituteCode, "/download")),
body = list(mcpd = "mcpd"), encode = "form",
httr::write_disk(file),
httr::add_headers(
Authorization = .genesysEnv$Authorization
)) # , httr::verbose())
if (httr::status_code(resp) != 200) {
stop("Genesys responded with HTTP status code ", httr::status_code(resp), ". Expected 200.")
}
invisible(resp)
}
#' Download PDCI data for one genebank in Excel format and save it to disk.
#'
#' @param instituteCode FAO WIEWS institute code
#' @param file Target file name. Defaults to Genesys-provided file name in the current working directory.
#'
#' @examples
#' \dontrun{
#' # Download PDCI data for NGA039
#' excelData <- download_pdci("NGA039")
#' }
#'
#' @export
#' @return HTTP response data
download_pdci <- function(instituteCode, file = NULL) {
if (is.na(instituteCode)) {
stop("instituteCode parameter is required")
}
if (is.null(file)) {
file <- paste0("genesys-pdci-", instituteCode, ".xlsx")
}
resp <- httr::POST(
api1_url(paste0("/wiews/", instituteCode, "/download")),
body = list(pdci = "pdci"), encode = "form",
httr::write_disk(file),
httr::add_headers(
Authorization = .genesysEnv$Authorization
)) # , httr::verbose())
if (httr::status_code(resp) != 200) {
stop("Genesys responded with HTTP status code ", httr::status_code(resp), ". Expected 200.")
}
invisible(resp)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment