From 2acd1e8a4b94c2f66976f24e9f4132108531d8d0 Mon Sep 17 00:00:00 2001 From: Phill Date: Tue, 16 Feb 2016 21:40:48 +0000 Subject: [PATCH] Switched auth details to env variables. Updated docs. Added build badges. --- R/b2AuthorizeAccount.R | 19 ++++++++++--------- R/b2CreateBucket.R | 13 +++++++------ R/b2DeleteBucket.R | 13 +++++++------ R/b2DeleteFileVersion.R | 10 +++++----- R/b2DownloadFileById.R | 10 +++++----- R/b2DownloadFileByName.R | 10 +++++----- R/b2GetFileInfo.R | 10 +++++----- R/b2GetUploadUrl.R | 10 +++++----- R/b2HideFile.R | 10 +++++----- R/b2ListBuckets.R | 11 ++++++----- R/b2ListFileNames.R | 10 +++++----- R/b2ListFileVersions.R | 10 +++++----- R/b2UpdateBucket.R | 13 +++++++------ R/b2UploadFile.R | 3 --- README.md | 4 +++- man/b2AuthorizeAccount.Rd | 12 ++++-------- vignettes/backblazer-howToUse.Rmd | 4 +--- 17 files changed, 85 insertions(+), 87 deletions(-) diff --git a/R/b2AuthorizeAccount.R b/R/b2AuthorizeAccount.R index dc92507..0da2d62 100644 --- a/R/b2AuthorizeAccount.R +++ b/R/b2AuthorizeAccount.R @@ -31,17 +31,14 @@ #' B2 account. This may be obtained by clicking \emph{Show Account ID and #' Application Key} hypertext from the B2 My Account area, after logging in #' with a web browser. -#' @return If successful, an authorisation token will be returned and stored in -#' an Rds file called \emph{accountAuthorization.Rds} in the current working -#' directory. The data in this Rds file will be used in all other functions in -#' this package. Specific B2 documentation regarding this API call can be +#' @return If successful, an authorisation token, API URL, download URL and your +#' Account ID will be returned. These are then stored in envrionment +#' variables. These environment variables will be used in all other functions +#' in this package. Specific B2 documentation regarding this API call can be #' found here: #' #' \url{https://www.backblaze.com/b2/docs/b2_authorize_account.html} #' -#' @section Note: Consider programmtically deleting -#' \emph{accountAuthorization.Rds} on exit. -#' #' @examples #' \dontrun{ #' b2AuthorizeAccount(url = "https://api.backblaze.com/b2api/v1/b2_authorize_account", @@ -70,9 +67,13 @@ b2AuthorizeAccount <- function(url, accountId, authorizationKey) { ) } else { - # Output as dataframe. Save as Rds file. + # Output as dataframe. accountAuthorization <- as.data.frame(jsonlite::fromJSON(httr::content(b2Return, type = "text"))) - saveRDS(accountAuthorization, "accountAuthorization.rds") + # Set environment variables to save authorisation details + Sys.setenv(apiUrl = accountAuthorization$apiUrl) + Sys.setenv(accountId = accountAuthorization$accountId) + Sys.setenv(authorizationToken = accountAuthorization$authorizationToken) + Sys.setenv(downloadUrl = accountAuthorization$downloadUrl) } } diff --git a/R/b2CreateBucket.R b/R/b2CreateBucket.R index 987b50c..5c723bf 100644 --- a/R/b2CreateBucket.R +++ b/R/b2CreateBucket.R @@ -34,12 +34,13 @@ #' @export b2CreateBucket <- function(bucketName, bucketType) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + accountId <- Sys.getenv('accountId') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe - accountId <- as.character(accountAuthorization$accountId) + accountId <- as.character(accountId) accountId <- as.data.frame(accountId, stringsAsFactors = FALSE) bucketName <- as.data.frame(bucketName, stringsAsFactors = FALSE) bucketType <- as.data.frame(bucketType, stringsAsFactors = FALSE) @@ -51,9 +52,9 @@ b2CreateBucket <- function(bucketName, bucketType) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_create_bucket", sep = "" + apiUrl,"/b2api/v1/b2_create_bucket", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2DeleteBucket.R b/R/b2DeleteBucket.R index 6c8aef8..52dae52 100644 --- a/R/b2DeleteBucket.R +++ b/R/b2DeleteBucket.R @@ -26,12 +26,13 @@ #' @export b2DeleteBucket <- function(bucketId) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + accountId <- Sys.getenv('accountId') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe - accountId <- as.character(accountAuthorization$accountId) + accountId <- as.character(accountId) bucketId <- as.data.frame(bucketId, stringsAsFactors = FALSE) # Bind function option data frames together @@ -41,9 +42,9 @@ b2DeleteBucket <- function(bucketId) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_delete_bucket", sep = "" + apiUrl,"/b2api/v1/b2_delete_bucket", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2DeleteFileVersion.R b/R/b2DeleteFileVersion.R index 39a337f..fb1e185 100644 --- a/R/b2DeleteFileVersion.R +++ b/R/b2DeleteFileVersion.R @@ -32,9 +32,9 @@ #' @export b2DeleteFileVersion <- function(fileName, fileId) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe fileName <- as.data.frame(fileName, stringsAsFactors = FALSE) @@ -47,9 +47,9 @@ b2DeleteFileVersion <- function(fileName, fileId) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_delete_file_version", sep = "" + apiUrl,"/b2api/v1/b2_delete_file_version", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2DownloadFileById.R b/R/b2DownloadFileById.R index cebddc9..e2f0768 100644 --- a/R/b2DownloadFileById.R +++ b/R/b2DownloadFileById.R @@ -34,9 +34,9 @@ #' @export b2DownloadFileById <- function(fileId, overwrite = FALSE) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + downloadUrl <- Sys.getenv('downloadUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe fileId <- as.data.frame(fileId, stringsAsFactors = FALSE) @@ -45,10 +45,10 @@ b2DownloadFileById <- function(fileId, overwrite = FALSE) { b2Return <- httr::POST( paste( - accountAuthorization$downloadUrl,"/b2api/v1/b2_download_file_by_id", sep = + downloadUrl,"/b2api/v1/b2_download_file_by_id", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(fileId), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ), httr::write_disk("tmp", overwrite = overwrite) ) diff --git a/R/b2DownloadFileByName.R b/R/b2DownloadFileByName.R index 8424478..abb88c7 100644 --- a/R/b2DownloadFileByName.R +++ b/R/b2DownloadFileByName.R @@ -37,9 +37,9 @@ b2DownloadFileByName <- function(bucketName, fileName, overwrite = FALSE) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + downloadUrl <- Sys.getenv('downloadUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe fileName <- as.data.frame(fileName, stringsAsFactors = FALSE) @@ -52,10 +52,10 @@ b2DownloadFileByName <- b2Return <- httr::GET( url = paste( - accountAuthorization$downloadUrl,"/file/", bucketName, "/", fileName, sep = + downloadUrl,"/file/", bucketName, "/", fileName, sep = "" ), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ), httr::write_disk("tmp", overwrite = overwrite) ) diff --git a/R/b2GetFileInfo.R b/R/b2GetFileInfo.R index dab95c8..99d526e 100644 --- a/R/b2GetFileInfo.R +++ b/R/b2GetFileInfo.R @@ -27,9 +27,9 @@ #' @export b2GetFileInfo <- function(fileId) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe fileId <- as.data.frame(fileId, stringsAsFactors = FALSE) @@ -38,9 +38,9 @@ b2GetFileInfo <- function(fileId) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_get_file_info", sep = "" + apiUrl,"/b2api/v1/b2_get_file_info", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(fileId), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2GetUploadUrl.R b/R/b2GetUploadUrl.R index f2a1df4..57af0e7 100644 --- a/R/b2GetUploadUrl.R +++ b/R/b2GetUploadUrl.R @@ -29,9 +29,9 @@ #' @export b2GetUploadUrl <- function(bucketId) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe bucketId <- as.data.frame(bucketId, stringsAsFactors = FALSE) @@ -40,9 +40,9 @@ b2GetUploadUrl <- function(bucketId) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_get_upload_url", sep = "" + apiUrl,"/b2api/v1/b2_get_upload_url", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(bucketId), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2HideFile.R b/R/b2HideFile.R index 36f992e..77c4861 100644 --- a/R/b2HideFile.R +++ b/R/b2HideFile.R @@ -29,9 +29,9 @@ #' @export b2HideFile <- function(bucketId, fileName) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe fileName <- as.data.frame(fileName, stringsAsFactors = FALSE) @@ -44,9 +44,9 @@ b2HideFile <- function(bucketId, fileName) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_hide_file", sep = "" + apiUrl,"/b2api/v1/b2_hide_file", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2ListBuckets.R b/R/b2ListBuckets.R index aee7ce3..6acb854 100644 --- a/R/b2ListBuckets.R +++ b/R/b2ListBuckets.R @@ -25,18 +25,19 @@ #' @export b2ListBuckets <- function() { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + accountId <- Sys.getenv('accountId') + authorizationToken <- Sys.getenv('authorizationToken') # API call b2Return <- httr::GET( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_list_buckets?accountId=",accountAuthorization$accountId,sep = + apiUrl,"/b2api/v1/b2_list_buckets?accountId=",accountId,sep = "" ), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2ListFileNames.R b/R/b2ListFileNames.R index e05bc05..a818210 100644 --- a/R/b2ListFileNames.R +++ b/R/b2ListFileNames.R @@ -46,9 +46,9 @@ b2ListFileNames <- function(bucketId, startFileName = "", maxFileCount = 100) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe bucketId <- as.data.frame(bucketId, stringsAsFactors = FALSE) @@ -66,9 +66,9 @@ b2ListFileNames <- b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_list_file_names", sep = "" + apiUrl,"/b2api/v1/b2_list_file_names", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2ListFileVersions.R b/R/b2ListFileVersions.R index ef61c2b..07a70f5 100644 --- a/R/b2ListFileVersions.R +++ b/R/b2ListFileVersions.R @@ -54,9 +54,9 @@ b2ListFileVersions <- function(bucketId, startFileName = "", startFileId = "", maxFileCount = 100) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe bucketId <- as.data.frame(bucketId, stringsAsFactors = FALSE) @@ -76,9 +76,9 @@ b2ListFileVersions <- b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_list_file_versions", sep = "" + apiUrl,"/b2api/v1/b2_list_file_versions", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2UpdateBucket.R b/R/b2UpdateBucket.R index 172d7c2..6326de5 100644 --- a/R/b2UpdateBucket.R +++ b/R/b2UpdateBucket.R @@ -34,12 +34,13 @@ #' @export b2UpdateBucket <- function(bucketId, bucketType) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") + # Read Environment variables for authorisation details + apiUrl <- Sys.getenv('apiUrl') + accountId <- Sys.getenv('accountId') + authorizationToken <- Sys.getenv('authorizationToken') # Function options from input, make a dataframe - accountId <- as.character(accountAuthorization$accountId) + accountId <- as.character(accountId) accountId <- as.data.frame(accountId, stringsAsFactors = FALSE) bucketId <- as.data.frame(bucketId, stringsAsFactors = FALSE) bucketType <- as.data.frame(bucketType, stringsAsFactors = FALSE) @@ -51,9 +52,9 @@ b2UpdateBucket <- function(bucketId, bucketType) { b2Return <- httr::POST( paste( - accountAuthorization$apiUrl,"/b2api/v1/b2_update_bucket", sep = "" + apiUrl,"/b2api/v1/b2_update_bucket", sep = "" ), body = jsonlite::toJSON(jsonlite::unbox(vars), pretty = TRUE), httr::add_headers( - 'Authorization' = as.character(accountAuthorization$authorizationToken) + 'Authorization' = as.character(authorizationToken) ) ) diff --git a/R/b2UploadFile.R b/R/b2UploadFile.R index c5bd375..849ff94 100644 --- a/R/b2UploadFile.R +++ b/R/b2UploadFile.R @@ -41,9 +41,6 @@ #' @export b2UploadFile <- function(authToken, uploadUrl, fileName) { - # Read Account Authorisation file - accountAuthorization <- NULL - accountAuthorization <- readRDS("accountAuthorization.rds") # Function options from input, make a dataframe # File Name diff --git a/README.md b/README.md index 1a3762d..e5aa8fc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ Backblazer ======= [![CRAN version](http://www.r-pkg.org/badges/version/backblazer)](http://cran.rstudio.com/web/packages/backblazer/index.html) +[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/grand-total/backblazer)](http://cran.rstudio.com/web/packages/backblazer/index.html) +[![Build Status](https://travis-ci.org/phillc73/backblazer.svg?branch=master)](https://travis-ci.org/phillc73/backblazer) An R package with bindings to the [Backblaze B2 API](https://www.backblaze.com/b2/docs/). @@ -80,7 +82,7 @@ Please refer directly to the [Backblaze B2 API documentation](https://www.backbl This should be largely complete and covers all Backblaze B2 API calls. -Current Version: 0.1 +Current Version: 0.2 ### Issues diff --git a/man/b2AuthorizeAccount.Rd b/man/b2AuthorizeAccount.Rd index 6dca7d2..822fbf7 100644 --- a/man/b2AuthorizeAccount.Rd +++ b/man/b2AuthorizeAccount.Rd @@ -20,10 +20,10 @@ Application Key} hypertext from the B2 My Account area, after logging in with a web browser.} } \value{ -If successful, an authorisation token will be returned and stored in - an Rds file called \emph{accountAuthorization.Rds} in the current working - directory. The data in this Rds file will be used in all other functions in - this package. Specific B2 documentation regarding this API call can be +If successful, an authorisation token, API URL, download URL and your + Account ID will be returned. These are then stored in envrionment + variables. These environment variables will be used in all other functions + in this package. Specific B2 documentation regarding this API call can be found here: \url{https://www.backblaze.com/b2/docs/b2_authorize_account.html} @@ -52,10 +52,6 @@ API account authorization \code{url}, \code{accountId}, Every time \code{b2AuthorizeAccount} is executed, a new login to Backblaze B2 occurs. Don't login more than is necessary. } -\section{Note}{ - Consider programmtically deleting - \emph{accountAuthorization.Rds} on exit. -} \examples{ \dontrun{ b2AuthorizeAccount(url = "https://api.backblaze.com/b2api/v1/b2_authorize_account", diff --git a/vignettes/backblazer-howToUse.Rmd b/vignettes/backblazer-howToUse.Rmd index 9de51b6..618cd05 100644 --- a/vignettes/backblazer-howToUse.Rmd +++ b/vignettes/backblazer-howToUse.Rmd @@ -32,9 +32,7 @@ b2AuthorizeAccount(url = "https://api.backblaze.com/b2api/v1/b2_authorize_accoun Obviously substitute your actual Account ID and Authorisation Key for the placeholders. -You must authorise your account first. Nothing else in this package will work until the account has been authorised. This function will ultimately save an Rds file named `accountAuthorization.Rds` in the current working directory. This file is then silently used with all other functions in the package. - -**Note:** Consider programatically deleting `accountAuthorization.Rds` on exit. A new Rds file will be written each time `b2AuthorizeAccount` is executed. +You must authorise your account first. Nothing else in this package will work until the account has been authorised. This function will ultimately save a number of environment variables containing API endpoint URLs and an authorisation token. These environment variables are then silently used with all other functions in the package. ### Working with Buckets