R/buildEndpoint.R

Defines functions .buildEndpoint

#' Build an EPPO API endpoint path.
#'
#' This helper function constructs an endpoint path for retrieving data from the
#' EPPO API. The result must be appended to the EPPO API base URL. It allows for
#' the optional inclusion of a specific code and/or service name, depending on
#' the desired API resource. The function is based on the fact that EPPO API
#' endpoints follow the pattern:
#' `{base path}/{resource identifier}/{service}`.
#'
#' @param basePath `character` (string). The base path, starting with "/" 
#'   (e.g., `"/taxons/taxon"`). Must match the pattern `"^/"`.
#' 
#' @param code `character` (string, optional). The resource identifier (e.g. an
#'   EPPO code or an ISO code). If provided, it will be appended to the base
#'   path.
#' 
#' @param service `character` (string, optional). The desired API service. If
#'   provided, it will be appended to the path after the resource identifier
#'   (if any, otherwise after the base path).
#'
#' @return A string representing the complete endpoint path to be used in an API
#'   request.
#' 
#' @importFrom checkmate assert_string
#' @importFrom glue glue
#'
#' @keywords internal
#' @noRd
#' 
.buildEndpoint <- function(basePath, code = NULL, service = NULL) {

  assert_string(basePath, pattern = "^/")
  if (!is.null(code)) assert_string(code)
  if (!is.null(service)) assert_string(service)
  
  endpointParts_ <- c(basePath, code, service)
  
  endpointPath_ <- paste(endpointParts_, collapse = '/')
  endpointPath_ <- gsub("//", '/', endpointPath_)
  
  return(endpointPath_)
}

Try the eppoFindeR package in your browser

Any scripts or data that you put into this service are public.

eppoFindeR documentation built on April 25, 2026, 1:07 a.m.