Skip to content

Commit

Permalink
api model interface (unfinished) #3 + get<Class> return changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rpc5102 committed Oct 28, 2019
1 parent c1660fe commit 5b8fc16
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 99 deletions.
10 changes: 2 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export(getInteractionComponent)
export(getInteractionComponents)
export(getInteractionType)
export(getInteractionTypes)
export(getInterface)
export(getInterfaceList)
export(getModel)
export(getModelList)
export(getObjectDefinition)
Expand All @@ -41,16 +43,8 @@ export(retrieve)
export(store)
export(test)
export(validateObject)
import("for")
import(API)
import(Learning)
import(Locker)
import(Sets)
import(htmltools)
import(httr)
import(needed)
import(scripts)
import(shiny)
import(up)
importFrom(jsonlite,toJSON)
importFrom(uuid,UUIDgenerate)
4 changes: 2 additions & 2 deletions R/activity.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ getActivityType <- function(name, asJSON = FALSE) {
exists <- exists(name, activityTypes)

if (exists & asJSON) {
return(formatJSON(activityTypes[name]))
return(formatJSON(activityTypes[[name]]))
} else if (exists) {
return(activityTypes[name])
return(activityTypes[[name]])
} else {
return(-1)
}
Expand Down
18 changes: 18 additions & 0 deletions R/config.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#'config
#'
#' @name config
NULL

set_locker_config <- function(config){
httr::set_config(
add_headers(
Authorization = config$auth
)
)

invisible(options(locker_config = config))
}

get_locker_config <- function(){
return(getOption("locker_config"))
}
84 changes: 6 additions & 78 deletions R/connect.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#' connect
#'connect
#'
#' @name connect
#'
#' @import htmltools httr
NULL

#' Sets up scripts needed for Learning Locker API
#'
Expand All @@ -21,11 +24,7 @@ connect <- function(session, config) {
session = getDefaultReactiveDomain()
)

set_config(
add_headers(
Authorization = config$auth
)
)
set_locker_config(config)

# Pass locker configuration to begin connection
session$sendCustomMessage("rlocker-setup", config)
Expand All @@ -51,7 +50,7 @@ test <- function(cfg) {
tryCatch({

response <- with_config(config(), GET(
paste0(cfg$base_url, "api/connection/statement"),
httr::modify_url(cfg$base_url, "api/connection/statement"),
))

status <- status_code(response)
Expand All @@ -73,74 +72,3 @@ test <- function(cfg) {

return(status)
}

#' Makes a simple HTTP GET request to Learning Locker API endpoint
#' Uses HTTP Connection Interface
#' @seealso \link{https://docs.learninglocker.net/http-statements/}
#'
#' ### ### ### ### ### ### ### ### ### THESE NEED TO BE UPDATED ### ### ### ### ### ### ### ### ###
#' Should this be renamed to retrieve as we're only making get requests? api_retrieve rlocker::retrieve
#' api/connection should be added to the base request automatically, we're not supporting REST at this time
#' base_url + "api/connection" + request_type + query
#' create new functions for connection details set_locker_config() / get_locker_config() (there's a collision with set_config in httr)
#'
#' @param session The current R session
#' @param request API request string
#' @param asJSON (optional) Return content as json string
#'
#' @return response content
#'
#' @export
retrieve <- function(model, interface = "connection", query, asJSON = FALSE){

# @todo if interface == connection, query belongs as html encoded + json encoded ?queryString
# else query belongs after the model
# @todo create a method to return available model names
request <- paste(get_locker_config()$base_url, "api", interface, model, query, sep = "/")

response <- with_config(config(), GET(request))

status <- response$status

content <- httr::content(response)

#' @details https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
if (status != 200) {
message(
paste(status,
stringr::str_squish(
stringr::str_trim(gsub("<.*?>|\n", " ", content(response, as = "text")), side = "both")
)
)
)

content <- NA
} else {
if(asJSON){
content <- formatJSON(content)
}
}

return(content)
}

#' Stores an xAPI Statement
#'
#' @param statement xAPI Statement
#' @param warn Show warnings
#'
#' @seealso \code{\link{createStatement}}
#'
#' @return HTTP Status
#'
#' @export
store <- function(session, statement = NULL, warn = FALSE, ...) {
# Pass the statement to the js handler
session$sendCustomMessage("rlocker-store", statement)

# HTTP Status Code
status_code <- ifelse(!is.null(session$input$storageStatus), session$input$storageStatus, 502)

# Return HTTP_STATUS
return(status_code)
}
15 changes: 10 additions & 5 deletions R/object.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ getInteractionType <- function(name, asJSON = FALSE) {
exists <- exists(name, interactionTypes)

if(exists & asJSON) {
return(formatJSON(interactionTypes[name]))
return(formatJSON(interactionTypes[[name]]))
} else if(exists) {
return(interactionTypes[name])
return(interactionTypes[[name]])
} else {
return(-1)
}
Expand All @@ -144,9 +144,14 @@ getInteractionComponent <- function(name, asJSON = FALSE){
exists <- exists(name, components)

if(exists & asJSON) {
return(formatJSON(components[name]))
return(formatJSON(components[[name]]))
} else if(exists) {
return(components[name])
return(
structure(
components[[name]],
class = "component"
)
)
} else {
return(-1)
}
Expand All @@ -164,7 +169,7 @@ getSupportedComponents <- function(interactionType) {
if(is.na(exists)){
return(NA)
} else {
return(getInteractionType(interactionType)[[1]]$components)
return(getInteractionType(interactionType)$components)
}
}

Expand Down
90 changes: 90 additions & 0 deletions R/request.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#'request
#'
#' @name request
NULL

#' Makes a simple HTTP GET request to Learning Locker API endpoint
#' Uses HTTP Connection Interface
#' @seealso \link{https://docs.learninglocker.net/http-statements/}
#'
#' ### ### ### ### ### ### ### ### ### THESE NEED TO BE UPDATED ### ### ### ### ### ### ### ### ###
#' Should this be renamed to retrieve as we're only making get requests? api_retrieve rlocker::retrieve
#' api/connection should be added to the base request automatically, we're not supporting REST at this time
#' base_url + "api/connection" + request_type + query
#' create new functions for connection details set_locker_config() / get_locker_config() (there's a collision with set_config in httr)
#'
#' @param session The current R session
#' @param request API request string
#' @param asJSON (optional) Return content as json string
#'
#' @return response content
#'
#' @export
retrieve <- function(model = NULL, interface = "connection", query, asJSON = FALSE){

# @todo if interface == connection, query belongs as html encoded + json encoded ?queryString
# else query belongs after the model

tryCatch({
if(is.null(model) || getModel(model) == -1){
stop("Unable to process request; api model not specified or is invalid.")
}

config <- get_locker_config()

interface <- getInterface(interface)

request <- modify_url(
config$base_url,
path = paste(interface$route, model, sep = "/"),
query = query
)

response <- with_config(config(), GET(request))

status <- response$status

content <- httr::content(response)

#' @details https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
if (status != 200) {
message(
paste(status,
stringr::str_squish(
stringr::str_trim(gsub("<.*?>|\n", " ", content(response, as = "text")), side = "both")
)
)
)

content <- NA
} else {
if(asJSON){
content <- formatJSON(content)
}
}

return(content)

})
}

#' Stores an xAPI Statement
#'
#' @param statement xAPI Statement
#' @param warn Show warnings
#'
#' @seealso \code{\link{createStatement}}
#'
#' @return HTTP Status
#'
#' @export
store <- function(session, statement = NULL, warn = FALSE, ...) {
# Pass the statement to the js handler
session$sendCustomMessage("rlocker-store", statement)

# HTTP Status Code
status_code <- ifelse(!is.null(session$input$storageStatus), session$input$storageStatus, 502)

# Return HTTP_STATUS
return(status_code)
}
9 changes: 7 additions & 2 deletions R/verb.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ getVerb <- function(name, asJSON = FALSE) {
exists <- exists(name, verbs)

if (exists & asJSON) {
return(formatJSON(verbs[name]))
return(formatJSON(verbs[[name]]))
} else if(exists) {
return(verbs[name])
return(
structure(
verbs[[name]],
class = "verb"
)
)
} else {
return(-1)
}
Expand Down
2 changes: 1 addition & 1 deletion inst/examples/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ shinyApp(
# Initialize Learning Locker connection
connection <- rlocker::connect(session, config)

response <- rlocker::api_request(paste0(config$base_url, "api/connection/statement?first=1"), asJSON = TRUE)
response <- rlocker::retrieve("api/connection/statement?first=1", asJSON = TRUE)

print(response)

Expand Down
8 changes: 8 additions & 0 deletions man/config.Rd

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

2 changes: 2 additions & 0 deletions man/connect.Rd

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

14 changes: 14 additions & 0 deletions man/getInterfaceList.Rd

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

8 changes: 8 additions & 0 deletions man/request.Rd

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

5 changes: 3 additions & 2 deletions man/retrieve.Rd

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

2 changes: 1 addition & 1 deletion man/store.Rd

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

0 comments on commit 5b8fc16

Please sign in to comment.