-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathb2GetFileInfo.R
60 lines (54 loc) · 2.02 KB
/
b2GetFileInfo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#' B2 Get File Info.
#'
#' \code{b2GetFileInfo} returns information about a single file from the user's
#' account on the Backblaze B2 cloud storage product.
#'
#' This function returns information about a single file from the user's account
#' on the Backblaze B2 cloud storage product. Further details regarding this API
#' call are available here:
#'
#' \url{https://www.backblaze.com/b2/docs/b2_get_file_info.html}
#'
#' \code{fileId} is mandatory and must be user defined.
#'
#' @param fileId The unique identifier of the file whose information is
#' required. File IDs may be obtained through the \code{b2ListFiles},
#' \code{b2ListFileVersions} and \code{b2UploadFile} functions in this
#' package.
#' @return If successful a list will be returned containing \code{fileId},
#' \code{fileName}, \code{accountId}, \code{contentSha1}, \code{bucketId},
#' \code{contentLength}, \code{contentType} and \code{fileInfo}.
#'
#' @examples
#' \dontrun{
#' b2GetFileInfo(fileId = "Unique_identifier_of_the_file")
#' }
#'
#' @export
b2GetFileInfo <- function(fileId) {
# 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)
# API call
b2Return <-
httr::POST(
paste(
apiUrl,"/b2api/v1/b2_get_file_info", sep = ""
), body = jsonlite::toJSON(jsonlite::unbox(fileId), pretty = TRUE), httr::add_headers(
'Authorization' = as.character(authorizationToken)
)
)
# Check for bad authorisation and sent message
if (httr::status_code(b2Return) != "200") {
badReturn <- jsonlite::fromJSON(httr::content(b2Return,type = "text"))
stop(
"\nSomething went wrong. Please check the function options to ensure valid values. \n",
"\nStatus Code: ", badReturn$code, "\nMessage: ", badReturn$message
)
} else {
# Output as dataframe
jsonlite::fromJSON(httr::content(b2Return, type = "text"))
}
}