Skip to content
This repository has been archived by the owner on Jun 7, 2023. It is now read-only.

added format option to getobjectdescriptio. #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 40 additions & 29 deletions R/rforcecom.getObjectDescription.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @export
rforcecom.getObjectDescription <-
function(session, objectName){

function(session, objectName, format="XML"){
if (!format %in% c("XML", "JSON")){break("Format must be XML or JSON")}
# Send a query
endpointPath <- rforcecom.api.getObjectDescriptionEndpoint(session['apiVersion'], objectName)
URL <- paste(session['instanceURL'], endpointPath, sep="")
Expand All @@ -15,34 +15,45 @@ function(session, objectName){
if(exists("rforcecom.debug") && rforcecom.debug){ message(res.content) }
# END DEBUG

# Parse XML
x.root <- xmlRoot(xmlTreeParse(res.content, asText=T))

# Check whether it success or not
errorcode <- NA
errormessage <- NA
try(errorcode <- iconv(xmlValue(x.root[['Error']][['errorCode']]), from="UTF-8", to=""), TRUE)
try(errormessage <- iconv(xmlValue(x.root[['Error']][['message']]), from="UTF-8", to=""), TRUE)
if(!is.na(errorcode) && !is.na(errormessage)){
stop(paste(errorcode, errormessage, sep=": "))
}

# Parse XML
xdf <- getNodeSet(xmlParse(res.content), "//fields")
xdfList <- sapply(xdf,xmlToList)
xdfDFList <- sapply(xdfList,data.frame)
set <- function(x,y){
coln <- unique(c(colnames(x),colnames(y)))
x[coln[!coln %in% colnames(x)]] <- NA
y[coln[!coln %in% colnames(y)]] <- NA
rbind(x,y)
# Switch between JSON and XML format. XML standard
if (format=="XML"){
# Parse XML
x.root <- xmlRoot(xmlTreeParse(res.content, asText=T))

# Check whether it success or not
errorcode <- NA
errormessage <- NA
try(errorcode <- iconv(xmlValue(x.root[['Error']][['errorCode']]), from="UTF-8", to=""), TRUE)
try(errormessage <- iconv(xmlValue(x.root[['Error']][['message']]), from="UTF-8", to=""), TRUE)
if(!is.na(errorcode) && !is.na(errormessage)){
stop(paste(errorcode, errormessage, sep=": "))
}

# Parse XML
xdf <- getNodeSet(xmlParse(res.content), "//fields")
xdfList <- sapply(xdf,xmlToList)
xdfDFList <- sapply(xdfList,data.frame)
set <- function(x,y){
coln <- unique(c(colnames(x),colnames(y)))
x[coln[!coln %in% colnames(x)]] <- NA
y[coln[!coln %in% colnames(y)]] <- NA
rbind(x,y)
}
for(i in seq(xdfDFList)[-1]){
xdfDFList[[1]] <- set(xdfDFList[[1]], xdfDFList[[i]])
}
xdf <- xdfDFList[[1]]

return(xdf)}
else if (format="JSON"){
jsonContent <-
jsonlite::fromJSON(res.content)

df <-
jsonContent$fields

return(df)
}
for(i in seq(xdfDFList)[-1]){
xdfDFList[[1]] <- set(xdfDFList[[1]], xdfDFList[[i]])
}
xdf <- xdfDFList[[1]]

return(xdf)

}