My First API (This is the Final API - Robert and Joe were finished)
library(plumber)
library(stringr)
library(jsonlite)
library(httr)
#* @apiTitle Simple API
#* Echo provided text
#* @param text The text to be echoed in the response
#* @param number A number to be echoed in the response
#* @get /echo
#* @post /echo
function(req,
text = "",
number = 1) {
queryJSON = list(
format = 'json',
gene = 'KRAS',
cohort = 'PAAD',
page = 1,
page_size = 100,
sort_by = 'tcga_participant_barcode'
)
hosturl = 'https://firebrowse.herokuapp.com'
endpointurl='http://firebrowse.org/api/v1/Samples/mRNASeq'
query.as.json <- paste(names(queryJSON),"=",queryJSON,sep="",collapse="&")
print(query.as.json)
call2 <- paste(hosturl ,'?',endpointurl ,'?' , query.as.json, sep="")
print(call2)
result.new <- content(POST(call2))
df <- as.data.frame(t(sapply(result.new[[1]],FUN =function(x){x})))
secret = c('a','b','c')
number = as.numeric(number)
theresult = list(
message_echo = paste("The text is:", text),
number_echo = paste("The number is:", number),
secret_echo = paste("The secret is:", secret[number]),
secret_echo2 = sprintf("The secret is: %s", secret),
df_echo = df,
raw_body = req$postBody
)
print('-----------------------------')
print(theresult)
return(theresult)
}
API Script
‘’’ Rlibrary(plumber)library(stringr)library(jsonlite)
* @apiTitle Simple API
* Echo provided text
* @param text The text to be echoed in the response
* @param number A number to be echoed in the response
* @get /echo
* @post /echofunction(req, text = “”, number = 1) { secret = c(‘a’,’b’,’c’)# plumber::plumb("myFirstAPI.R")$run(port = 5762)number = as.numeric(number)theresult = list(message_echo = paste("The text is:", text),number_echo = paste("The number is:", number),secret_echo = paste("The secret is:", secret[number]),secret_echo2 = sprintf("The secret is: %s", secret),raw_body = req$postBody ) print(‘——————————————-‘) print(theresult) return(theresult)}‘’’