-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add funtion to validate api key
- Loading branch information
Showing
3 changed files
with
28 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
#' Validate 'API' key | ||
#' | ||
#' @return A status code and a message indicating whether the 'API' key is valid. | ||
#' @return TRUE if the API key is valid, FALSE otherwise. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY") | ||
#' nixtlar::nixtla_validate_api_key | ||
#' nixtlar::nixtla_validate_api_key() | ||
#' } | ||
#' | ||
nixtla_validate_api_key <- function(){ | ||
|
||
api_key <- .get_api_key() | ||
url <- "https://api.nixtla.io/validate_token" | ||
|
||
url <- "https://api.nixtla.io/validate_forecast" | ||
resp <- httr2::request(url) |> | ||
req <- httr2::request(url) |> | ||
httr2::req_headers( | ||
Authorization = paste("Bearer", api_key) | ||
) |> | ||
httr2::req_user_agent("nixtla-r") |> | ||
httr2::req_perform() | ||
"accept" = "application/json", | ||
"content-type" = "application/json", | ||
"authorization" = paste("Bearer", api_key) | ||
) |> | ||
httr2::req_user_agent("nixtlar") |> | ||
httr2::req_body_json(data = NULL) | ||
|
||
status_code <- httr2::resp_status(resp) | ||
if(status_code >= 200 & status_code < 300){ | ||
message("API key validation successful. Happy forecasting! :) \nIf you have questions or need support, please email [email protected]") | ||
|
||
resp <- tryCatch({ | ||
req |> | ||
httr2::req_perform() | ||
}, error = function(err){ | ||
NULL | ||
}) | ||
|
||
if(is.null(resp)){ | ||
result <- FALSE | ||
}else{ | ||
message("API key validation failed. Please go https://dashboard.nixtla.io/ to get a valid API key.") | ||
status_code <- httr2::resp_status(resp) | ||
result <- ifelse(status_code == 200, TRUE, FALSE) | ||
} | ||
|
||
return(result) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.