Skip to content

Commit

Permalink
merge json branch
Browse files Browse the repository at this point in the history
  • Loading branch information
gdower committed Feb 29, 2024
2 parents 19e4f38 + 6031065 commit 59b7ca7
Show file tree
Hide file tree
Showing 49 changed files with 607 additions and 130 deletions.
87 changes: 0 additions & 87 deletions .github/workflows/real-requests.yml

This file was deleted.

14 changes: 10 additions & 4 deletions R/http.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ api_user_token <- function() {

api_project_token <- function() {
if (!exists("TW_PROJECT_TOKEN", envir = globalenv())) {
cat("\033[38;2;255;255;0mNo TW_PROJECT_TOKEN variable is set\033[0m\n")
return()
}
get("TW_PROJECT_TOKEN", envir = globalenv())
Expand All @@ -23,7 +24,7 @@ add_token_params <- function(query) {
query <- c(query, list(
token = api_user_token(),
project_token = api_project_token()))
return(query)
return(cc(query))
}

serialize <- function(params) {
Expand All @@ -37,7 +38,7 @@ serialize <- function(params) {
} else {
url_params <- c(url_params,
paste0(
name, "=",
name, "=",
URLencode(as.character(params[[name]]))))
}
}
Expand Down Expand Up @@ -75,17 +76,22 @@ tw_GET <- function(
path <- paste0(path, ".csv")
}

# if token and project_token are null then add them to the query
# if tokens are null then add tokens from global environment
if (is.null(query$token) && is.null(query$project_token)) {
query <- add_token_params(query)
}


url_params_string <- serialize(query)
url <- paste0(api_base_url(), path, url_params_string)

cat(sprintf("\033[32mGET %s\033[39m\n", url))

if (csv) {
return(as_tibble(read.csv(url)))
return(as_tibble(read.csv(url, sep = "\t",
header = TRUE,
stringsAsFactors = FALSE,
na.strings = c("", "NA"))))
} else {
req <- request(url)
resp <- req_perform(req)
Expand Down
2 changes: 1 addition & 1 deletion R/tw_biological_associations.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ tw_biological_associations <- function(subresource = NULL, biological_associatio
match_identifiers_delimiter = NULL, match_identifiers_type = NULL,
namespace_id = NULL, note_exact = NULL, note_text = NULL, notes = NULL,
keyword_id_and = NULL, keyword_id_or = NULL, tags = NULL,
token = NULL, project_token = NULL, csv = TRUE, page = 0, per = 50, ...) {
token = NULL, project_token = NULL, csv = FALSE, page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
assert(per, c("numeric", "integer"))
Expand Down
6 changes: 3 additions & 3 deletions R/tw_collection_objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ tw_collection_objects <- function(biocuration_class_id = NULL, biological_associ
taxon_determinations = NULL, taxon_name_id = NULL, type_material = NULL, type_specimen_taxon_name_id = NULL,
user_date_end = NULL, user_date_start = NULL, user_id = NULL, user_target = NULL, validity = NULL,
with_buffered_collecting_event = NULL, with_buffered_determinations = NULL, with_buffered_other_labels = NULL,
wkt = NULL, token = NULL, project_token = NULL, page = 0, per = 50, ...) {
wkt = NULL, csv = FALSE, token = NULL, project_token = NULL, page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
assert(per, c("numeric", "integer"))
Expand Down Expand Up @@ -136,8 +136,8 @@ tw_collection_objects <- function(biocuration_class_id = NULL, biological_associ
with_buffered_other_labels = with_buffered_other_labels, wkt = wkt, token = token, project_token = project_token,
page = page, per = per))

res <- tw_GET(api_base_url(), "/collection_objects", query = args, ...)
return(res)
df <- tw_GET(api_base_url(), "/collection_objects", query = args, csv = csv, ...)
return(df)
}


Expand Down
4 changes: 2 additions & 2 deletions R/tw_otus.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tw_otus <- function(asserted_distribution_ids = NULL, biological_association_ids
keyword_id_or = NULL, name = NULL, note_exact = NULL, note_text = NULL, notes = NULL,
origin_citations = NULL, otu_id = NULL, tags = NULL,
taxon_name_classification_id = NULL, taxon_name_id = NULL,
taxon_name_relationship_id = NULL, token = NULL, project_token = NULL,
taxon_name_relationship_id = NULL, csv = FALSE, token = NULL, project_token = NULL,
page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
Expand All @@ -55,7 +55,7 @@ tw_otus <- function(asserted_distribution_ids = NULL, biological_association_ids
taxon_name_id = taxon_name_id, taxon_name_relationship_id = taxon_name_relationship_id,
token = token, project_token = project_token, page = page, per = per))

res <- tw_GET(api_base_url(), "/otus", query = args, ...)
res <- tw_GET(api_base_url(), "/otus", query = args, csv = csv, ...)
return(res)
}

Expand Down
4 changes: 2 additions & 2 deletions R/tw_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
#' tw_projects()
#' }
tw_projects <- function() {
res <- tw_GET(api_base_url())
return(res)
res <- tw_GET(api_base_url(), csv = FALSE)
return(res$open_projects)
}
9 changes: 5 additions & 4 deletions R/tw_taxon_name_classifications.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#' }
tw_taxon_name_classifications <- function(taxon_name_id = NULL,
taxon_name_classification_type = NULL, taxon_name_classification_set = NULL,
token = NULL, project_token = NULL, page = 0, per = 50, ...) {
csv = FALSE, token = NULL, project_token = NULL, page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
assert(per, c("numeric", "integer"))
Expand All @@ -24,10 +24,11 @@ tw_taxon_name_classifications <- function(taxon_name_id = NULL,
taxon_name_classification_set = taxon_name_classification_set,
token = token, project_token = project_token, page = page, per = per))

res <- tw_GET(api_base_url(), "/taxon_name_classifications", body = args, ...)
return(res)
res <- tw_GET(api_base_url(), "/taxon_name_classifications", query = args, csv = csv, ...)
df <- as_tibble(res)
return(df)
}

# TODO: tnc needs a csv endpoint?

#' Taxon Name Classifications
#'
Expand Down
10 changes: 5 additions & 5 deletions R/tw_taxon_name_relationships.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
tw_taxon_name_relationships <- function(object_taxon_name_id = NULL,
subject_taxon_name_id = NULL, taxon_name_id = NULL,
taxon_name_relationship_set = NULL, taxon_name_relationship_type = NULL,
token = NULL, project_token = NULL, page = 0, per = 50, ...) {
csv = FALSE, token = NULL, project_token = NULL, page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
assert(per, c("numeric", "integer"))
Expand All @@ -28,10 +28,10 @@ tw_taxon_name_relationships <- function(object_taxon_name_id = NULL,
taxon_name_relationship_type = taxon_name_relationship_type,
token = token, project_token = project_token, page = page, per = per))

res <- tw_GET(api_base_url(), "/taxon_name_relationships", body = args, ...)
return(res)
}

