Skip to content

Commit

Permalink
extract some common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
boshek committed Nov 17, 2024
1 parent 0410aba commit 285db24
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
54 changes: 7 additions & 47 deletions R/realtime-webservice.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,58 +83,18 @@ realtime_ws <- function(station_number,
)
}

if (!is.numeric(parameters)) stop("parameters should be a number", call. = FALSE)

if (inherits(start_date, "Date")) start_date <- paste0(start_date, " 00:00:00")
if (inherits(end_date, "Date")) end_date <- paste0(end_date, " 23:59:59")


if (!grepl("[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]", start_date)) {
stop(
"Invalid date format. start_date need to be in either YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats",
call. = FALSE
)
}

if (!grepl("[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]", end_date)) {
stop(
"Invalid date format. start_date need to be in either YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats",
call. = FALSE
)
}


if (!is.null(start_date) & !is.null(end_date)) {
if (lubridate::ymd_hms(end_date) < lubridate::ymd_hms(start_date)) {
stop(
"start_date is after end_date. Try swapping values.",
call. = FALSE
)
}
}

## Check date is in the right format
if (is.na(as.Date(start_date, format = "%Y-%m-%d")) | is.na(as.Date(end_date, format = "%Y-%m-%d"))) {
stop("Invalid date format. Dates need to be in YYYY-MM-DD format")
}
validate_params(parameters, start_date, end_date)

## Build link for GET
baseurl <- "https://wateroffice.ec.gc.ca/services/real_time_data/csv/inline?"


station_string <- paste0("stations[]=", station_number, collapse = "&")
parameters_string <- paste0("parameters[]=", parameters, collapse = "&")
date_string <- paste0(
"start_date=", substr(start_date, 1, 10), "%20", substr(start_date, 12, 19),
"&end_date=", substr(end_date, 1, 10), "%20", substr(end_date, 12, 19)
)

## paste them all together
query_url <- paste0(
browser()
query_url <- construct_url(
baseurl,
station_string, "&",
parameters_string, "&",
date_string
station_number,
parameters,
start_date,
end_date
)

## Get data
Expand Down
58 changes: 58 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,62 @@ tidyhydat_perform <- function(req, ...) {
req <- httr2::req_retry(req, max_tries = 5)
req <- httr2::req_progress(req)
httr2::req_perform(req, ...)
}

validate_params <- function(parameters, start_date, end_date) {


if (!is.numeric(parameters)) stop("parameters should be a number", call. = FALSE)

if (inherits(start_date, "Date")) start_date <- paste0(start_date, " 00:00:00")
if (inherits(end_date, "Date")) end_date <- paste0(end_date, " 23:59:59")


if (!grepl("[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]", start_date)) {
stop(
"Invalid date format. start_date need to be in either YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats",
call. = FALSE
)
}

if (!grepl("[0-9]{4}-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]", end_date)) {
stop(
"Invalid date format. start_date need to be in either YYYY-MM-DD or YYYY-MM-DD HH:MM:SS formats",
call. = FALSE
)
}


if (!is.null(start_date) & !is.null(end_date)) {
if (lubridate::ymd_hms(end_date) < lubridate::ymd_hms(start_date)) {
stop(
"start_date is after end_date. Try swapping values.",
call. = FALSE
)
}
}

## Check date is in the right format
if (is.na(as.Date(start_date, format = "%Y-%m-%d")) | is.na(as.Date(end_date, format = "%Y-%m-%d"))) {
stop("Invalid date format. Dates need to be in YYYY-MM-DD format")
}

invisible(TRUE)
}

construct_url <- function(baseurl, station_number, parameters, start_date, end_date) {
station_string <- paste0("stations[]=", station_number, collapse = "&")
parameters_string <- paste0("parameters[]=", parameters, collapse = "&")
date_string <- paste0(
"start_date=", substr(start_date, 1, 10), "%20", substr(start_date, 12, 19),
"&end_date=", substr(end_date, 1, 10), "%20", substr(end_date, 12, 19)
)

## paste them all together
paste0(
baseurl,
station_string, "&",
parameters_string, "&",
date_string
)
}

0 comments on commit 285db24

Please sign in to comment.