Commit 5ca33a29 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added list_crops to retrieve Genesys crops

parent 6a6e8e1d
......@@ -16,6 +16,7 @@ export(filter_ORIGCTY)
export(filter_SAMPSTAT)
export(filter_SPECIES)
export(get_accessions)
export(list_crops)
export(mcpd_filter)
export(me)
export(print_setup)
......
......@@ -205,13 +205,14 @@ api2_url <- function(path) {
}
#' @keywords internal
.get <- function(path, query = NULL) {
.get <- function(path, query = NULL, accept = "application/json") {
.check_auth()
resp <- httr::GET(path, query = query, httr::add_headers(
Authorization = .genesysEnv$Authorization
Authorization = .genesysEnv$Authorization,
"Accept" = accept
))
if (httr::http_type(resp) != "application/json") {
stop("API did not return json", call. = FALSE)
if (httr::http_type(resp) != accept) {
stop("API did not return ", accept, " but Content-Type: ", httr::content(resp), ". See response content:\n", httr::content(resp), call. = FALSE)
}
resp
}
......
......@@ -25,6 +25,7 @@ me <- function() {
invisible(resp)
}
#' Fetch accession passport data (paginated)
#'
#' @param filters an R \code{structure} with Genesys filters
......@@ -287,3 +288,40 @@ download_pdci <- function(instituteCode, file = NULL) {
invisible(resp)
}
#' Fetch Genesys crops. Note that the list of Genesys crops does not fully
#' correspond with various CROPNAME in MCPD provided by genebanks.
#'
#' @examples
#' \dontrun{
#' # Retrieve all Genesys crops
#' crops <- genesysr::list_crops()
#' }
#'
#' @export
#' @return Genesys crops
list_crops <- function() {
start_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15)
resp <- .get(path = api1_url("/crops/list"), accept = "text/csv")
if (httr::status_code(resp) != 200) {
stop("Genesys responded with HTTP status code ", httr::status_code(resp), ". Expected 200.")
}
if (httr::http_type(resp) != "text/csv") {
stop("API returned ", httr::http_type(resp), ". Expected text/csv.")
}
end_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15)
headers <- httr::headers(resp)
message(paste("Retrieved crops in", end_time - start_time, "ms."))
body <- httr::content(resp, "text")
if (nchar(trimws(body)) == 0) {
message(paste("Received 0 bytes"))
data <- data.frame()
} else {
data <- read.csv(text = body, quote = '"', sep = '\t', stringsAsFactors = FALSE)
}
data
}
Supports Markdown
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