diff --git a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache index b97fa13c584..a4b7d9f9fe5 100644 --- a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache @@ -32,6 +32,7 @@ #' @field oauth_authorization_url Authoriziation URL #' @field oauth_token_url Token URL #' @field oauth_pkce Boolean flag to enable PKCE +#' @field oauth_scopes OAuth scopes {{/isOAuth}} {{/authMethods}} {{/hasOAuthMethods}} @@ -39,7 +40,6 @@ #' @field timeout Default timeout in seconds #' @field retry_status_codes vector of status codes to retry #' @field max_retry_attempts maximum number of retries for the status codes -#' @importFrom httr add_headers accept timeout content {{#useRlangExceptionHandling}} #' @importFrom rlang abort {{/useRlangExceptionHandling}} @@ -79,6 +79,8 @@ ApiClient <- R6::R6Class( oauth_token_url = "{{tokenUrl}}", # Enable PKCE? oauth_pkce = TRUE, + # OAuth scopes + oauth_scopes = NULL, {{/isOAuth}} {{/authMethods}} {{/hasOAuthMethods}} @@ -283,7 +285,17 @@ ApiClient <- R6::R6Class( token_url = self$oauth_token_url, name = "{{packageName}}-oauth" ) - req <- req %>% req_oauth_auth_code(client, scope = oauth_scopes, + + req_oauth_scopes <- NULL + if (!is.null(self$oauth_scopes)) { + # use oauth scopes provided by the user + req_oauth_scopes <- self$oauth_scopes + } else { + # use oauth scopes defined in openapi spec + req_oauth_scopes <- oauth_scopes + } + + req <- req %>% req_oauth_auth_code(client, scope = req_oauth_scopes, pkce = self$oauth_pkce, auth_url = self$oauth_authoriziation_url) } diff --git a/samples/client/petstore/R-httr2/R/api_client.R b/samples/client/petstore/R-httr2/R/api_client.R index f0c4aab978e..1da085b65f8 100644 --- a/samples/client/petstore/R-httr2/R/api_client.R +++ b/samples/client/petstore/R-httr2/R/api_client.R @@ -36,11 +36,11 @@ #' @field oauth_authorization_url Authoriziation URL #' @field oauth_token_url Token URL #' @field oauth_pkce Boolean flag to enable PKCE +#' @field oauth_scopes OAuth scopes #' @field bearer_token Bearer token #' @field timeout Default timeout in seconds #' @field retry_status_codes vector of status codes to retry #' @field max_retry_attempts maximum number of retries for the status codes -#' @importFrom httr add_headers accept timeout content #' @importFrom rlang abort #' @export ApiClient <- R6::R6Class( @@ -75,6 +75,8 @@ ApiClient <- R6::R6Class( oauth_token_url = "", # Enable PKCE? oauth_pkce = TRUE, + # OAuth scopes + oauth_scopes = NULL, # Bearer token bearer_token = NULL, # Time Out (seconds) @@ -274,7 +276,17 @@ ApiClient <- R6::R6Class( token_url = self$oauth_token_url, name = "petstore-oauth" ) - req <- req %>% req_oauth_auth_code(client, scope = oauth_scopes, + + req_oauth_scopes <- NULL + if (!is.null(self$oauth_scopes)) { + # use oauth scopes provided by the user + req_oauth_scopes <- self$oauth_scopes + } else { + # use oauth scopes defined in openapi spec + req_oauth_scopes <- oauth_scopes + } + + req <- req %>% req_oauth_auth_code(client, scope = req_oauth_scopes, pkce = self$oauth_pkce, auth_url = self$oauth_authoriziation_url) } diff --git a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R index ad50729bd8d..2e9aa2f8842 100644 --- a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R @@ -114,6 +114,7 @@ test_that("update_pet_with_form", { ## update pet with form pet_api$api_client$oauth_client_id <- "client_id_aaa" pet_api$api_client$oauth_secret <- "secrete_bbb" + pet_api$api_client$oauth_scopes <- "write:pets read:pets" update_result <- pet_api$update_pet_with_form(update_pet_id, name = "pet2", status = "sold") # get pet