diff --git a/NAMESPACE b/NAMESPACE index a7b10dc..069e42c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,6 @@ # Generated by roxygen2: do not edit by hand export(check_container_class) -export(check_envvar) export(check_nzchar) export(check_scalar_type) export(check_that) diff --git a/R/get_container.R b/R/get_container.R index 2370ede..1374abb 100644 --- a/R/get_container.R +++ b/R/get_container.R @@ -1,41 +1,23 @@ #' Get Azure storage container #' -#' The environment variable "AZ_STORAGE_EP" should be set. This provides the URL -#' for the default Azure storage endpoint. -#' Use [list_container_names] to get a list of available container names. +#' It may be helpful to set the environment variable "AZ_STORAGE_EP". This +#' can contain your usual Azure storage endpoint URL should you not wish to +#' pass it in explicitly to the function. +#' You may find it helpful to use [list_container_names] to get a list of +#' available container names. #' -#' @param container_name Name of the container as a string. `NULL` by default, -#' which means the function will look instead for a container name stored in -#' the environment variable "AZ_CONTAINER" -#' @param token An Azure authentication token. If left as `NULL`, a token -#' returned by [get_auth_token] will be used -#' @param endpoint_url An Azure endpoint URL. If left as `NULL`, the default, -#' the value of the environment variable "AZ_STORAGE_EP" will be used -#' @param ... arguments to be passed through to [get_auth_token], if a token is -#' not already supplied +#' @param container_name Name of the container as a string. +#' @param endpoint_url An Azure endpoint URL. +#' @param token An Azure authentication token, or a function that returns one. +#' Uses [get_auth_token] by default. #' @returns An Azure blob container (list object of class "blob_container") #' @export get_container <- function( - container_name = NULL, - token = NULL, - endpoint_url = NULL, - ... + container_name, + endpoint_url = Sys.getenv("AZ_STORAGE_EP"), + token = get_auth_token() ) { - msg1 <- paste0( - "{.var container_name} is empty. ", - "Did you forget to set an environment variable?" - ) - msg2 <- paste0( - "{.var endpoint_url} is empty. ", - "Did you forget to set an environment variable?" - ) - container_name <- (container_name %||% check_envvar("AZ_CONTAINER")) |> - check_nzchar(msg1) - endpoint_url <- (endpoint_url %||% check_envvar("AZ_STORAGE_EP")) |> - check_nzchar(msg2) - token <- token %||% get_auth_token(...) - - get_azure_endpoint(token, endpoint_url) |> + AzureStor::blob_endpoint(endpoint_url, token = token) |> AzureStor::blob_container(container_name) } @@ -45,39 +27,16 @@ get_container <- function( #' @inheritParams get_container #' @returns A character vector of all container names found #' @export -list_container_names <- function(token = NULL, endpoint_url = NULL, ...) { - token <- token %||% get_auth_token(...) - endpoint <- get_azure_endpoint(token, endpoint_url) - container_list <- AzureStor::list_blob_containers(endpoint) - stopifnot("no containers found" = length(container_list) >= 1L) - names(container_list) -} - - -#' Return an Azure "blob_endpoint" -#' -#' This function will return the endpoint specified in the environment variable -#' "AZ_STORAGE_EP" by default -#' -#' @inheritParams get_container -#' @returns An Azure blob endpoint (object of class "blob_endpoint") -#' @keywords internal -get_azure_endpoint <- function(token = NULL, endpoint_url = NULL, ...) { - token <- token %||% get_auth_token(...) - endpoint_url <- endpoint_url %||% check_envvar("AZ_STORAGE_EP") - AzureStor::blob_endpoint(endpoint_url, token = token) -} - - -#' Check that an environment variable exists -#' -#' The function prints a helpful error if the variable is not found, else -#' it returns the value of `Sys.getenv(x)` -#' -#' @param x the *name* of the environment variable to be found and checked -#' @returns the value of the environment variable named in `x` -#' @export -check_envvar <- function(x) { - cst_msg <- cst_error_msg("The environment variable {.envvar {x}} is not set") - check_scalar_type(Sys.getenv(x, NA_character_), "string", cst_msg) +list_container_names <- function( + endpoint_url = Sys.getenv("AZ_STORAGE_EP"), + token = get_auth_token() +) { + container_list <- AzureStor::blob_endpoint(endpoint_url, token = token) |> + AzureStor::list_blob_containers() + if (length(container_list) == 0) { + cli::cli_alert_info("No containers found") + character(0) + } else { + names(container_list) + } } diff --git a/R/read_azure_table.R b/R/read_azure_table.R index d95c3b4..c9a9269 100644 --- a/R/read_azure_table.R +++ b/R/read_azure_table.R @@ -1,30 +1,21 @@ #' Read in data from an Azure table #' -#' @param table_name name of the table to be read. If left as `NULL`, -#' the default, the function will look instead for a value stored in the -#' environment variable "AZ_TABLE_NAME" -#' @param table_endpoint URL of the Azure table endpoint. If left as `NULL`, -#' the default, the function will look instead for a value stored in the -#' environment variable "AZ_TABLE_EP" -#' @param ... parameters to be passed through to [get_auth_token] +#' @param table_name Name of the table to be read. +#' @param table_endpoint An Azure table endpoint URL. #' @inheritParams get_container #' @returns A tibble #' @export read_azure_table <- function( - table_name = NULL, - table_endpoint = NULL, - token = NULL, - ... + table_name, + table_endpoint = Sys.getenv("AZ_TABLE_EP"), + token = get_auth_token() ) { - table_name <- table_name %||% check_envvar("AZ_TABLE_NAME") - table_ep <- table_endpoint %||% check_envvar("AZ_TABLE_EP") - token <- token %||% get_auth_token(...) access_token <- token |> purrr::pluck("credentials", "access_token") headers <- list("2025-11-05", "application/json;odata=nometadata") |> purrr::set_names(c("x-ms-version", "Accept")) - resp <- httr2::request(table_ep) |> + resp <- httr2::request(table_endpoint) |> httr2::req_url_path_append(table_name) |> httr2::req_auth_bearer_token(access_token) |> httr2::req_headers(!!!headers) |> diff --git a/man/check_envvar.Rd b/man/check_envvar.Rd deleted file mode 100644 index a5e98d9..0000000 --- a/man/check_envvar.Rd +++ /dev/null @@ -1,18 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get_container.R -\name{check_envvar} -\alias{check_envvar} -\title{Check that an environment variable exists} -\usage{ -check_envvar(x) -} -\arguments{ -\item{x}{the \emph{name} of the environment variable to be found and checked} -} -\value{ -the value of the environment variable named in \code{x} -} -\description{ -The function prints a helpful error if the variable is not found, else -it returns the value of \code{Sys.getenv(x)} -} diff --git a/man/get_azure_endpoint.Rd b/man/get_azure_endpoint.Rd deleted file mode 100644 index d1d4be7..0000000 --- a/man/get_azure_endpoint.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/get_container.R -\name{get_azure_endpoint} -\alias{get_azure_endpoint} -\title{Return an Azure "blob_endpoint"} -\usage{ -get_azure_endpoint(token = NULL, endpoint_url = NULL, ...) -} -\arguments{ -\item{token}{An Azure authentication token. If left as \code{NULL}, a token -returned by \link{get_auth_token} will be used} - -\item{endpoint_url}{An Azure endpoint URL. If left as \code{NULL}, the default, -the value of the environment variable "AZ_STORAGE_EP" will be used} - -\item{...}{arguments to be passed through to \link{get_auth_token}, if a token is -not already supplied} -} -\value{ -An Azure blob endpoint (object of class "blob_endpoint") -} -\description{ -This function will return the endpoint specified in the environment variable -"AZ_STORAGE_EP" by default -} -\keyword{internal} diff --git a/man/get_container.Rd b/man/get_container.Rd index 6e88a05..3049a6e 100644 --- a/man/get_container.Rd +++ b/man/get_container.Rd @@ -4,27 +4,27 @@ \alias{get_container} \title{Get Azure storage container} \usage{ -get_container(container_name = NULL, token = NULL, endpoint_url = NULL, ...) +get_container( + container_name, + endpoint_url = Sys.getenv("AZ_STORAGE_EP"), + token = get_auth_token() +) } \arguments{ -\item{container_name}{Name of the container as a string. \code{NULL} by default, -which means the function will look instead for a container name stored in -the environment variable "AZ_CONTAINER"} +\item{container_name}{Name of the container as a string.} -\item{token}{An Azure authentication token. If left as \code{NULL}, a token -returned by \link{get_auth_token} will be used} +\item{endpoint_url}{An Azure endpoint URL.} -\item{endpoint_url}{An Azure endpoint URL. If left as \code{NULL}, the default, -the value of the environment variable "AZ_STORAGE_EP" will be used} - -\item{...}{arguments to be passed through to \link{get_auth_token}, if a token is -not already supplied} +\item{token}{An Azure authentication token, or a function that returns one. +Uses \link{get_auth_token} by default.} } \value{ An Azure blob container (list object of class "blob_container") } \description{ -The environment variable "AZ_STORAGE_EP" should be set. This provides the URL -for the default Azure storage endpoint. -Use \link{list_container_names} to get a list of available container names. +It may be helpful to set the environment variable "AZ_STORAGE_EP". This +can contain your usual Azure storage endpoint URL should you not wish to +pass it in explicitly to the function. +You may find it helpful to use \link{list_container_names} to get a list of +available container names. } diff --git a/man/list_container_names.Rd b/man/list_container_names.Rd index 3587268..2de7469 100644 --- a/man/list_container_names.Rd +++ b/man/list_container_names.Rd @@ -4,17 +4,16 @@ \alias{list_container_names} \title{Return a list of container names that are found at the endpoint} \usage{ -list_container_names(token = NULL, endpoint_url = NULL, ...) +list_container_names( + endpoint_url = Sys.getenv("AZ_STORAGE_EP"), + token = get_auth_token() +) } \arguments{ -\item{token}{An Azure authentication token. If left as \code{NULL}, a token -returned by \link{get_auth_token} will be used} +\item{endpoint_url}{An Azure endpoint URL.} -\item{endpoint_url}{An Azure endpoint URL. If left as \code{NULL}, the default, -the value of the environment variable "AZ_STORAGE_EP" will be used} - -\item{...}{arguments to be passed through to \link{get_auth_token}, if a token is -not already supplied} +\item{token}{An Azure authentication token, or a function that returns one. +Uses \link{get_auth_token} by default.} } \value{ A character vector of all container names found diff --git a/man/read_azure_table.Rd b/man/read_azure_table.Rd index 0dc2010..c94ed27 100644 --- a/man/read_azure_table.Rd +++ b/man/read_azure_table.Rd @@ -4,21 +4,19 @@ \alias{read_azure_table} \title{Read in data from an Azure table} \usage{ -read_azure_table(table_name = NULL, table_endpoint = NULL, token = NULL, ...) +read_azure_table( + table_name, + table_endpoint = Sys.getenv("AZ_TABLE_EP"), + token = get_auth_token() +) } \arguments{ -\item{table_name}{name of the table to be read. If left as \code{NULL}, -the default, the function will look instead for a value stored in the -environment variable "AZ_TABLE_NAME"} +\item{table_name}{Name of the table to be read.} -\item{table_endpoint}{URL of the Azure table endpoint. If left as \code{NULL}, -the default, the function will look instead for a value stored in the -environment variable "AZ_TABLE_EP"} +\item{table_endpoint}{An Azure table endpoint URL.} -\item{token}{An Azure authentication token. If left as \code{NULL}, a token -returned by \link{get_auth_token} will be used} - -\item{...}{parameters to be passed through to \link{get_auth_token}} +\item{token}{An Azure authentication token, or a function that returns one. +Uses \link{get_auth_token} by default.} } \value{ A tibble