|
| 1 | +#' @title Supported http verbs packages |
| 2 | +#' @keywords internal |
| 3 | +#' @noRd |
| 4 | +#' @return Names of http verbs packages supported by sits |
| 5 | +.request_supported_packages <- function() { |
| 6 | + return("httr2") |
| 7 | +} |
| 8 | + |
| 9 | +#' @title Check for request package availability |
| 10 | +#' @name .request_check_package |
| 11 | +#' @keywords internal |
| 12 | +#' @noRd |
| 13 | +#' |
| 14 | +#' @return name of the package. |
| 15 | +.request_check_package <- function() { |
| 16 | + pkg_class <- .conf_request_pkg() |
| 17 | + class(pkg_class) <- pkg_class |
| 18 | + |
| 19 | + UseMethod(".request_check_package", pkg_class) |
| 20 | +} |
| 21 | + |
| 22 | +#' @title Perform a request |
| 23 | +#' @name .request |
| 24 | +#' @keywords internal |
| 25 | +#' @noRd |
| 26 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 27 | +#' |
| 28 | +#' @param req_obj A request object. |
| 29 | +#' @param ... Additional parameters to be passed to httr2 package |
| 30 | +#' |
| 31 | +#' @return A response object. |
| 32 | +.request <- function(req_obj, ...) { |
| 33 | + # check package |
| 34 | + pkg_class <- .request_check_package() |
| 35 | + |
| 36 | + # call function |
| 37 | + UseMethod(".request", pkg_class) |
| 38 | +} |
| 39 | + |
| 40 | +#' @title Retry a GET requisition |
| 41 | +#' @name .retry_request |
| 42 | +#' @keywords internal |
| 43 | +#' @noRd |
| 44 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 45 | +#' |
| 46 | +#' @param url A character with URL. |
| 47 | +#' @param n_tries A integer with the number with tried requisitions. |
| 48 | +#' @param sleep A integer with sleep time in seconds. |
| 49 | +#' @param ... Additional parameters to be passed to httr2 package |
| 50 | +#' |
| 51 | +#' @return A response object returned by the requisition package |
| 52 | +.retry_request <- function(url, n_tries = 10, sleep = 10, ...) { |
| 53 | + # check package |
| 54 | + pkg_class <- .request_check_package() |
| 55 | + |
| 56 | + # call function |
| 57 | + UseMethod(".retry_request", pkg_class) |
| 58 | +} |
| 59 | + |
| 60 | +#' @title GET requistion |
| 61 | +#' @name .get_request |
| 62 | +#' @keywords internal |
| 63 | +#' @noRd |
| 64 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 65 | +#' |
| 66 | +#' @param url A character with URL. |
| 67 | +#' @param query A named list with values to be passed in query. |
| 68 | +#' @param headers A named list with values to be passed to headers. |
| 69 | +#' @param ... Additional parameters to be passed to httr2 package |
| 70 | +#' |
| 71 | +#' @return A response object returned by the requisition package |
| 72 | +.get_request <- function(url, query = NULL, headers = NULL, ...) { |
| 73 | + # check package |
| 74 | + pkg_class <- .request_check_package() |
| 75 | + |
| 76 | + # call function |
| 77 | + UseMethod(".get_request", pkg_class) |
| 78 | +} |
| 79 | + |
| 80 | +#' @title Add query values into a request object |
| 81 | +#' @name .request_query |
| 82 | +#' @keywords internal |
| 83 | +#' @noRd |
| 84 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 85 | +#' |
| 86 | +#' @param req_obj A request object. |
| 87 | +#' @param query A named list with values to be passed in query. |
| 88 | +#' |
| 89 | +#' @return A request object returned by the requisition package. |
| 90 | +.request_query <- function(req_obj, query) { |
| 91 | + # check package |
| 92 | + pkg_class <- .request_check_package() |
| 93 | + |
| 94 | + # call function |
| 95 | + UseMethod(".request_query", pkg_class) |
| 96 | +} |
| 97 | + |
| 98 | +#' @title Add headers values into a request object |
| 99 | +#' @name .request_headers |
| 100 | +#' @keywords internal |
| 101 | +#' @noRd |
| 102 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 103 | +#' |
| 104 | +#' @param req_obj A request object. |
| 105 | +#' @param header A named list with values to be passed in headers. |
| 106 | +#' |
| 107 | +#' @return A request object returned by the requisition package. |
| 108 | +.request_headers <- function(req_obj, ...) { |
| 109 | + # check package |
| 110 | + pkg_class <- .request_check_package() |
| 111 | + |
| 112 | + # call function |
| 113 | + UseMethod(".request_headers", pkg_class) |
| 114 | +} |
| 115 | + |
| 116 | +#' @title Get response content from object |
| 117 | +#' @name .response_content |
| 118 | +#' @keywords internal |
| 119 | +#' @noRd |
| 120 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 121 | +#' |
| 122 | +#' @param resp_obj A response object. |
| 123 | +#' |
| 124 | +#' @return A list with content values returned by the response. |
| 125 | +.response_content <- function(resp_obj) { |
| 126 | + # check package |
| 127 | + pkg_class <- .request_check_package() |
| 128 | + |
| 129 | + # call function |
| 130 | + UseMethod(".response_content", pkg_class) |
| 131 | +} |
| 132 | + |
| 133 | +#' @title Get response status from object |
| 134 | +#' @name .response_status |
| 135 | +#' @keywords internal |
| 136 | +#' @noRd |
| 137 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 138 | +#' |
| 139 | +#' @param resp_obj A response object. |
| 140 | +#' |
| 141 | +#' @return A integer value returned by the response. |
| 142 | +.response_status <- function(resp_obj) { |
| 143 | + # check package |
| 144 | + pkg_class <- .request_check_package() |
| 145 | + |
| 146 | + # call function |
| 147 | + UseMethod(".response_status", pkg_class) |
| 148 | +} |
| 149 | + |
| 150 | +#' @title Get TRUE/FALSE response status from object |
| 151 | +#' @name .response_is_error |
| 152 | +#' @keywords internal |
| 153 | +#' @noRd |
| 154 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 155 | +#' |
| 156 | +#' @param resp_obj A response object. |
| 157 | +#' |
| 158 | +#' @return A logical value returned by the response. |
| 159 | +.response_is_error <- function(resp_obj) { |
| 160 | + # check package |
| 161 | + pkg_class <- .request_check_package() |
| 162 | + |
| 163 | + # call function |
| 164 | + UseMethod(".response_is_error", pkg_class) |
| 165 | +} |
| 166 | + |
| 167 | +#' @title A response checker status from object |
| 168 | +#' @name .response_status |
| 169 | +#' @keywords internal |
| 170 | +#' @noRd |
| 171 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 172 | +#' |
| 173 | +#' @param resp_obj A response object. |
| 174 | +#' |
| 175 | +#' @return An invisible logical or an error. |
| 176 | +.response_check_status <- function(resp_obj) { |
| 177 | + # check package |
| 178 | + pkg_class <- .request_check_package() |
| 179 | + |
| 180 | + # call function |
| 181 | + UseMethod(".response_check_status", pkg_class) |
| 182 | +} |
| 183 | + |
| 184 | +#' @title Get response type from object |
| 185 | +#' @name .response_content_type |
| 186 | +#' @keywords internal |
| 187 | +#' @noRd |
| 188 | +#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br} |
| 189 | +#' |
| 190 | +#' @param resp_obj A response object. |
| 191 | +#' |
| 192 | +#' @return An character with response type. |
| 193 | +.response_content_type <- function(resp_obj) { |
| 194 | + # check package |
| 195 | + pkg_class <- .request_check_package() |
| 196 | + |
| 197 | + # call function |
| 198 | + UseMethod(".response_content_type", pkg_class) |
| 199 | +} |
0 commit comments