From cd1a2ca1d26ea8d5c157931bb7ffd4b7a9065411 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Sat, 18 Apr 2020 16:44:29 -0400 Subject: [PATCH] progress bars and fixed issue when 0 api results came back --- DESCRIPTION | 7 ++++--- NAMESPACE | 1 + NEWS.md | 4 ++++ R/assessments.R | 2 ++ R/attackerkb-package.R | 4 ++++ R/contributor.R | 2 ++ R/topics.R | 2 ++ R/utils.R | 20 +++++++++++++++++--- man/attackerkb.Rd | 4 ++++ man/attackerkb_api_key.Rd | 2 +- man/kb_assessments.Rd | 3 +++ man/kb_contributors.Rd | 3 +++ man/kb_topics.Rd | 3 +++ 13 files changed, 50 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 01dcfb5..fa03ce7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: attackerkb Type: Package Title: Tools to Query the Rapid7 AttackerKB API -Version: 0.1.0 -Date: 2020-04-15 +Version: 0.1.1 +Date: 2020-04-18 Authors@R: c( person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"), comment = c(ORCID = "0000-0001-5670-2640")) @@ -22,6 +22,7 @@ Depends: Imports: httr, jsonlite, - data.table + data.table, + progress Roxygen: list(markdown = TRUE) RoxygenNote: 7.1.0 diff --git a/NAMESPACE b/NAMESPACE index 3ced161..40b83c9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,3 +10,4 @@ export(kd_contributor) import(httr) importFrom(data.table,rbindlist) importFrom(jsonlite,fromJSON) +importFrom(progress,progress_bar) diff --git a/NEWS.md b/NEWS.md index 9b4679b..0960271 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,2 +1,6 @@ +0.1.1 +* Fixed an issue where errors wld be thrown if zero API results came back +* Added progress spinner for API calls vs the hacky dots + 0.1.0 * Initial release diff --git a/R/assessments.R b/R/assessments.R index b712813..fd1295c 100644 --- a/R/assessments.R +++ b/R/assessments.R @@ -3,6 +3,8 @@ #' The main `kb_contributors` function maps 1:1 to the API. Leave values #' `NULL` that you do not want included in the search parameters. #' +#' Set the option `progress_enabled` to `FALSE` to disable progress spinner. +#' #' @param assessment_id UUID of a specific assessment to return #' @param editor_id UUID of a contributor. #' @param topid_id UUID of the topic this assessment was based on. diff --git a/R/attackerkb-package.R b/R/attackerkb-package.R index 3304931..2525a8f 100644 --- a/R/attackerkb-package.R +++ b/R/attackerkb-package.R @@ -4,6 +4,9 @@ #' various aspects of emergent or existing vulnerabilities and the community can query and #' retrieve results. Tools are provided to query the AttackerKB API. #' +#' For all API calls, set the option `progress_enabled` to `FALSE` to disable +#' progress spinner. +#' #' @md #' @name attackerkb #' @keywords internal @@ -11,4 +14,5 @@ #' @import httr #' @importFrom jsonlite fromJSON #' @importFrom data.table rbindlist +#' @importFrom progress progress_bar "_PACKAGE" diff --git a/R/contributor.R b/R/contributor.R index 4d5a062..66adb61 100644 --- a/R/contributor.R +++ b/R/contributor.R @@ -3,6 +3,8 @@ #' The main `kb_contributors` function maps 1:1 to the API. Leave values #' `NULL` that you do not want included in the search parameters. #' +#' Set the option `progress_enabled` to `FALSE` to disable progress spinner. +#' #' @param contributor_id UUID of a specific contributror to return #' @param username Return contributors with the matching username. #' @param avatar Return all contributors where avatar matches the given value diff --git a/R/topics.R b/R/topics.R index 1b17a11..ffe0167 100644 --- a/R/topics.R +++ b/R/topics.R @@ -3,6 +3,8 @@ #' The main `kb_topics` function maps 1:1 to the API. Leave values #' `NULL` that you do not want included in the search parameters. #' +#' Set the option `progress_enabled` to `FALSE` to disable progress spinner. +#' #' @param topic_id UUID of a specific topic to return #' @param editor_id UUID of a contributor #' @param name Text to query the name attribute. A substring match is performed diff --git a/R/utils.R b/R/utils.R index 9f09c8e..668a668 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,9 +1,18 @@ +date_convert <- function(.x) { + as.POSIXct(.x, format="%Y-%m-%dT%H:%M:%OS", tz="GMT") +} + .kb_reshape <- function(.x, path) { + if (length(.x) == 0) return(data.frame(stringsAsFactors=FALSE)) + if (path == "/topics") { easy_cols <- .x[, c("id", "editorId", "name", "created", "revisionDate", "disclosureDate", "document")] + easy_cols[["created"]] <- date_convert(easy_cols[["created"]]) + easy_cols[["revisionDate"]] <- date_convert(easy_cols[["revisionDate"]]) + easy_cols$references <- .x[["metadata"]][["references"]] cvss_v3 <- .x[["metadata"]][["baseMetricV3"]][["cvssV3"]] @@ -46,12 +55,16 @@ } else if (path == "/contributors") { + .x[["created"]] <- date_convert(.x[["created"]]) .x } else if (path == "/assessments") { easy_cols <- .x[,c("id", "editorId", "topicId", "created", "revisionDate", "document", "score")] + easy_cols[["created"]] <- date_convert(easy_cols[["created"]]) + easy_cols[["revisionDate"]] <- date_convert(easy_cols[["revisionDate"]]) + metadata <- easy_cols[["metadata"]] easy_cols$tags <- metadata[["tags"]] @@ -79,6 +92,9 @@ handle_response <- function(.x, api_key = attackerkb_api_key()) { + .pb <- progress::progress_bar$new(format = "(:spin)", total = NA) + .pb$tick() + path <- .x[["links"]][["self"]][["href"]] ret <- .x[["data"]] @@ -89,7 +105,7 @@ handle_response <- function(.x, api_key = attackerkb_api_key()) { while(length(next_href)) { - cat(".") + .pb$tick() httr::GET( url = sprintf("https://api.attackerkb.com%s", next_href), @@ -108,8 +124,6 @@ handle_response <- function(.x, api_key = attackerkb_api_key()) { } - cat("\n") - class(ret) <- c("tbl_df", "tbl", "data.frame") ret diff --git a/man/attackerkb.Rd b/man/attackerkb.Rd index fa941f7..40bc0d9 100644 --- a/man/attackerkb.Rd +++ b/man/attackerkb.Rd @@ -10,6 +10,10 @@ Rapid7 manages a service — \url{https://attackerkb.com/} - where experts can e various aspects of emergent or existing vulnerabilities and the community can query and retrieve results. Tools are provided to query the AttackerKB API. } +\details{ +For all API calls, set the option \code{progress_enabled} to \code{FALSE} to disable +progress spinner. +} \seealso{ Useful links: \itemize{ diff --git a/man/attackerkb_api_key.Rd b/man/attackerkb_api_key.Rd index f5e86dc..806b276 100644 --- a/man/attackerkb_api_key.Rd +++ b/man/attackerkb_api_key.Rd @@ -10,7 +10,7 @@ attackerkb_api_key(force = FALSE) \item{force}{Force setting a new AttackerKB key for the current environment?} } \value{ -atomic character vector containing the AttackerKB api key +atomic character vector containing the AttackerKB API key } \description{ The API wrapper functions in this package all rely on a AttackerKB API diff --git a/man/kb_assessments.Rd b/man/kb_assessments.Rd index c8cf005..6c4ab2b 100644 --- a/man/kb_assessments.Rd +++ b/man/kb_assessments.Rd @@ -45,6 +45,9 @@ kb_assessment(assessment_id, api_key = attackerkb_api_key()) The main \code{kb_contributors} function maps 1:1 to the API. Leave values \code{NULL} that you do not want included in the search parameters. } +\details{ +Set the option \code{progress_enabled} to \code{FALSE} to disable progress spinner. +} \references{ \url{https://api.attackerkb.com/api-docs/docs} } diff --git a/man/kb_contributors.Rd b/man/kb_contributors.Rd index 11235fc..e16c966 100644 --- a/man/kb_contributors.Rd +++ b/man/kb_contributors.Rd @@ -36,6 +36,9 @@ kd_contributor(contributor_id, api_key = attackerkb_api_key()) The main \code{kb_contributors} function maps 1:1 to the API. Leave values \code{NULL} that you do not want included in the search parameters. } +\details{ +Set the option \code{progress_enabled} to \code{FALSE} to disable progress spinner. +} \references{ \url{https://api.attackerkb.com/api-docs/docs} } diff --git a/man/kb_topics.Rd b/man/kb_topics.Rd index 0cf1fee..60e2488 100644 --- a/man/kb_topics.Rd +++ b/man/kb_topics.Rd @@ -51,6 +51,9 @@ kb_topic( The main \code{kb_topics} function maps 1:1 to the API. Leave values \code{NULL} that you do not want included in the search parameters. } +\details{ +Set the option \code{progress_enabled} to \code{FALSE} to disable progress spinner. +} \references{ \url{https://api.attackerkb.com/api-docs/docs} } -- 2.45.2