Skip to content

Commit

Permalink
Merge pull request #11 from KevinMcGin/master
Browse files Browse the repository at this point in the history
Adds HTTP requests (put, patch & delete)
  • Loading branch information
Kohze committed Sep 19, 2018
2 parents f8b04a6 + 1cb98e0 commit d1f3177
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 51 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 0 additions & 21 deletions FireData.Rproj

This file was deleted.

3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
126 changes: 97 additions & 29 deletions R/index.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
}

Expand Down Expand Up @@ -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:
Expand All @@ -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))
}

Expand All @@ -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")
Expand Down Expand Up @@ -188,4 +257,3 @@ path_check <- function(path){
if (path_replaced != path) warning(paste0("path changed to ", path_replaced))
return(path)
}

31 changes: 31 additions & 0 deletions man/delete.Rd

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

31 changes: 31 additions & 0 deletions man/patch.Rd

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

31 changes: 31 additions & 0 deletions man/put.Rd

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

0 comments on commit d1f3177

Please sign in to comment.