Skip to content

Commit

Permalink
Merge pull request #35 from Nixtla/feat/azure-endpoints
Browse files Browse the repository at this point in the history
feat: add Azure endpoints
  • Loading branch information
MMenchero authored Oct 18, 2024
2 parents a9ac12b + f67edee commit 5888fb3
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 65 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(.generate_output_dates)
export(.get_api_key)
export(.get_client_steup)
export(.get_model_params)
export(.level_from_quantiles)
export(.make_request)
Expand All @@ -14,6 +14,7 @@ export(nixtla_client_detect_anomalies)
export(nixtla_client_forecast)
export(nixtla_client_historic)
export(nixtla_client_plot)
export(nixtla_client_setup)
export(nixtla_set_api_key)
export(nixtla_validate_api_key)
importFrom(dplyr,all_of)
Expand Down
27 changes: 0 additions & 27 deletions R/get_api_key.R

This file was deleted.

39 changes: 39 additions & 0 deletions R/get_client_setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' Get NIXTLA_API_KEY from options or from .Renviron
#' This is a private function of 'nixtlar'
#'
#' @return If available, the NIXTLA_API_KEY. Otherwise it returns an error and a message asking the user to set the 'API' key.
#' @export
#' @keywords internal
#' @examples
#' \dontrun{
#' .get_api_key()
#' }
#'
.get_client_steup <- function(){

# Get setup from .Renviron
base_url <- Sys.getenv("NIXTLA_BASE_URL", unset = NA)
api_key <- Sys.getenv("NIXTLA_API_KEY", unset = NA)

# If not available, get it from options
if(is.na(base_url)){
base_url <- getOption("NIXTLA_BASE_URL", default = NA)
}
if(is.na(base_url)){
base_url <- "https://api.nixtla.io/"
}

if(is.na(api_key)) {
api_key <- getOption("NIXTLA_API_KEY", default = NA)
}
if(is.na(api_key)){
stop("Please set your NIXTLA_API_KEY. Use nixtla_client_setup() or set it as an environment variable in .Renviron")
}

setup <- list(
base_url = base_url,
api_key = api_key
)

return(setup)
}
7 changes: 4 additions & 3 deletions R/get_model_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#'
#' @examples
#' \dontrun{
#' .get_model_params()
#' .get_model_params(model, freq)
#' }
#'
.get_model_params <- function(model, freq){
Expand All @@ -20,11 +20,12 @@
freq = freq
)

