Permalink
Browse files

Check http responses status before proceeding.

  • Loading branch information...
1 parent 8bb9b93 commit 7e8444af67a80580d483b97397be82bc83a4ecbe @jeroen jeroen committed Sep 5, 2015
Showing with 8 additions and 6 deletions.
  1. +1 −0 NAMESPACE
  2. +7 −6 R/cranlogs.R
View
@@ -4,4 +4,5 @@ export(cran_downloads)
export(cran_top_downloads)
importFrom(httr,GET)
importFrom(httr,content)
+importFrom(httr,stop_for_status)
importFrom(jsonlite,fromJSON)
View
@@ -3,8 +3,7 @@
#'
#' @docType package
#' @name cranlogs
-#' @importFrom httr GET
-#' @importFrom httr content
+#' @importFrom httr GET content stop_for_status
#' @importFrom jsonlite fromJSON
NULL
@@ -79,8 +78,9 @@ cran_downloads <- function(packages = NULL,
ppackages <- paste0("/", ppackages)
}
- r <- fromJSON(content(GET(paste0(daily_url, interval, ppackages)), as = "text"),
- simplifyVector = FALSE)
+ req <- GET(paste0(daily_url, interval, ppackages))
+ stop_for_status(req)
+ r <- fromJSON(content(req, as = "text"), simplifyVector = FALSE)
if ("error" %in% names(r) && r$error == "Invalid query") {
stop("Invalid query, probably invalid dates")
@@ -162,8 +162,9 @@ cran_top_downloads <- function(when = c("last-day", "last-week",
"last-month"), count = 10) {
when <- match.arg(when)
- r <- fromJSON(content(GET(paste0(top_url, when, '/', count)), as = "text"),
- simplifyVector = FALSE)
+ req <- GET(paste0(top_url, when, '/', count))
+ stop_for_status(req)
+ r <- fromJSON(content(req, as = "text"), simplifyVector = FALSE)
df <- data.frame(
stringsAsFactors = FALSE,

0 comments on commit 7e8444a

Please sign in to comment.