Skip to content

Commit

Permalink
all sites function improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mburu committed Dec 6, 2023
1 parent d8abfbe commit 642e6ba
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,modify_url)
importFrom(httr,status_code)
importFrom(janitor,adorn_totals)
importFrom(janitor,make_clean_names)
Expand Down
31 changes: 28 additions & 3 deletions R/get_all_sites.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
#'
#' @param auth_response Authentication response object obtained from the OpenSpecimen API.
#'
#' @return A data table containing the details of all sites, or NULL if an error occurs.
#' @param startAt Starting row of the result (optional).
#' @param maxResults Maximum number of records to fetch (optional). By default, it will be 100.
#' @param name Result contains all sites which have a name containing the given value (optional).
#' @param exactMatch If TRUE, considers the name parameter as an exact match with the site name (optional).
#' @param institute Result contains all sites which belong to the given institute (optional).
#' @importFrom httr GET add_headers modify_url
#' @return A data frame containing the details of all sites, or NULL if an error occurs.
#'
#'
#' @seealso \code{\link{auth_os}}, \code{\link{parse_os_response}}, \code{\link{parse_all_sites}}
#' @note [see api docs for more details:](https://openspecimen.atlassian.net/wiki/spaces/CAT/pages/1115685/Get+All+Sites)
#'
#' @export
#' @examples
#' #get_all_sites()
get_all_sites <- function(auth_response) {
get_all_sites <- function(auth_response, startAt = NULL, maxResults = 500, name = NULL, exactMatch = FALSE, institute = NULL) {

# URL to get all sites
# For more details, refer to: https://openspecimen.atlassian.net/wiki/spaces/CAT/pages/1115685/Get+All+Sites
Expand All @@ -27,6 +34,23 @@ get_all_sites <- function(auth_response) {
"X-OS-API-TOKEN" = auth_response$auth_response$token
)

# Construct query parameters based on provided arguments
query_params <- list(
startAt = startAt,
maxResults = maxResults,
name = name,
exactMatch = exactMatch,
institute = institute
)

# Remove NULL parameters
query_params <- query_params[!sapply(query_params, is.null)]

# Append query parameters to the URL
if (length(query_params) > 0) {
url <- modify_url(url, query = query_params)
}

# Make the GET request for the query
response <- GET(url = url, config = headers, encode = "json")

Expand All @@ -43,3 +67,4 @@ get_all_sites <- function(auth_response) {




31 changes: 28 additions & 3 deletions dev/OpenSpecimenAPI.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1211,12 +1211,19 @@ test_that("get_bulk_order_items works", {
#'
#' @param auth_response Authentication response object obtained from the OpenSpecimen API.
#'
#' @return A data table containing the details of all sites, or NULL if an error occurs.
#' @param startAt Starting row of the result (optional).
#' @param maxResults Maximum number of records to fetch (optional). By default, it will be 100.
#' @param name Result contains all sites which have a name containing the given value (optional).
#' @param exactMatch If TRUE, considers the name parameter as an exact match with the site name (optional).
#' @param institute Result contains all sites which belong to the given institute (optional).
#' @importFrom httr GET add_headers modify_url
#' @return A data frame containing the details of all sites, or NULL if an error occurs.
#'
#'
#' @seealso \code{\link{auth_os}}, \code{\link{parse_os_response}}, \code{\link{parse_all_sites}}
#' @note [see api docs for more details:](https://openspecimen.atlassian.net/wiki/spaces/CAT/pages/1115685/Get+All+Sites)
#'
#' @export
get_all_sites <- function(auth_response) {
get_all_sites <- function(auth_response, startAt = NULL, maxResults = 500, name = NULL, exactMatch = FALSE, institute = NULL) {
# URL to get all sites
# For more details, refer to: https://openspecimen.atlassian.net/wiki/spaces/CAT/pages/1115685/Get+All+Sites
Expand All @@ -1228,6 +1235,23 @@ get_all_sites <- function(auth_response) {
"X-OS-API-TOKEN" = auth_response$auth_response$token
)
# Construct query parameters based on provided arguments
query_params <- list(
startAt = startAt,
maxResults = maxResults,
name = name,
exactMatch = exactMatch,
institute = institute
)
# Remove NULL parameters
query_params <- query_params[!sapply(query_params, is.null)]
# Append query parameters to the URL
if (length(query_params) > 0) {
url <- modify_url(url, query = query_params)
}
# Make the GET request for the query
response <- GET(url = url, config = headers, encode = "json")
Expand All @@ -1244,6 +1268,7 @@ get_all_sites <- function(auth_response) {
```


Expand Down
24 changes: 19 additions & 5 deletions man/get_all_sites.Rd

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

0 comments on commit 642e6ba

Please sign in to comment.