req <- httr2::request("https://api.nixtla.io/model_params") |>
setup <- .get_client_steup()
req <- httr2::request(paste0(setup$base_url, "model_params")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload_params) |>
Expand Down
7 changes: 4 additions & 3 deletions R/make_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#' This is a private function of 'nixtlar'
#'
#' @param url String specifying the API endpoint to which the request is sent.
#' @param api_key The user's API key.
#' @param payload_list List containing the information to be sent to the 'TimeGPT' API.
#'
#' @return List representing the JSON response from the API endpoint.
Expand All @@ -10,17 +11,17 @@
#'
#' @examples
#' \dontrun{
#' response <- .make_request(url, payload_list_element)
#' response <- .make_request(url, api_key, payload_list_element)
#' }
#'
.make_request <- function(url, payload_list) {
.make_request <- function(base_url, api_key, payload_list) {

req_resp <- function(payload) {
req <- httr2::request(url) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload) |>
Expand Down
6 changes: 3 additions & 3 deletions R/nixtla_client_cross_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ nixtla_client_cross_validation <- function(df, h=8, freq=NULL, id_col="unique_id
}

# Make request ----
url <- "https://api.nixtla.io/v2/cross_validation"
req <- httr2::request(url) |>
setup <- .get_client_steup()
req <- httr2::request(paste0(setup$base_url, "v2/cross_validation")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload) |>
Expand Down
6 changes: 3 additions & 3 deletions R/nixtla_client_detect_anomalies.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ nixtla_client_detect_anomalies <- function(df, freq=NULL, id_col="unique_id", ti
}

# Make request ----
url <- "https://api.nixtla.io/v2/anomaly_detection"
req <- httr2::request(url) |>
setup <- .get_client_steup()
req <- httr2::request(paste0(setup$base_url, "v2/anomaly_detection")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload) |>
Expand Down
6 changes: 3 additions & 3 deletions R/nixtla_client_forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ nixtla_client_forecast <- function(df, h=8, freq=NULL, id_col="unique_id", time_
}

# Make request ----
url <- "https://api.nixtla.io/v2/forecast"
req <- httr2::request(url) |>
setup <- .get_client_steup()
req <- httr2::request(paste0(setup$base_url, "v2/forecast")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload) |>
Expand Down
6 changes: 3 additions & 3 deletions R/nixtla_client_historic.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ nixtla_client_historic <- function(df, freq=NULL, id_col=NULL, time_col="ds", ta
}

# Make request ----
url <- "https://api.nixtla.io/v2/historic_forecast"
req <- httr2::request(url) |>
setup <- .get_client_steup()
req <- httr2::request(paste0(setup$base_url, "v2/historic_forecast")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", .get_api_key())
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = payload) |>
Expand Down
31 changes: 31 additions & 0 deletions R/nixtla_client_setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' Set base 'ULR' and 'API' key in global environment
#'
#' @param base_url Custom base 'URL'. If NULL, defaults to "https://api.nixtla.io/".
#' @param api_key The user's 'API' key. Get yours here: https://dashboard.nixtla.io/
#'
#' @return A message indicating the configuration status.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_client_setup(
#' base_url = "Base URL",
#' api_key = "Your API key"
#' )
#' }
#'
nixtla_client_setup <- function(base_url = NULL, api_key = NULL) {
if (is.null(base_url)) {
base_url <- "https://api.nixtla.io/"
} else {
message("Base URL has been set to: ", base_url)
}
options("NIXTLA_BASE_URL" = base_url)

if (is.null(api_key)) {
stop("API key must be provided. Get yours at https://dashboard.nixtla.io/")
} else {
options("NIXTLA_API_KEY" = api_key)
message("API key has been set for the current session.")
}
}
7 changes: 4 additions & 3 deletions R/nixtla_set_api_key.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#' Set 'API' key in global environment
#'
#' This function will be deprecated in future versions. Please use `nixtla_client_setup` instead.
#'
#' @param api_key The user's 'API' key. Get yours here: https://dashboard.nixtla.io/
#'
#' @return A message indicating the 'API' key has been set in the global environment.
#' @export
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' nixtlar::nixtla_set_api_key("Your API key")
#' }
#'
nixtla_set_api_key <- function(api_key) {
options("NIXTLA_API_KEY"=api_key)
message("API key has been set for the current session.")
nixtla_client_setup(base_url = NULL, api_key = api_key)
}
9 changes: 4 additions & 5 deletions R/nixtla_validate_api_key.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
#'
#' @examples
#' \dontrun{
#' nixtlar::nixtla_set_api_key("YOUR_API_KEY")
#' nixtlar::nixtla_client_setup(api_key = "Your API key")
#' nixtlar::nixtla_validate_api_key()
#' }
#'
nixtla_validate_api_key <- function(){

api_key <- .get_api_key()
url <- "https://api.nixtla.io/validate_token"
setup <- .get_client_steup()

req <- httr2::request(url) |>
req <- httr2::request(paste0(setup$base_url, "validate_token")) |>
httr2::req_headers(
"accept" = "application/json",
"content-type" = "application/json",
"authorization" = paste("Bearer", api_key)
"authorization" = paste("Bearer", setup$api_key)
) |>
httr2::req_user_agent("nixtlar") |>
httr2::req_body_json(data = NULL)
Expand Down
8 changes: 4 additions & 4 deletions man/dot-get_api_key.Rd → man/dot-get_client_steup.Rd

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

2 changes: 1 addition & 1 deletion man/dot-get_model_params.Rd

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

8 changes: 5 additions & 3 deletions man/dot-make_request.Rd

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

28 changes: 28 additions & 0 deletions man/nixtla_client_setup.Rd

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

4 changes: 2 additions & 2 deletions man/nixtla_set_api_key.Rd

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

2 changes: 1 addition & 1 deletion 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 5888fb3

Please sign in to comment.