...
 
Commits (2)
Package: genesysr
Version: 0.9.1
Title: Genesys PGR Client
Encoding: UTF8
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>.
Authors@R: c(person(family = "Global Crop Diversity Trust", role = c("cph")),
person("Matija", "Obreza", email = "matija.obreza@croptrust.org", role = c("aut", "cre")))
person("Matija", "Obreza", email = "matija.obreza@croptrust.org", role = c("aut", "cre")),
person("Nora", "Castañeda", email = "nora.castaneda@croptrust.org", role = c("ctb")))
Maintainer: Matija Obreza <matija.obreza@croptrust.org>
Depends: R (>= 3.1.0)
Imports:
......
......@@ -177,7 +177,7 @@ api_url <- function(path) {
}
get <- function(path, query = NULL) {
resp <- httr::GET(api_url(path), httr::add_headers(
resp <- httr::GET(api_url(path), query = query, httr::add_headers(
Authorization = .genesysEnv$Authorization
))
if (httr::http_type(resp) != "application/json") {
......@@ -186,11 +186,24 @@ get <- function(path, query = NULL) {
resp
}
post <- function(path, query = NULL, body) {
resp <- httr::POST(api_url(path), httr::add_headers(
#' HTTP POST method
#'
#' @param path API path
#' @param query query string parameters
#' @param body request body (will be serialized to JSON)
#'
#' @return httr response
post <- function(path, query = NULL, body = NULL, content.type = "application/json") {
content <- jsonlite::toJSON(body)
if (! is.null(body) && length(body) == 0) {
# If body is provided, but has length of 0
content <- "{}"
}
# print(paste("Body is:", content))
resp <- httr::POST(api_url(path), query = query, httr::add_headers(
Authorization = .genesysEnv$Authorization,
"Content-Type" = "application/json"
), body = jsonlite::toJSON(body))
"Content-Type" = content.type
), body = content)
if (httr::http_type(resp) != "application/json") {
stop("API did not return json", call. = FALSE)
}
......
......@@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#' Default page size
FETCH_PAGESIZE <- 100
MAX_ALLOWED_PAGES <- 100
#' Who am i?
#'
......@@ -22,21 +25,79 @@ me <- function() {
invisible(resp)
}
#' Fetch accession passport data
#' Fetch accession passport data (paginated)
#'
#' @param filters an R \code{structure} with Genesys filters
#' @param size number of records to load per page (page size)
#' @param page the page index (0-based)
#' @param selector NULL or a function to "select" variables of interest
#'
#' @seealso \code{\link{mcpd_filter}}
#'
#' @examples
#' # Retrieve accession data by DOI
#' # Retrieve accession data by country of origin
#' accessions <- fetch_accessions(mcpd_filter(ORIGCTY = c("DEU", "SVN")))
#'
#' @export
#' @return Paged data structure
fetch_accessions <- function(filters = list(), page = 0, size = 100) {
fetch_accessions_page <- function(filters = list(), page = 0, size = FETCH_PAGESIZE, selector = NULL) {
resp <- post(path = "/acn/filter", query=list(p = page, s = size), body = filters)
jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)
paged <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)
message(paste("Retrieved page", page + 1, "of", paged$totalPages, "with", paged$numberOfElements, "rows"))
# Apply selector
if (is.function(selector)) {
paged$content <- lapply(paged$content, selector)
}
paged
}
#' Fetch accession passport data
#'
#' @param filters an R \code{structure} with Genesys filters
#' @param size number of records to load per page (page size)
#' @param page the page index (0-based)
#' @param selector NULL or a function to "select" variables of interest
#'
#' @examples
#' # Retrieve all accession data by country of origin
#' accessions <- fetch_accessions(mcpd_filter(ORIGCTY = c("DEU", "SVN")))
#'
#' # Fetch Musa
#' musa <- genesysr::fetch_accessions(list(taxonomy.genus = c('Musa')))
#'
#' # Apply selector function
#' accessions <- fetch_accessions(mcpd_filter(ORIGCTY = c("DEU", "SVN")), selector = function(x) {
#' list(id = x$id, acceNumb = x$acceNumb, instCode = x$institute$code)
#' })
#'
#' @export
#' @return Paged data structure
fetch_accessions <- function(filters = list(), page = NULL, size = FETCH_PAGESIZE, selector = NULL) {
if (! is.null(page)) {
# Fetch page
return(fetch_accessions_page(filters, page, size, selector));
}
# Fetch first page to determine number of records
paged <- fetch_accessions_page(filters, page = 0, size, selector)
pages <- paged$totalPages
for (page in 1:pages) {
if (page > MAX_ALLOWED_PAGES) {
# Break if over 100 pages
return(page)
}
p <- fetch_accessions_page(filters, page, size, selector)
paged$content <- c(paged$content, p$content)
paged$last <- p$last
paged$numberOfElements <- paged$numberOfElements + p$numberOfElements
if (p$last) {
# print("Got last page")
break
}
}
paged
}