Skip to content

Instantly share code, notes, and snippets.

@Dulani
Created November 16, 2016 20:18
Show Gist options
  • Save Dulani/e34f747b7f47d9a9537c0a236db1f907 to your computer and use it in GitHub Desktop.
Save Dulani/e34f747b7f47d9a9537c0a236db1f907 to your computer and use it in GitHub Desktop.
R helper functions for performing WebDAV tasks
library(curl)
library(XML)
listFiles <- function(username, password, relPath = "/", dav = "https://dav.box.com/dav") {
uri <- URLencode(paste(dav, relPath, sep=""))
# fetch directory listing via curl and parse XML response
h <- new_handle()
handle_setopt(h, customrequest = "PROPFIND")
handle_setopt(h, username = username)
handle_setopt(h, password = password)
response <- curl_fetch_memory(uri, h)
text <- rawToChar(response$content)
doc <- xmlParse(text, asText=TRUE)
# calculate relative paths
base <- paste(paste("/", strsplit(uri, "/")[[1]][-1:-3], sep="", collapse=""), "/", sep="")
result <- unlist(
xpathApply(doc, "//d:response/d:href", function(node) {
sub(base, "", URLdecode(xmlValue(node)), fixed=TRUE)
})
)
result[result != ""]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment