|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +#' Check whether a reference exists on DataStore |
| 5 | +#' |
| 6 | +#' @description given a DataStore reference ID, the function will hit the DataStore API to determine whether the reference exists. If it cannot contact DataStore, it will stop with an error that states the connection failed. |
| 7 | +#' |
| 8 | +#' @param reference_id Integer. A 7-digit number indicating the datastore reference to be queried. |
| 9 | +#' @param secure Logical. Defaults to TRUE, indicating that the user is logged on to the NPS VPN (or is accessing the resource from an NPS office). Can be set to FALSE for non-NPS users. If `secure` is set to FALSE, `dev` is automatically set to FALSE. |
| 10 | +#' @param dev Logical. Defaults to FALSE, indicating that the user wishes to work with the production version of DataStore. Can be set to TRUE if the user wants to work with the development environment on DataStore. Setting dev = TRUE will only work if the user has the appropriate permissions. Requires secure = TRUE. |
| 11 | +#' |
| 12 | +#' @return logical. TRUE means the reference exists, false means it does not exist. |
| 13 | +#' @export |
| 14 | +#' |
| 15 | +#' @examples |
| 16 | +#' \dontrun{ |
| 17 | +#' check_ref_exists(1234567) |
| 18 | +#' check_ref_exists(1234567, secure = FALSE) |
| 19 | +#' } |
| 20 | +check_ref_exists <- function (reference_id, secure = TRUE, dev = FALSE) { |
| 21 | + |
| 22 | + test_req <- NULL |
| 23 | + if (secure == FALSE) { |
| 24 | + #using secure api should be an option though... |
| 25 | + url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id) |
| 26 | + } |
| 27 | + if (secure == TRUE) { |
| 28 | + if (dev == FALSE) { |
| 29 | + url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id) |
| 30 | + } |
| 31 | + if (dev == TRUE) { |
| 32 | + url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id) |
| 33 | + } |
| 34 | + } |
| 35 | + test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) |
| 36 | + status_code <- httr::stop_for_status(test_req)$status_code |
| 37 | + #if API call fails, alert user and remind them to log on to VPN: |
| 38 | + if (!status_code == 200) { |
| 39 | + stop("DataStore connection failed. Are you logged in to the VPN?\n") |
| 40 | + } |
| 41 | + |
| 42 | + #get API request info: |
| 43 | + ref_data <- jsonlite::fromJSON(httr::content(test_req, "text")) |
| 44 | + |
| 45 | + #if an invalid reference number was supplied (no reference found): |
| 46 | + if(length(ref_data) == 0){ |
| 47 | + return(FALSE) |
| 48 | + } else { |
| 49 | + return (TRUE) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#' Check whether reference is a data package |
| 54 | +#' |
| 55 | +#' @description The function tests whether a valid DataStore reference is a data package or not. If it is a data package, the value "TRUE" is returned. If it is not a data package, the value FALSE is returned. The default setting assumes the user is logged on to the VPN (secure = TRUE), but this can be set to secure = FALSE for non-NPS users. This is a relatively simple helper function that will not test whether a reference exists and may produce unexpected errors if the reference does not exist or is not accessible. You are advised to run `check_reference_exists()` prior to using this function. |
| 56 | +#' |
| 57 | +#' @inheritParams check_ref_exists |
| 58 | +#' |
| 59 | +#' @return Logical.FALSE if the DataStore reference is not a data package. TRUE if the DataStore reference is a data package. |
| 60 | +#' @export |
| 61 | +#' |
| 62 | +#' @examples |
| 63 | +#' \dontrun{ |
| 64 | +#' check_is_data_package(1234567) |
| 65 | +#' check_is_data_package(1234567, secure = FALSE) |
| 66 | +#' } |
| 67 | +check_is_data_package <- function(reference_id, secure = TRUE, dev = FALSE) { |
| 68 | + test_req <- NULL |
| 69 | + |
| 70 | + test_req <- NULL |
| 71 | + if (secure == FALSE) { |
| 72 | + #using secure api should be an option though... |
| 73 | + url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id) |
| 74 | + } |
| 75 | + if (secure == TRUE) { |
| 76 | + if (dev == FALSE) { |
| 77 | + url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id) |
| 78 | + } |
| 79 | + if (dev == TRUE) { |
| 80 | + url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) |
| 85 | + status_code <- httr::stop_for_status(test_req)$status_code |
| 86 | + #if API call fails, alert user and remind them to log on to VPN: |
| 87 | + if (!status_code == 200) { |
| 88 | + stop("DataStore connection failed. Are you logged in to the VPN?\n") |
| 89 | + } |
| 90 | + |
| 91 | + #get reference info: |
| 92 | + ref_data <- jsonlite::fromJSON(httr::content(test_req, "text")) |
| 93 | + ref_type <- ref_data$referenceType |
| 94 | + |
| 95 | + #test whether it is a data package or not: |
| 96 | + if (!identical(ref_type, "Data Package")) { |
| 97 | + return(FALSE) |
| 98 | + } else { |
| 99 | + return(TRUE) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +#' Checks whether a reference has a more recent version |
| 105 | +#' |
| 106 | +#' @description |
| 107 | +#' This function tests whether an existing and accessible DataStore reference |
| 108 | +#' has a newer version or not. If a newer version exists, the value "TRUE" is returned. If a newer version does not exist, the value FALSE is returned. The default setting assumes the user is logged on to the VPN (secure = TRUE), but this can be set to secure = FALSE for non-NPS users. This is a relatively simple helper function that will not test whether a reference exists and may produce unexpected errors if the reference does not exist or is not accessible. You are advised to run `check_reference_exists()` prior to using this function. |
| 109 | +#' |
| 110 | +#' @inheritParams check_ref_exists |
| 111 | +#' |
| 112 | +#' @return Logical. TRUE if a newer version exists, FALSE if a newer version |
| 113 | +#' does not exist. |
| 114 | +#' @export |
| 115 | +#' |
| 116 | +#' @examples |
| 117 | +#' \dontrun{ |
| 118 | +#' check_new_version(1234567) |
| 119 | +#' check_new_version(1234567, secure = FALSE) |
| 120 | +#' } |
| 121 | +check_new_version <- function(reference_id, secure = TRUE, dev = FALSE) { |
| 122 | + test_req <- NULL |
| 123 | + |
| 124 | + test_req <- NULL |
| 125 | + if (secure == FALSE) { |
| 126 | + #using secure api should be an option though... |
| 127 | + url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id) |
| 128 | + } |
| 129 | + if (secure == TRUE) { |
| 130 | + if (dev == FALSE) { |
| 131 | + url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id) |
| 132 | + } |
| 133 | + if (dev == TRUE) { |
| 134 | + url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) |
| 139 | + status_code <- httr::stop_for_status(test_req)$status_code |
| 140 | + #if API call fails, alert user and remind them to log on to VPN: |
| 141 | + if (!status_code == 200) { |
| 142 | + stop("DataStore connection failed. Are you logged in to the VPN?\n") |
| 143 | + } |
| 144 | + |
| 145 | + #get request info: |
| 146 | + ref_data <- jsonlite::fromJSON(httr::content(test_req, "text")) |
| 147 | + #check for a newer version: |
| 148 | + version <-ref_data$mostRecentVersion |
| 149 | + |
| 150 | + #test for newer version: |
| 151 | + if (!is.na(version)) { |
| 152 | + return(TRUE) |
| 153 | + } else { |
| 154 | + return(FALSE) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +#' Get newest DataStore reference version |
| 159 | +#' |
| 160 | +#' @description If a DataStore reference has been versioned, `get_new_version_id()` will return the DataStore reference ID for the newest version. If the reference has not been versioned, the function returns NULL. |
| 161 | +#' |
| 162 | +#' @inheritParams check_ref_exists |
| 163 | +#' |
| 164 | +#' @return Integer. A 7-digit integer corresponding to the DataStore reference ID for the newest version of the supplied DataStore reference. If the supplied reference has not been versioned, NULL is returned. |
| 165 | +#' @export |
| 166 | +#' |
| 167 | +#' @examples |
| 168 | +#' \dontrun{ |
| 169 | +#' get_new_version_id(1234567) |
| 170 | +#' get_new_version_id(1234567, secure = FALSE) |
| 171 | +#' } |
| 172 | +get_new_version_id <- function(reference_id, secure = TRUE, dev = FALSE) { |
| 173 | + #gets a newer version (assuming a newer version exists) |
| 174 | + test_req <- NULL |
| 175 | + |
| 176 | + test_req <- NULL |
| 177 | + if (secure == FALSE) { |
| 178 | + #using secure api should be an option though... |
| 179 | + url <- paste0(.ds_api(), "ReferenceCodeSearch?q=", reference_id) |
| 180 | + } |
| 181 | + if (secure == TRUE) { |
| 182 | + if (dev == FALSE) { |
| 183 | + url <- paste0(.ds_secure_api(), "ReferenceCodeSearch?q=", reference_id) |
| 184 | + } |
| 185 | + if (dev == TRUE) { |
| 186 | + url <- paste0(.ds_dev_api(), "ReferenceCodeSearch?q=", reference_id) |
| 187 | + } |
| 188 | + } |
| 189 | + test_req <- httr::GET(url, httr::authenticate(":", ":", "ntlm")) |
| 190 | + status_code <- httr::stop_for_status(test_req)$status_code |
| 191 | + #if API call fails, alert user and remind them to log on to VPN: |
| 192 | + if (!status_code == 200) { |
| 193 | + stop("DataStore connection failed. Are you logged in to the VPN?\n") |
| 194 | + } |
| 195 | + |
| 196 | + #get request info: |
| 197 | + ref_data <- jsonlite::fromJSON(httr::content(test_req, "text")) |
| 198 | + #check for a newer version: |
| 199 | + version <-ref_data$mostRecentVersion |
| 200 | + |
| 201 | + if (!is.na(version)) { |
| 202 | + return(version) |
| 203 | + } |
| 204 | + if (is.na(version)) { |
| 205 | + return(NULL) |
| 206 | + } |
| 207 | +} |
0 commit comments