...
 
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("Nora", "Castañeda", email = "nora.castaneda@croptrust.org", role = c("ctb")))
person("Nora", "Castaneda", email = "nora.castaneda@croptrust.org", role = c("ctb")))
Maintainer: Matija Obreza <matija.obreza@croptrust.org>
Depends: R (>= 3.1.0)
Imports:
......
......@@ -76,6 +76,13 @@ authorization <- function(authorization) {
message(paste('Genesys Authorization:', authorization))
}
#' Ensure that environment has OAuth token
check_auth <- function() {
if (is.null(.genesysEnv$Authorization)) {
stop("You must first authorize with Genesys with user_login or client_login.");
}
}
#' Login to Genesys as a user
#'
#' The authorization URL will open in a browser, ask the user to grant
......@@ -149,6 +156,7 @@ client_login <- function() {
api_call <- function(path, method = "get") {
check_auth()
resp <- httr::GET(api_url(path), httr::add_headers(
Authorization = .genesysEnv$Authorization
)
......@@ -177,6 +185,7 @@ api_url <- function(path) {
}
get <- function(path, query = NULL) {
check_auth()
resp <- httr::GET(api_url(path), query = query, httr::add_headers(
Authorization = .genesysEnv$Authorization
))
......@@ -191,9 +200,11 @@ get <- function(path, query = NULL) {
#' @param path API path
#' @param query query string parameters
#' @param body request body (will be serialized to JSON)
#' @param content.type Content-Type of the body
#'
#' @return httr response
post <- function(path, query = NULL, body = NULL, content.type = "application/json") {
check_auth()
content <- jsonlite::toJSON(body)
if (! is.null(body) && length(body) == 0) {
# If body is provided, but has length of 0
......
......@@ -13,8 +13,8 @@
# limitations under the License.
#' Default page size
FETCH_PAGESIZE <- 100
MAX_ALLOWED_PAGES <- 100
FETCH_PAGESIZE <- 1000
MAX_ALLOWED_PAGES <- 500
#' Who am i?
#'
......@@ -41,6 +41,9 @@ me <- function() {
#' @return Paged data structure
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)
if (httr::status_code(resp) != 200) {
stop("Genesys responded with HTTP status code ", httr::status_code(resp), ". Expected 200.")
}
paged <- jsonlite::fromJSON(httr::content(resp, "text"), simplifyVector = FALSE)
message(paste("Retrieved page", page + 1, "of", paged$totalPages, "with", paged$numberOfElements, "rows"))
......@@ -84,7 +87,7 @@ fetch_accessions <- function(filters = list(), page = NULL, size = FETCH_PAGESIZ
for (page in 1:pages) {
if (page > MAX_ALLOWED_PAGES) {
# Break if over 100 pages
# Break if over max pages
return(page)
}
p <- fetch_accessions_page(filters, page, size, selector)
......