Skip to content

Commit

Permalink
feat: add funtion to validate api key
Browse files Browse the repository at this point in the history
  • Loading branch information
MMenchero committed Oct 7, 2024
1 parent 8369299 commit 82d8f89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
3 changes: 2 additions & 1 deletion R/generate_output_dates.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
r_freq <- .r_frequency(freq)

if(freq == "QE") {
# End of quarter dates are: "YYY-03-31", "YYYY-06-30", "YYYY-09-30" and "YYYY-12-31".
dt <- seq(from = start_date, by = "quarter", length.out = h+1)
month <- lubridate::month(start_date)
if (month %in% c(3, 12)) {
Expand All @@ -39,7 +40,7 @@
dates_df <- data.frame(lapply(new_dates, as.POSIXct))

ids <- df_info$unique_id
if(class(df_info$unique_id) == "integer"){
if(inherits(df_info$unique_id, "numeric") | inherits(df_info$unique_id, "integer")){
ids <- as.character(df_info$unique_id)
}
names(dates_df) <- ids
Expand Down
36 changes: 24 additions & 12 deletions R/nixtla_validate_api_key.R
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)
}
4 changes: 2 additions & 2 deletions man/nixtla_validate_api_key.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 82d8f89

Please sign in to comment.