devtools::use_package("httr")
devtools::use_package("jsonlite")
library(httr)
library(jsonlite)
flywheel = function(APIKEY=NULL)
{
if (!requireNamespace("httr", quietly = TRUE)) {
stop("library httr is needed for this function to work. Please install it.",
call. = FALSE)
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
stop("library jsonlite is needed for this function to work. Please install it.",
call. = FALSE)
}
## Get the environment for this
## instance of the function.
thisEnv <- environment()
api_key <- ""
base_url <- ""
debug_mode <- FALSE
## Create the list used to represent an
## object for this class
me <- list(
## Define the environment where this list is defined so
## that I can refer to it later.
thisEnv = thisEnv,
## Define the accessors for the data fields.
getEnv = function()
{
return(get("thisEnv",thisEnv))
},
getApiKey = function()
{
return(get("api_key",thisEnv))
},
setApiKey = function(value)
{
return(assign("api_key",value,thisEnv))
},
getBaseURL = function()
{
return(get("base_url",thisEnv))
},
setBaseURL = function(value)
{
return(assign("base_url",value,thisEnv))
},
setDebugMode = function(value)
{
return(assign("debug_mode",value,thisEnv))
},
getDebugMode = function()
{
return(get("debug_mode",thisEnv))
},
getFlywheel = function(endpoint) {
protocol <- "https://"
baseURL <- this$getBaseURL()
apiKey <- this$getApiKey()
authHeader <- paste("scitran-user ",apiKey,sep="")
url <- paste(protocol,baseURL,"/api/",endpoint,sep="")
#print(this$getDebugMode())
#print(this$getDebugMode()==TRUE)
if (this$getDebugMode()==TRUE) {
print(url)
print(authHeader)
}
response = GET(url, add_headers(Authorization = authHeader))
contentType <- headers(response)['content-type']
if (this$getDebugMode()==TRUE) {
print(response)
print(contentType)
}
#jsonDataFrame = fromJSON(content(response,as = "text"), simplifyDataFrame=TRUE)
result <- content(response,as = "text")
if (this$getDebugMode()==TRUE) {
if (contentType=="application/json; charset=utf-8") print(prettify(result, indent = 4))
}
return(result)
},
putFlywheel = function(endpoint, payload) {
protocol <- "https://"
baseURL <- this$getBaseURL()
apiKey <- this$getApiKey()
authHeader <- paste("scitran-user ",apiKey,sep="")
url <- paste(protocol,baseURL,"/api/",endpoint,sep="")
if (this$getDebugMode()==TRUE) {
print(url)
print(authHeader)
}
response = PUT(url, body=payload, add_headers(Authorization = authHeader), content_type_json(), verbose())
if (this$getDebugMode()==TRUE) {
print(payload)
print(response)
}
#jsonDataFrame = fromJSON(content(response,as = "text"), simplifyDataFrame=TRUE)
result = content(response,as = "text")
#print(prettify(result, indent = 4))
return(result)
},
getCurrentUser = function() {
return(this$getFlywheel("users/self"))
},
getAllUsers = function() {
return(this$getFlywheel("users"))
},
getUser = function(key) {
endpoint = paste("users/",key, sep="")
return(this$getFlywheel(endpoint))
},
getAllGroups = function() {
return(this$getFlywheel("groups"))
},
getGroup = function(key) {
endpoint = paste("groups/",key, sep="")
return(this$getFlywheel(endpoint))
},
getAllProjects = function() {
return(this$getFlywheel("projects"))
},
getProject = function(key) {
endpoint = paste("projects/",key, sep="")
return(this$getFlywheel(endpoint))
},
getProjectByName = function(project_name) {
projects_df <- fromJSON(this$getAllProjects(), simplifyDataFrame=TRUE)
return(projects_df[projects_df$label==project_name,])
},
getProjectIdByName = function(project_name) {
project_df <- this$getProjectByName(project_name)
return(project_df$'_id')
},
getProjectsDataFrame = function() {
projects_df <- fromJSON(this$getAllProjects(), simplifyDataFrame=TRUE)
return(projects_df)
},
getProjectSessions = function(key) {
endpoint = paste("projects/",key, "/sessions", sep="")
return(this$getFlywheel(endpoint))
},
getProjectSessionsDataFrame = function(project_id) {
sessions_df <- fromJSON(this$getProjectSessions(project_id), simplifyDataFrame=TRUE)
return(sessions_df)
},
putProjectInfo = function(project, json) {
endpoint = paste("projects/",project, sep="")
payload = paste(' { "info": ', json, '}')
return(this$putFlywheel(endpoint, payload))
},
putProjectInfoKey = function(project, key, value) {
payload = paste(' { "',key, '": ', toJSON(value, auto_unbox=TRUE), '}', sep="")
print(payload)
return(this$putProjectInfo(project, payload))
},
getProjectFileUrl = function(project, filename) {
endpoint = paste("projects/", project, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","projects/", project, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getProjectAnalysisFileUrl = function(project, analysis, filename) {
endpoint = paste("projects/", project, "/analyses/", analysis, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","projects/", project, "/analyses/", analysis, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getAllSessions = function() {
return(this$getFlywheel("sessions"))
},
getSession = function(key) {
endpoint = paste("sessions/",key, sep="")
return(this$getFlywheel(endpoint))
},
getSessionByName = function(session_label) {
sessions_df <- fromJSON(this$getAllSessions(), simplifyDataFrame=TRUE)
return(sessions_df[sessions_df$label==session_label,])
},
getSessionIdByName = function(session_label) {
session_df <- this$getSessionByName(session_label)
return(session_df$'_id')
},
getSessionsDataFrame = function(project_id) {
sessions_df <- fromJSON(this$getAllProjects(), simplifyDataFrame=TRUE)
return(projects_df)
},
getSessionAnalysis = function(session, analysis) {
endpoint = paste("sessions/", session, "/analyses/", analysis, sep="")
return(this$getFlywheel(endpoint))
},
getSessionAnalysisFile = function(session, analysis, filename) {
endpoint = paste("sessions/", session, "/analyses/", analysis, "/files/", filename, "?view=true", sep="")
return(this$getFlywheel(endpoint))
},
getSessionAnalysisFileUrl = function(session, analysis, filename) {
endpoint = paste("sessions/", session, "/analyses/", analysis, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","sessions/", session, "/analyses/", analysis, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getSessionFileUrl = function(session, filename) {
endpoint = paste("sessions/", session, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","sessions/", session, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
putSessionInfo = function(session, json) {
endpoint = paste("sessions/",session, sep="")
payload = paste(' { "info": ', json, '}')
return(this$putFlywheel(endpoint, payload))
},
putSessionInfoKey = function(session, key, value) {
payload = paste(' { "',key, '": ', toJSON(value, auto_unbox=TRUE), '}', sep="")
print(payload)
return(this$putSessionInfo(session, payload))
},
putSubjectInfo = function(session, value) {
endpoint = paste("sessions/",session, sep="")
payload = paste('{ "subject": { "info": ', value, '}}')
return(this$putFlywheel(endpoint, payload))
},
putSubjectInfoKey = function(session, key, value) {
payload = paste(' { "',key, '": ', toJSON(value, auto_unbox=TRUE), '}', sep="")
print(payload)
return(this$putSubjectInfo(session, payload))
},
getAllAcquisitions = function() {
return(this$getFlywheel("acquisitions"))
},
getAcquisition = function(key) {
endpoint = paste("acquisitions/",key, sep="")
return(this$getFlywheel(endpoint))
},
getAcquisitionFileUrl = function(acquisition, filename) {
endpoint = paste("acquisitions/", acquisition, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","acquisitions/", acquisition, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
putAcquisitionInfo = function(acquisition, json) {
endpoint = paste("acquisitions/",acquisition, sep="")
payload = paste(' { "info": ', json, '}')
return(this$putFlywheel(endpoint, payload))
},
putAcquisitionInfoKey = function(acquisition, key, value) {
payload = paste(' { "',key, '": ', toJSON(value, auto_unbox=TRUE), '}', sep="")
print(payload)
return(this$putAcquisitionInfo(acquisition, payload))
},
getAcquisitionAnalysis = function(acquisition, analysis) {
endpoint = paste("acquisitions/", acquisition, "/analyses/", analysis, sep="")
return(this$getFlywheel(endpoint))
},
getAcquisitionAnalysisFileUrl = function(acquisition, analysis, filename) {
endpoint = paste("acquisitions/", acquisition, "/analyses/", analysis, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","acquisitions/", acquisition, "/analyses/", analysis, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getAllCollections = function() {
return(this$getFlywheel("collections"))
},
getCollection = function(key) {
endpoint = paste("collections/",key, sep="")
return(this$getFlywheel(endpoint))
},
getCollectionByName = function(collection_name) {
collections_df <- fromJSON(this$getAllCollections(), simplifyDataFrame=TRUE)
return(collections_df[collections_df$label==collection_name,])
},
getCollectionIdByName = function(collection_name) {
collection_df <- this$getCollectionByName(collection_name)
return(collection_df$'_id')
},
getCollectionsDataFrame = function() {
collections_df <- fromJSON(this$getAllCollections(), simplifyDataFrame=TRUE)
return(collections_df)
},
getCollectionSessions = function(key) {
endpoint = paste("collections/",key, "/sessions", sep="")
return(this$getFlywheel(endpoint))
},
getCollectionSessionsDataFrame = function(collection_id) {
sessions_df <- fromJSON(this$getCollectionSessions(collection_id), simplifyDataFrame=TRUE)
return(sessions_df)
},
putCollectionInfo = function(collection, json) {
endpoint = paste("collections/",collection, sep="")
payload = paste(' { "info": ', json, '}')
return(this$putFlywheel(endpoint, payload))
},
putCollectionInfoKey = function(collection, key, value) {
payload = paste(' { "',key, '": ', toJSON(value, auto_unbox=TRUE), '}', sep="")
print(payload)
return(this$putCollectionInfo(collection, payload))
},
getCollectionFileUrl = function(collection, filename) {
endpoint = paste("collections/", collection, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","collections/", collection, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getCollectionAnalysisFileUrl = function(collection, analysis, filename) {
endpoint = paste("collections/", collection, "/analyses/", analysis, "/files/", filename, "?ticket=", sep="")
response <- this$getFlywheel(endpoint)
result = fromJSON(response)
url <- paste("https://",this$getBaseURL(),"/api/","collections/", collection, "/analyses/", analysis, "/files/", filename, "?ticket=",result$ticket, sep="")
return(url)
},
getAllGears = function() {
return(this$getFlywheel("gears"))
},
getGear = function(key) {
endpoint = paste("gears/",key, sep="")
return(this$getFlywheel(endpoint))
},
getVersion = function() {
return(this$getFlywheel("version"))
},
apikey = function(APIKEY) {
if (!is.null(APIKEY)) {
result <- unlist(strsplit(APIKEY, ":"))
if (length(result)==2) {
this$setBaseURL(result[1])
} else if (length(result)==3) {
this$setBaseURL(paste(result[1], result[2], sep=":"))
} else {
stop("Bad APIKEY")
}
this$setApiKey(APIKEY)
this$getCurrentUser()
}
}
)
## Define the value of the list within the current environment.
assign('this',me,envir=thisEnv)
# capture APIKEY and base_url if APIKEY provided - "connect"
this$apikey(APIKEY)
## Set the name for the class
class(me) <- append(class(me),"Flywheel")
return(me)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.