diff --git a/DESCRIPTION b/DESCRIPTION index 52fd3d4..d28f8cf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -11,7 +11,7 @@ URL: https://github.com/Kohze/fireData BugReports: https://github.com/Kohze/fireData/issues Encoding: UTF-8 LazyData: true -RoxygenNote: 6.0.1 +RoxygenNote: 6.1.0 Imports: httr (>= 1.2.1), jsonlite (>= 1.4), diff --git a/FireData.Rproj b/FireData.Rproj deleted file mode 100644 index 30e02be..0000000 --- a/FireData.Rproj +++ /dev/null @@ -1,21 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: No -SaveWorkspace: No -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: knitr -LaTeX: pdfLaTeX - -AutoAppendNewline: Yes -StripTrailingWhitespace: Yes - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source -PackageRoxygenize: rd,collate,namespace diff --git a/NAMESPACE b/NAMESPACE index de45b16..a75f355 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,10 @@ export(auth) export(createUser) export(dataBackup) +export(delete) export(download) +export(patch) export(path_check) +export(put) export(resetPassword) export(upload) diff --git a/R/index.R b/R/index.R index a7e899b..407de1d 100644 --- a/R/index.R +++ b/R/index.R @@ -1,3 +1,72 @@ +#' @title The firebase data delete function: +#' @author Kevin McGinley +#' @description The function allows to delete data objects, such as variables, lists and data.frames +#' @param x A list, data.frame or data.table {object} +#' @param projectURL The Firebase project URL {string} +#' @param directory The optimal Firebase subdirectory {string} +#' @param token The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string} +#' @return returns http request answer. +#' @export +#' @examples +#' \dontrun{ +#' delete(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +#' } +delete <- function(x, projectURL, directory = "main", token = "none"){ + output = fileConversion(x) + if (token == "none") { + Response = httr::DELETE(paste0(projectURL,"/",directory,".json"), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } else { + Response = httr::DELETE(paste0(projectURL,"/",directory,".json?auth=",token), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } + return(paste0(directory,"/",httr::content(Response)$name)) +} + +#' @title The firebase data patch function: +#' @author Kevin McGinley +#' @description The function allows to update data objects, such as variables, lists and data.frames +#' @param x A list, data.frame or data.table {object} +#' @param projectURL The Firebase project URL {string} +#' @param directory The optimal Firebase subdirectory {string} +#' @param token The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string} +#' @return returns http request answer. +#' @export +#' @examples +#' \dontrun{ +#' patch(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +#' } +patch <- function(x, projectURL, directory = "main", token = "none"){ + output = fileConversion(x) + if (token == "none") { + Response = httr::PATCH(paste0(projectURL,"/",directory,".json"), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } else { + Response = httr::PATCH(paste0(projectURL,"/",directory,".json?auth=",token), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } + return(paste0(directory,"/",httr::content(Response)$name)) +} + +#' @title The firebase data put function: +#' @author Kevin McGinley +#' @description The function allows to update data objects, such as variables, lists and data.frames +#' @param x A list, data.frame or data.table {object} +#' @param projectURL The Firebase project URL {string} +#' @param directory The optimal Firebase subdirectory {string} +#' @param token The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string} +#' @return returns http request answer. +#' @export +#' @examples +#' \dontrun{ +#' patch(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +#' } +put <- function(x, projectURL, directory = "main", token = "none"){ + output = fileConversion(x) + if (token == "none") { + Response = httr::PUT(paste0(projectURL,"/",directory,".json"), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } else { + Response = httr::PUT(paste0(projectURL,"/",directory,".json?auth=",token), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } + return(paste0(directory,"/",httr::content(Response)$name)) +} + #' @title The firebase data upload function: #' @author Robin Kohze #' @description The function allows to upload data objects, such as variables,lists and data.frames @@ -12,12 +81,12 @@ #' upload(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") #' } upload <- function(x, projectURL, directory = "main", token = "none"){ - output = fileConversion(x) - if (token == "none") { - Response = httr::POST(paste0(projectURL,"/",directory,".json"), body = jsonlite::toJSON(output, auto_unbox = TRUE)) - } else { - Response = httr::POST(paste0(projectURL,"/",directory,".json?auth=",token), body = jsonlite::toJSON(output, auto_unbox = TRUE)) - } + output = fileConversion(x) + if (token == "none") { + Response = httr::POST(paste0(projectURL,"/",directory,".json"), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } else { + Response = httr::POST(paste0(projectURL,"/",directory,".json?auth=",token), body = jsonlite::toJSON(output, auto_unbox = TRUE)) + } return(paste0(directory,"/",httr::content(Response)$name)) } @@ -48,25 +117,25 @@ fileConversion <- function(x){ #' } download <- function(projectURL, fileName, secretKey = "none", token = "none", isClass = FALSE) { - if (secretKey == "none" && token == "none") { - urlPath = paste0(projectURL,"/",fileName,".json") - } else if (token != "none") { - urlPath = paste0(projectURL,"/",fileName,".json?auth=",token) - } else { - urlPath = paste0(projectURL,"/",fileName,".json?auth=",secretKey) - } + if (secretKey == "none" && token == "none") { + urlPath = paste0(projectURL,"/",fileName,".json") + } else if (token != "none") { + urlPath = paste0(projectURL,"/",fileName,".json?auth=",token) + } else { + urlPath = paste0(projectURL,"/",fileName,".json?auth=",secretKey) + } - data = httr::GET(urlPath) + data = httr::GET(urlPath) - if (is.null(jsonlite::fromJSON(httr::content(data,"text")))) warning("No data found at database location.") - if (isClass) { - retrievedData = httr::content(data,"text") - tempPath = tempfile() - writeBin(jsonlite::base64_dec(jsonlite::fromJSON(retrievedData)), tempPath) - return(readRDS(tempPath)) - } else { - return(jsonlite::fromJSON(httr::content(data,"text"))) - } + if (is.null(jsonlite::fromJSON(httr::content(data,"text")))) warning("No data found at database location.") + if (isClass) { + retrievedData = httr::content(data,"text") + tempPath = tempfile() + writeBin(jsonlite::base64_dec(jsonlite::fromJSON(retrievedData)), tempPath) + return(readRDS(tempPath)) + } else { + return(jsonlite::fromJSON(httr::content(data,"text"))) + } } #' @title The firebase database backup function: @@ -89,8 +158,8 @@ dataBackup <- function(projectURL, secretKey="prompt", fileName){ print("Fetching Data") urlPath = paste0(projectURL,"/.json?auth=",secretKey) curl::curl_download(url = urlPath, - destfile = fileName, - quiet = FALSE) + destfile = fileName, + quiet = FALSE) print(paste0("Backup created in ", fileName)) } @@ -107,9 +176,9 @@ dataBackup <- function(projectURL, secretKey="prompt", fileName){ #' } auth <- function(projectAPI, email="prompt", password="prompt"){ if (password == "prompt" && email == "prompt") { - email <- readline(prompt = "Email: ") - password <- readline(prompt = "Password: ") - print("Connecting to SpatialMaps:") + email <- readline(prompt = "Email: ") + password <- readline(prompt = "Password: ") + print("Connecting to SpatialMaps:") } AuthUrl = paste0("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=", projectAPI) userData = httr::POST(url = AuthUrl, body = list("email" = email, "password" = password, "returnSecureToken" = "True"), encode = "json") @@ -188,4 +257,3 @@ path_check <- function(path){ if (path_replaced != path) warning(paste0("path changed to ", path_replaced)) return(path) } - diff --git a/man/delete.Rd b/man/delete.Rd new file mode 100644 index 0000000..60c6481 --- /dev/null +++ b/man/delete.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/index.R +\name{delete} +\alias{delete} +\title{The firebase data delete function:} +\usage{ +delete(x, projectURL, directory = "main", token = "none") +} +\arguments{ +\item{x}{A list, data.frame or data.table {object}} + +\item{projectURL}{The Firebase project URL {string}} + +\item{directory}{The optimal Firebase subdirectory {string}} + +\item{token}{The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string}} +} +\value{ +returns http request answer. +} +\description{ +The function allows to delete data objects, such as variables, lists and data.frames +} +\examples{ +\dontrun{ +delete(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +} +} +\author{ +Kevin McGinley +} diff --git a/man/patch.Rd b/man/patch.Rd new file mode 100644 index 0000000..263de27 --- /dev/null +++ b/man/patch.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/index.R +\name{patch} +\alias{patch} +\title{The firebase data patch function:} +\usage{ +patch(x, projectURL, directory = "main", token = "none") +} +\arguments{ +\item{x}{A list, data.frame or data.table {object}} + +\item{projectURL}{The Firebase project URL {string}} + +\item{directory}{The optimal Firebase subdirectory {string}} + +\item{token}{The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string}} +} +\value{ +returns http request answer. +} +\description{ +The function allows to update data objects, such as variables, lists and data.frames +} +\examples{ +\dontrun{ +patch(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +} +} +\author{ +Kevin McGinley +} diff --git a/man/put.Rd b/man/put.Rd new file mode 100644 index 0000000..03a02f2 --- /dev/null +++ b/man/put.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/index.R +\name{put} +\alias{put} +\title{The firebase data put function:} +\usage{ +put(x, projectURL, directory = "main", token = "none") +} +\arguments{ +\item{x}{A list, data.frame or data.table {object}} + +\item{projectURL}{The Firebase project URL {string}} + +\item{directory}{The optimal Firebase subdirectory {string}} + +\item{token}{The user access token that can be retrieved with the auth() function. Required when if the database rules specify the need for user authentications. {string}} +} +\value{ +returns http request answer. +} +\description{ +The function allows to update data objects, such as variables, lists and data.frames +} +\examples{ +\dontrun{ +patch(x = mtcars, projectURL = "https://firedata-b0e54.firebaseio.com/", directory = "main") +} +} +\author{ +Kevin McGinley +}