Skip to content

Commit

Permalink
simplify checking/parsing of results
Browse files Browse the repository at this point in the history
  • Loading branch information
fmichonneau committed Mar 14, 2016
1 parent 72c2d06 commit 008c9a0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 46 deletions.
12 changes: 3 additions & 9 deletions R/api-gol.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
## Summary information about the Graph of Life
.gol_about <- function(...) {
res <- otl_POST(path="graph/about", body=list(), ...)
cont <- otl_parse(res)
if (length(cont) < 1) {
warning("Nothing returned")
}
return(cont)
res
}


Expand All @@ -33,8 +29,7 @@
q <- list(study_id=jsonlite::unbox(study_id), tree_id=jsonlite::unbox(tree_id),
git_sha=jsonlite::unbox(git_sha))
res <- otl_POST(path="graph/source_tree", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}

##' @importFrom jsonlite unbox
Expand All @@ -53,6 +48,5 @@
q <- list(ott_id=jsonlite::unbox(ott_id), include_lineage=jsonlite::unbox(include_lineage))
}
res <- otl_POST(path="graph/node_info", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}
19 changes: 7 additions & 12 deletions R/api-studies.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
res <- otl_POST(path="studies/find_studies/",
body=req_body,
...)
cont <- otl_parse(res)
return(cont)
res
}

##' @importFrom jsonlite unbox
Expand Down Expand Up @@ -65,17 +64,15 @@
body=c(req_body,
jsonlite::unbox(verbose),
jsonlite::unbox(exact)), ...)
cont <- otl_parse(res)
return(cont)
res
}


##' @importFrom httr content
## Return a list of properties that can be used to search studies and trees
.studies_properties <- function() {
res <- otl_POST(path="studies/properties/", body=list())
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -92,8 +89,7 @@
res <- otl_GET(path=paste("study",
paste0(study_id, otl_formats(format)), sep="/"),
...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -117,13 +113,12 @@
tip_label <- paste0("/?tip_label=", tip_label)
tree_file <- paste0(tree_id, otl_formats(format), tip_label)
res <- otl_GET(path=paste("study", study_id, "tree", tree_file, sep="/"), ...)
cont <- otl_parse(res)
return(cont)
res
}

##' @importFrom httr content
.get_study_meta <- function(study_id, ...) {
otl_parse(otl_GET(path= paste("study", study_id, "meta", sep="/"), ...))
otl_GET(path= paste("study", study_id, "meta", sep="/"), ...)
}


Expand All @@ -149,7 +144,7 @@
format <- otl_formats(format)
url_stem <- paste("study", study_id, "tree", paste0(tree_id, format), sep="/")
res <- otl_GET(path=paste(url_stem, "?subtree_id=", subtree_id, sep=""), ...)
otl_parse(res)
res
}

### Let's not worry about those for now, as their results could be
Expand Down
13 changes: 4 additions & 9 deletions R/api-taxonomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
## Summary information about the OpenTree Taxaonomy (OTT)
.taxonomy_about <- function(...) {
res <- otl_POST(path="/taxonomy/about", body=list(), ...)
cont <- otl_parse(res)

return(cont)
res
}


Expand All @@ -28,8 +26,7 @@
include_lineage = jsonlite::unbox(include_lineage),
list_terminal_descendants = jsonlite::unbox(list_terminal_descendants))
res <- otl_POST(path="/taxonomy/taxon", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -46,8 +43,7 @@
}
q <- list(ott_id=jsonlite::unbox(ott_id))
res <- otl_POST(path="/taxonomy/subtree", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -61,6 +57,5 @@
}
q <- list(ott_ids=ott_ids)
res <- otl_POST(path="/taxonomy/lica", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}
9 changes: 3 additions & 6 deletions R/api-tnrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@
q <- q[!toKeep]

res <- otl_POST("tnrs/match_names", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}


##' @importFrom httr content
## Get OpenTree TNRS contexts
.tnrs_contexts <- function(...) {
res <- otl_POST("tnrs/contexts", body=list(), ...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -63,6 +61,5 @@
}
q <- list(names=names)
res <- otl_POST("tnrs/infer_context", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}
12 changes: 4 additions & 8 deletions R/api-tol.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
q <- list(study_list=jsonlite::unbox(study_list))
res <- otl_POST(path="tree_of_life/about", body=q, ...)
cont <- otl_parse(res)
return(invisible(cont))
res
}


Expand All @@ -26,8 +25,7 @@
q <- list(ott_ids = ott_ids)
}
res <- otl_POST(path="tree_of_life/mrca", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -47,8 +45,7 @@
q <- list(ott_id = jsonlite::unbox(ott_id))
}
res <- otl_POST(path="tree_of_life/subtree", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}


Expand All @@ -71,6 +68,5 @@
q <- list(ott_ids = ott_ids)

res <- otl_POST("tree_of_life/induced_subtree", body=q, ...)
cont <- otl_parse(res)
return(cont)
res
}
4 changes: 2 additions & 2 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ otl_check_error <- function(cont) {
}
}

## Check and parse result of query
otl_check <- function(req) {
if (!req$status_code < 400) {
msg <- otl_parse(req)
stop("HTTP failure: ", req$status_code, "\n", msg, call. = FALSE)
}
desc <- otl_parse(req)
otl_check_error(desc)
desc
}

##' @importFrom httr GET
otl_GET <- function(path, dev_url = FALSE, otl_v = otl_version(), ...) {
req <- httr::GET(otl_url(), path=paste(otl_v, path, sep="/"), ...)
otl_check(req)
req
}

##' @importFrom jsonlite toJSON
Expand All @@ -69,7 +70,6 @@ otl_POST <- function(path, body, dev_url = FALSE, otl_v = otl_version(), ...) {
path=paste(otl_v, path, sep="/"),
body=body_json, ...)
otl_check(req)
req
}


Expand Down

0 comments on commit 008c9a0

Please sign in to comment.