[R] allow oauth scopes customization, remove unused import (#13179)

* add oauth scopes, remove unused import

* fix null check
This commit is contained in:
William Cheng 2022-08-14 22:50:36 +08:00 committed by GitHub
parent 1f42b09b33
commit 9e20b01b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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