From 0b04ed78979a664119c6ff9dfabdb66ec0ac929a Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Tue, 9 Jul 2019 19:50:08 +0200 Subject: [PATCH 1/8] Update package imports & suggestions --- DESCRIPTION | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d994645..7aff7b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: BacDiveR -Title: Programmatic Interface For The DSMZ' Bacterial Diversity Metadatabase +Title: Programmatic Interface For The Bacterial Diversity + Metadatabase by DSMZ Version: 0.9.0 -Date: 2019-04-30 Authors@R: person(given = "Katrin", family = "Leinweber", @@ -12,23 +12,24 @@ Description: 'BacDive' focuses on providing organism-linked information covering the multifarious aspects of bacterial biodiversity: taxonomic classification, morphology, physiology, cultivation, origin natural habitat and more, both for bacterial and - archaeal strains (see Söhngen et al. (2014) ). - BacDiveR helps you investigate this data offline, and semi-automatically in R. + archaeal strains (see Söhngen et al. (2014) + ). BacDiveR helps you investigate this data + offline, and semi-automatically in R. License: MIT + file LICENSE URL: https://github.com/TIBHannover/BacDiveR BugReports: https://github.com/TIBHannover/BacDiveR/issues Imports: - httr (>= 1.3.0), - jsonlite (>= 1.5), + httr (>= 1.4.0), + jsonlite (>= 1.6), utils (>= 3.5.0) Suggests: - ggplot2 (>= 3.0.0), - knitr (>= 1.20), + ggplot2 (>= 3.2.0), + knitr (>= 1.23), magrittr (>= 1.5), - purrr (>= 0.2.5), - rmarkdown (>= 1.10), - spelling (>= 1.2), - testthat (>= 2.0.0) + purrr (>= 0.3.2), + rmarkdown (>= 1.13), + spelling (>= 2.1), + testthat (>= 2.1.1) VignetteBuilder: knitr ByteCompile: true From b3fe6c0081babea632cfafae5371f8ae7067c9d5 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber <9948149+katrinleinweber@users.noreply.github.com> Date: Fri, 12 Jul 2019 17:03:44 +0200 Subject: [PATCH 2/8] Let Travis use only R's development version --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 8cb63e9..c661580 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r language: R +r: devel sudo: false cache: packages From 55217e9efe821822351c2ceffc660b1a3bab4a33 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Thu, 25 Jul 2019 15:45:13 +0200 Subject: [PATCH 3/8] Construct URLs more elegantly --- R/bd_retrieve_data.R | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/R/bd_retrieve_data.R b/R/bd_retrieve_data.R index c7afae5..7f2b7e4 100644 --- a/R/bd_retrieve_data.R +++ b/R/bd_retrieve_data.R @@ -93,13 +93,12 @@ sanitise_type <- function(searchType) { construct_url <- function(searchTerm, searchType = "bacdive_id") { utils::URLencode( - paste0( - "https://bacdive.dsmz.de/api/bacdive/", + paste( + "https://bacdive.dsmz.de/api/bacdive", searchType, - "/", searchTerm, - "/", - "?format=json" + "?format=json", + sep = "/" ) ) } From e148c9c741d233249c026988850c6673f117ce40 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Tue, 6 Aug 2019 18:18:48 +0200 Subject: [PATCH 4/8] Refactor downloader to accept credentials input --- R/util-download.R | 8 ++++---- man/download.Rd | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/R/util-download.R b/R/util-download.R index 6360739..fdf1af9 100644 --- a/R/util-download.R +++ b/R/util-download.R @@ -1,16 +1,16 @@ #' Download Something from BacDive #' +#' Both `user` and `password` are read from the .Renviron file by default. +#' #' @param URL A correctly encoded character string, either from construct_url() #' or a JSON list #' #' @return The R object deserialised from the downloaded JSON #' @keywords internal -download <- function(URL) { +download <- function(URL, user = get_credentials()[1], password = get_credentials()[2]) { message(URLs_to_IDs(URL), " ", appendLF = FALSE) - cred <- get_credentials() - - response <- httr::GET(URL, httr::authenticate(cred[1], cred[2])) + response <- httr::GET(URL, httr::authenticate(user, password)) payload <- httr::content(response, as = "text", encoding = "UTF-8") jsonlite::fromJSON(payload) } diff --git a/man/download.Rd b/man/download.Rd index 1edf789..51b0ee2 100644 --- a/man/download.Rd +++ b/man/download.Rd @@ -4,7 +4,8 @@ \alias{download} \title{Download Something from BacDive} \usage{ -download(URL) +download(URL, user = get_credentials()[1], + password = get_credentials()[2]) } \arguments{ \item{URL}{A correctly encoded character string, either from construct_url() @@ -14,6 +15,6 @@ or a JSON list} The R object deserialised from the downloaded JSON } \description{ -Download Something from BacDive +Both \code{user} and \code{password} are read from the .Renviron file by default. } \keyword{internal} From d7a2526e2a9a2bf0415ee55f052bdfc0acf0d09f Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Tue, 6 Aug 2019 18:27:57 +0200 Subject: [PATCH 5/8] Raise error if credentials are invalid (fix #110) --- DESCRIPTION | 2 +- NEWS.md | 7 +++++++ R/util-download.R | 8 +++++++- tests/testthat/test-download.R | 4 ++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/test-download.R diff --git a/DESCRIPTION b/DESCRIPTION index 7aff7b0..49f9587 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: BacDiveR Title: Programmatic Interface For The Bacterial Diversity Metadatabase by DSMZ -Version: 0.9.0 +Version: 0.9.1 Authors@R: person(given = "Katrin", family = "Leinweber", diff --git a/NEWS.md b/NEWS.md index e09da23..73ffaf3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,13 @@ ### Fixed ### Security + +## BacDiveR 0.9.1 + +### Fixed + +- Expose a previously silent download error (#110; Thanks to @jfy133!) + ## BacDiveR 0.9.0 All notable changes to this project will be documented in this file. diff --git a/R/util-download.R b/R/util-download.R index fdf1af9..6ed9c7c 100644 --- a/R/util-download.R +++ b/R/util-download.R @@ -12,5 +12,11 @@ download <- function(URL, user = get_credentials()[1], password = get_credential response <- httr::GET(URL, httr::authenticate(user, password)) payload <- httr::content(response, as = "text", encoding = "UTF-8") - jsonlite::fromJSON(payload) + data <- jsonlite::fromJSON(payload) + + if (response$status_code == 403) { + stop(paste(data, "\nCheck your .Renviron file, and try copy-pasting your login credentials into https://bacdive.dsmz.de/api/bacdive/ to test them. Correct as necessary and try your data download again.")) + } else { + return(data) + } } diff --git a/tests/testthat/test-download.R b/tests/testthat/test-download.R new file mode 100644 index 0000000..2d947f3 --- /dev/null +++ b/tests/testthat/test-download.R @@ -0,0 +1,4 @@ +test_that("Downloading without preper credentials raises an error", { + random <- sample(letters, 8) + expect_error(download(construct_url(717), user = random, password = random)) +}) From 1e2012dc16290d993b62de11f623a682d12a3673 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Tue, 6 Aug 2019 18:34:35 +0200 Subject: [PATCH 6/8] Refactor HTTP-related imports --- BacDiveR.Rproj | 1 + NAMESPACE | 2 ++ R/util-download.R | 8 +++++--- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/BacDiveR.Rproj b/BacDiveR.Rproj index 49f2092..1440aa0 100644 --- a/BacDiveR.Rproj +++ b/BacDiveR.Rproj @@ -17,3 +17,4 @@ StripTrailingWhitespace: Yes BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/NAMESPACE b/NAMESPACE index ec17439..c7ca7ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,5 @@ export(bd_retrieve_by_search) export(bd_retrieve_by_sequence) export(bd_retrieve_taxon) export(prepare_Renviron) +import(httr) +importFrom(jsonlite,fromJSON) diff --git a/R/util-download.R b/R/util-download.R index 6ed9c7c..2e867ad 100644 --- a/R/util-download.R +++ b/R/util-download.R @@ -7,12 +7,14 @@ #' #' @return The R object deserialised from the downloaded JSON #' @keywords internal +#' @import httr +#' @importFrom jsonlite fromJSON download <- function(URL, user = get_credentials()[1], password = get_credentials()[2]) { message(URLs_to_IDs(URL), " ", appendLF = FALSE) - response <- httr::GET(URL, httr::authenticate(user, password)) - payload <- httr::content(response, as = "text", encoding = "UTF-8") - data <- jsonlite::fromJSON(payload) + response <- GET(URL, authenticate(user, password)) + payload <- content(response, as = "text", encoding = "UTF-8") + data <- fromJSON(payload) if (response$status_code == 403) { stop(paste(data, "\nCheck your .Renviron file, and try copy-pasting your login credentials into https://bacdive.dsmz.de/api/bacdive/ to test them. Correct as necessary and try your data download again.")) From 30bcffc4c48faab41daf7c8ab0afa54bfa019707 Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Wed, 7 Aug 2019 18:44:01 +0200 Subject: [PATCH 7/8] Summarise changes since v0.9.0 --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index 73ffaf3..4f792af 100644 --- a/NEWS.md +++ b/NEWS.md @@ -14,6 +14,12 @@ - Expose a previously silent download error (#110; Thanks to @jfy133!) +### Changed + +- Repair outdated links in documentation +- Improve some code sections in minor ways + + ## BacDiveR 0.9.0 All notable changes to this project will be documented in this file. From 42367fd103fd123a216964127a7d67411020d73f Mon Sep 17 00:00:00 2001 From: Katrin Leinweber Date: Wed, 7 Aug 2019 18:57:39 +0200 Subject: [PATCH 8/8] Run styler:::style_active_pkg() --- R/bd_retrieve.R | 4 +-- R/bd_retrieve_by_culture.R | 2 +- R/bd_retrieve_by_search.R | 36 ++++++++++++--------- R/bd_retrieve_by_sequence.R | 2 +- R/util-get_credentials.R | 3 +- man/bd_retrieve.Rd | 12 +++---- man/bd_retrieve_by_search.Rd | 20 ++++++------ man/get_credentials.Rd | 3 +- tests/testthat/test-bd_retrieve_by_search.R | 9 ++++-- tests/testthat/test-utils.R | 3 +- 10 files changed, 55 insertions(+), 39 deletions(-) diff --git a/R/bd_retrieve.R b/R/bd_retrieve.R index 6299125..f9cc0fd 100644 --- a/R/bd_retrieve.R +++ b/R/bd_retrieve.R @@ -10,8 +10,8 @@ #' #' @export #' @examples -#' dataset_717 <- bd_retrieve(id = 717) -#' dataset_717 <- bd_retrieve(id = "717") +#' dataset_717 <- bd_retrieve(id = 717) +#' dataset_717 <- bd_retrieve(id = "717") #' @rdname bd_retrieve bd_retrieve <- function(id) { bd_retrieve_data(searchTerm = as.character(id), searchType = "bacdive_id") diff --git a/R/bd_retrieve_by_culture.R b/R/bd_retrieve_by_culture.R index 216ab5b..5e1c98a 100644 --- a/R/bd_retrieve_by_culture.R +++ b/R/bd_retrieve_by_culture.R @@ -3,7 +3,7 @@ #' number by which the associated dataset(s) will be retrieved. #' @export #' @examples -#' dataset_DSM_319 <- bd_retrieve_by_culture(collection_no = "DSM 319") +#' dataset_DSM_319 <- bd_retrieve_by_culture(collection_no = "DSM 319") bd_retrieve_by_culture <- function(collection_no) { bd_retrieve_data(searchTerm = collection_no, searchType = "culturecollectionno") } diff --git a/R/bd_retrieve_by_search.R b/R/bd_retrieve_by_search.R index a1f5e48..e63b1df 100644 --- a/R/bd_retrieve_by_search.R +++ b/R/bd_retrieve_by_search.R @@ -12,23 +12,27 @@ #' @export #' #' @examples -#' plant_animal_pathogens <- -#' bd_retrieve_by_search( -#' queryURL = paste( -#' "https://bacdive.dsmz.de/advsearch?site=advsearchsearch", -#' "params%5B5%5D%5Bsearchterm%5D=1", -#' "searchparams%5B157%5D%5Bsearchterm%5D=1", -#' "searchparams%5B158%5D%5Bsearchterm%5D=1", -#' "advsearch=search", sep = "&") -#' ) +#' plant_animal_pathogens <- +#' bd_retrieve_by_search( +#' queryURL = paste( +#' "https://bacdive.dsmz.de/advsearch?site=advsearchsearch", +#' "params%5B5%5D%5Bsearchterm%5D=1", +#' "searchparams%5B157%5D%5Bsearchterm%5D=1", +#' "searchparams%5B158%5D%5Bsearchterm%5D=1", +#' "advsearch=search", +#' sep = "&" +#' ) +#' ) bd_retrieve_by_search <- function(queryURL) { # guard against other URLs if (!grepl(pattern = "^https:\\/\\/bacdive\\.dsmz\\.de\\/advsearch", queryURL) | - !grepl("[?&]site=advsearch", queryURL) | - !grepl("[?&]advsearch=search", queryURL) | - !grepl("\\&searchparams", queryURL)) { + !grepl("[?&]site=advsearch", queryURL) | + !grepl("[?&]advsearch=search", queryURL) | + !grepl("\\&searchparams", queryURL)) { stop( - "I'm sorry, but this doesn't seem like an advanced search URL from https://BacDive.DSMZ.de/advsearch! Aborting...\nPlease read https://TIBHannover.GitHub.io/BacDiveR/#how-to-use" + "I'm sorry, but this doesn't seem like an advanced search URL from + https://BacDive.DSMZ.de/advsearch! Aborting...\nPlease read + https://TIBHannover.GitHub.io/BacDiveR/#how-to-use" ) } @@ -46,8 +50,10 @@ bd_retrieve_by_search <- function(queryURL) { httr::content(response, as = "text", encoding = "UTF-8") if (grepl("^[[:digit:]]", payload)) { - aggregate_datasets(payload = strsplit(x = payload, split = "\\n")[[1]], - from_IDs = TRUE) + aggregate_datasets( + payload = strsplit(x = payload, split = "\\n")[[1]], + from_IDs = TRUE + ) } else if (grepl("^