From 92d0f1d8095385863423271034596f39e5b0b23a Mon Sep 17 00:00:00 2001 From: Scott Chamberlain Date: Tue, 8 Jan 2019 16:06:46 -0800 Subject: [PATCH] fix #298 better failure behavior for swdi fxns during govt shutdown, bump vers --- DESCRIPTION | 2 +- R/swdi.r | 12 +++++++----- R/zzz.r | 7 +++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 54f3ade8..9eaf6d09 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -7,7 +7,7 @@ Description: Client for many 'NOAA' data sources including the 'NCDC' climate for 'NOAA' sea ice data, the 'NOAA' severe weather inventory, 'NOAA' Historical Observing 'Metadata' Repository ('HOMR') data, 'NOAA' storm data via 'IBTrACS', tornado data via the 'NOAA' storm prediction center, and more. -Version: 0.8.2 +Version: 0.8.2.9321 License: MIT + file LICENSE Encoding: UTF-8 Language: en-US diff --git a/R/swdi.r b/R/swdi.r index 4497392f..b67ccd1a 100644 --- a/R/swdi.r +++ b/R/swdi.r @@ -152,14 +152,16 @@ swdi <- function(dataset=NULL, format='xml', startdate=NULL, enddate=NULL, crul::url_build(base, tmp$path, tmp$parameter) } url <- make_key(url, args) + cli <- crul::HttpClient$new(url = url, opts = list(...)) + temp <- cli$get(disk = filepath) if (format == 'shp') { filepath <- paste0(filepath, ".zip") - download.file(url, destfile = filepath) - message(sprintf("Zip file downloaded to %s", filepath)) - } else if (format == 'kmz') { - download.file(url, destfile = filepath) - message(sprintf("kmz file downloaded to %s", filepath)) } + if (any(grepl("shutdown", unlist(temp$response_headers_all)))) { + on.exit(unlink(filepath), add = TRUE) + stop("there's a government shutdown; check back later") + } + message(sprintf("%s file downloaded to %s", format, filepath)) } else { if (length(args) == 0) args <- NULL cli <- crul::HttpClient$new(url = url, opts = list(...)) diff --git a/R/zzz.r b/R/zzz.r index d6c7e46a..7b2e9d19 100644 --- a/R/zzz.r +++ b/R/zzz.r @@ -129,6 +129,13 @@ check_response_swdi <- function(x, format){ warning(x$status_http()$message) } } else { + # government shutdown check + if ( + grepl("html", x$response_headers$`content-type`) && + grepl("shutdown", x$parse("UTF-8")) + ) { + stop("there's a government shutdown; check back later") + } if (format == 'csv') { stopifnot(grepl('text/plain', x$response_headers$`content-type`)) read.delim(text = x$parse("UTF-8"), sep = ",")