Skip to content

Commit

Permalink
refactor: change API path building
Browse files Browse the repository at this point in the history
Formerly all APIs has "/api" as base path but hydrometrie/v2 has "/" and endpoints has "api/v2/endpoint" paths

This change takes care of the base path which can be different from one API to another now.

Refs #45
  • Loading branch information
DDorch committed Dec 1, 2024
1 parent b91a7ed commit 2a027c9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 1 addition & 3 deletions R/doApiQuery.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ doApiQuery <- function(api,
if (!missing(...)) params <- p_ellipsis
if (missing(params)) params <- list()

availableParams <- list_params(api, endpoint)

query <-
file.path(.cfg$api_url, .cfg$apis[[api]]$path, .cfg$apis[[api]]$endpoints[[endpoint]]$path)
file.path(.cfg$api_url, .cfg$apis[[api]]$endpoints[[endpoint]]$path)
for (paramName in names(params)) {
if (!paramName %in% .cfg$apis[[api]]$endpoints[[endpoint]]$fields) {
stop(
Expand Down
7 changes: 4 additions & 3 deletions data-raw/configuration.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ library(httr)

defineApis <- function(api, cfg) {
message("Processing API: ", api$path)
query <- file.path(cfg$api_url, api$path, cfg$api_docs)
query <- file.path(cfg$api_url, cfg$api_path, api$path, cfg$api_docs)
d <- content(GET(query), as = "parsed")
sapply(c("swagger", "openapi"), function(fmt) {
if (!is.null(d[[fmt]])) {
message(fmt, " ", d[[fmt]])
}
})
path_endpoints <- names(d$paths)
path_endpoints <- path_endpoints[!grepl(".csv", path_endpoints, fixed = TRUE)]
path_endpoints <- path_endpoints[!grepl("\\.(csv|xml)$", path_endpoints)]
endpoints <- basename(path_endpoints)
fields <- lapply(path_endpoints, function(ep) {
if(!is.null(d$paths[[ep]]$get$parameters)) {
sapply(d$paths[[ep]]$get$parameters, "[[", "name")
}
})
l <- lapply(seq_along(endpoints), function(i) {
path <- gsub(api$path, "", path_endpoints[i])
path <- file.path(d$basePath, path_endpoints[i])
path <- gsub("^/+", "", path)
path <- gsub("//", "/", path)
list(path = path,
fields = fields[[i]])
})
Expand Down

0 comments on commit 2a027c9

Please sign in to comment.