Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
91 changes: 25 additions & 66 deletions R/get_container.R
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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)
}
}
21 changes: 6 additions & 15 deletions R/read_azure_table.R
Original file line number Diff line number Diff line change
@@ -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) |>
Expand Down
18 changes: 0 additions & 18 deletions man/check_envvar.Rd

This file was deleted.

26 changes: 0 additions & 26 deletions man/get_azure_endpoint.Rd

This file was deleted.

28 changes: 14 additions & 14 deletions man/get_container.Rd

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

15 changes: 7 additions & 8 deletions man/list_container_names.Rd

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

20 changes: 9 additions & 11 deletions man/read_azure_table.Rd

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

Loading