res <- tw_GET(api_base_url(), "/taxon_name_relationships", query = args, csv = csv, ...)
df <- as_tibble(res)
return(df)
} # TODO: tnr needs a csv endpoint?

#' Taxon Name Relationships
#'
Expand Down
7 changes: 4 additions & 3 deletions R/tw_taxon_names.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tw_taxon_names <- function(ancestors = NULL, author = NULL,
tags = NULL, taxon_name_author_id_or = NULL, taxon_name_id = NULL,
taxon_name_relationship = NULL, taxon_name_relationship_type = NULL,
taxon_name_type = NULL, type_metadata = NULL, validity = NULL, year = NULL,
year_end = NULL, year_start = NULL, token = NULL, project_token = NULL,
year_end = NULL, year_start = NULL, csv = FALSE, token = NULL, project_token = NULL,
page = 0, per = 50, ...) {

assert(page, c("numeric", "integer"))
Expand Down Expand Up @@ -98,8 +98,9 @@ tw_taxon_names <- function(ancestors = NULL, author = NULL,
year = year, year_end = year_end, year_start = year_start,
token = token, project_token = project_token, page = page, per = per))

res <- tw_GET(api_base_url(), "/taxon_names", query = args, ...)
return(res)
res <- tw_GET(api_base_url(), "/taxon_names", query = args, csv = csv, ...)
df <- as_tibble(res)
return(df)
}


Expand Down
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ tol <- function(x) if (is.character(x)) tolower(x) else x
#' @keywords internal
#' @return boolean. `TRUE` if http request succeeds, `FALSE` if not
tw_up <- function() {
z <- tryCatch(tw_GET(api_base_url(), "/ping")$pong,
z <- tryCatch(tw_GET(api_base_url(), "/ping", csv=FALSE),
error = function(e) e)
if (inherits(z, "error")) FALSE else z
if (inherits(z, "error")) FALSE else TRUE
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ X-schema.org-keywords: biology, science, API, web, api-client,
taxonomy, species
X-schema.org-isPartOf: https://ropensci.org
NeedsCompilation: no
Packaged: 2024-02-14 23:30:23 UTC; gdo
Packaged: 2024-02-15 20:03:57 UTC; gdo
Author: Geoff Ower [aut, cre] (<https://orcid.org/0000-0002-9770-2345>),
Species File Group [fnd] (https://speciesfilegroup.org/)
Maintainer: Geoff Ower <gdower@illinois.edu>
2 changes: 2 additions & 0 deletions check/rtaxonworks.Rcheck/00_pkg_src/rtaxonworks/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export(tw_ba)
export(tw_biological_associations)
export(tw_co)
export(tw_collection_objects)
export(tw_otu)
export(tw_otus)
export(tw_projects)
Expand Down
8 changes: 6 additions & 2 deletions check/rtaxonworks.Rcheck/00_pkg_src/rtaxonworks/R/http.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
api_base_url <- function() {
if (!exists("TW_API_URL", envir = globalenv())) {
assign("TW_API_URL", "https://sfg.taxonworks.org/api/v1", envir = globalenv())
return("https://sfg.taxonworks.org/api/v1")
}
get("TW_API_URL", envir = globalenv())
}
Expand Down Expand Up @@ -61,7 +61,11 @@ tw_ual <- list(`User-Agent` = tw_ua(ongha), `X-USER-AGENT` = tw_ua(ongha))
#' @return a list of JSON results
tw_GET <- function(url, path = NULL, query = list(), headers = list(), ...) {

query <- add_token_params(query)
browser()
# if token and project_token are null then add them to the query
if (is.null(query$token) && is.null(query$project_token)) {
query <- add_token_params(query)
}
url_params_string <- serialize(query)
url <- paste0(api_base_url(), path, url_params_string)

Expand Down
Loading

0 comments on commit 59b7ca7

Please # to comment.