Skip to content

Commit

Permalink
Add expand_servers() function. (#68)
Browse files Browse the repository at this point in the history
Closes #66.
  • Loading branch information
jonthegeek authored Oct 6, 2023
1 parent f3f4ed3 commit c343ec9
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 17 deletions.
28 changes: 15 additions & 13 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Imports:
S7 (>= 0.1.1),
snakecase,
stbl,
xml2,
yaml
Suggests:
testthat (>= 3.0.0)
Expand All @@ -35,31 +36,32 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Collate:
'properties.R'
'security_requirements.R'
'components-security_scheme_details.R'
'components-security_scheme_collection.R'
'components.R'
'servers-server_variables.R'
'servers-string_replacements.R'
'servers.R'
'info-origin.R'
'info-license.R'
'info-contact.R'
'info.R'
'zz-rapid.R'
'absolute_paths.R'
'as.R'
'components-security_scheme.R'
'properties.R'
'components-security_scheme-api_key.R'
'components-security_scheme-oauth2-scopes.R'
'components-security_scheme-oauth2-flow.R'
'components-security_scheme-oauth2-authorization_code_flow.R'
'components-security_scheme-oauth2-implicit_flow.R'
'components-security_scheme-oauth2-token_flow.R'
'components-security_scheme-oauth2.R'
'components-security_scheme_details.R'
'components-security_scheme_collection.R'
'components.R'
'info-contact.R'
'info-license.R'
'info-origin.R'
'info.R'
'rapid-package.R'
'security_requirements.R'
'servers-server_variables.R'
'servers-string_replacements.R'
'servers.R'
'utils.R'
'validate_in.R'
'validate_lengths.R'
'validate_parallel.R'
'zz-rapid.R'
'zzz.R'
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(as_string_replacements)
export(class_origin)
export(component_collection)
export(contact)
export(expand_servers)
export(info)
export(license)
export(oauth2_authorization_code_flow)
Expand All @@ -49,4 +50,5 @@ importFrom(rlang,"%||%")
importFrom(rlang,check_dots_empty)
importFrom(stbl,stabilize_chr_scalar)
importFrom(stbl,to_chr_scalar)
importFrom(xml2,url_absolute)
importFrom(yaml,read_yaml)
46 changes: 46 additions & 0 deletions R/absolute_paths.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' @include zz-rapid.R
NULL

#' Expand server urls to absolute paths
#'
#' `expand_servers()` uses the `origin` property of a `rapid` object to expand
#' the `servers` `url` property to an absolute path.
#'
#' @inheritParams rlang::args_error_context
#' @inheritParams rlang::args_dots_empty
#' @param x The object to update. Must be a `rapid`.
#'
#' @return A `rapid` object as returned by [rapid()], with absolute server
#' paths.
#' @export
expand_servers <- S7::new_generic("expand_servers", dispatch_args = "x")

S7::method(expand_servers, rapid) <- function(x) {
if (length(x@servers@url)) {
relative_servers <- .is_relative_url(x@servers@url)
origin_url <- x@info@origin@url
if (length(origin_url)) {
base_url <- url_absolute("..", origin_url)
x@servers@url[relative_servers] <- paste0(
base_url,
x@servers@url[relative_servers]
)
x@servers@url[relative_servers] <- gsub(
pattern = "(?<!\\:)//",
replacement = "/",
x = x@servers@url[relative_servers],
perl = TRUE
)
}
}
return(x)
}

S7::method(expand_servers, class_any) <- function(x,
arg = rlang::caller_arg(x),
call = rlang::caller_env()) {
cli::cli_abort(
"{.arg {arg}} {.cls {class(x)}} must be a {.cls rapid}.",
call = call
)
}
4 changes: 2 additions & 2 deletions R/info-origin.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ S7::method(as_origin, class_missing | NULL) <- function(x) {
}

S7::method(as_origin, class_any) <- function(x,
...,
arg = rlang::caller_arg(x)) {
...,
arg = rlang::caller_arg(x)) {
cli::cli_abort(
"Can't coerce {.arg {arg}} {.cls {class(x)}} to {.cls class_origin}."
)
Expand Down
1 change: 1 addition & 0 deletions R/rapid-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @importFrom S7 prop<-
#' @importFrom stbl stabilize_chr_scalar
#' @importFrom stbl to_chr_scalar
#' @importFrom xml2 url_absolute
#' @importFrom yaml read_yaml
## usethis namespace: end
NULL
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@
}
x
}

.is_relative_url <- function(x) {
grepl("^/", x)
}
5 changes: 4 additions & 1 deletion R/zz-rapid.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' @include info.R
#' @include servers.R
#' @include components.R
#' @include security_requirements.R
NULL

#' R API description object
Expand Down Expand Up @@ -121,7 +123,8 @@ S7::method(as_rapid, rapid) <- function(x) {
S7::method(as_rapid, class_list) <- function(x) {
rlang::try_fetch(
{
.as_class(x, rapid)
x <- .as_class(x, rapid)
expand_servers(x)
},
rapid_error_missing_names = function(cnd) {
cli::cli_abort(
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ reference:
contents:
- rapid
- as_rapid
- expand_servers
- title: info class
contents:
- info
Expand Down
21 changes: 21 additions & 0 deletions man/expand_servers.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/zz-rapid.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
.. .. @ format : chr "openapi"
.. .. @ version: chr "3.0"
@ servers : <rapid::servers>
.. @ url : chr "/v1"
.. @ url : chr "https://api.open.fec.gov/v1"
.. @ description: chr(0)
.. @ variables : <rapid::server_variables> list()
@ components: <rapid::component_collection>
Expand Down
Binary file added tests/testthat/fixtures/fec_guru_rapid.rds
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/testthat/test-absolute_paths.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("expand_servers can expand servers", {
# test_url <- "https://api.apis.guru/v2/specs/fec.gov/1.0/openapi.yaml"
# input_rapid <- suppressWarnings(as_rapid(url(test_url)))
# input_rapid@servers <- servers(
# url = c(input_rapid@servers@url, "https://example.com")
# )
# saveRDS(input_rapid, test_path("fixtures", "fec_guru_rapid.rds"))
input_rapid <- readRDS(test_path("fixtures", "fec_guru_rapid.rds"))
output_rapid <- expand_servers(input_rapid)
expect_identical(
output_rapid@servers@url,
c("https://api.open.fec.gov/v1", "https://example.com")
)
})

0 comments on commit c343ec9

Please sign in to comment.