From 19eeebf083f7c263c9a332ea5e9e5d95f95a6e0d Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 15 Aug 2022 02:11:45 +0800 Subject: [PATCH] add refresh token setting, use oauth_client_secret instead --- .../src/main/resources/r/api_client.mustache | 4 ++-- .../resources/r/libraries/httr2/api_client.mustache | 12 ++++++++---- samples/client/petstore/R-httr2/R/api_client.R | 12 ++++++++---- .../petstore/R-httr2/tests/testthat/test_petstore.R | 2 +- samples/client/petstore/R/R/api_client.R | 4 ++-- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/api_client.mustache b/modules/openapi-generator/src/main/resources/r/api_client.mustache index 2e6e6b839d1..e641cd2637f 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -24,7 +24,7 @@ {{#hasOAuthMethods}} #' @field access_token Access token #' @field oauth_client_id OAuth client ID -#' @field oauth_secret OAuth secret +#' @field oauth_client_secret OAuth secret #' @field oauth_refresh_token OAuth refresh token {{#authMethods}} {{#isOAuth}} @@ -65,7 +65,7 @@ ApiClient <- R6::R6Class( # OAuth2 client ID oauth_client_id = NULL, # OAuth2 secret - oauth_secret = NULL, + oauth_client_secret = NULL, # OAuth2 refresh token oauth_refresh_token = NULL, # OAuth2 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 a4b7d9f9fe5..86bb84d4be3 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 @@ -24,7 +24,7 @@ {{#hasOAuthMethods}} #' @field access_token Access token #' @field oauth_client_id OAuth client ID -#' @field oauth_secret OAuth secret +#' @field oauth_client_secret OAuth secret #' @field oauth_refresh_token OAuth refresh token {{#authMethods}} {{#isOAuth}} @@ -33,6 +33,7 @@ #' @field oauth_token_url Token URL #' @field oauth_pkce Boolean flag to enable PKCE #' @field oauth_scopes OAuth scopes +#' @field oauth_scopes OAuth scopes {{/isOAuth}} {{/authMethods}} {{/hasOAuthMethods}} @@ -65,9 +66,11 @@ ApiClient <- R6::R6Class( # OAuth2 client ID oauth_client_id = NULL, # OAuth2 secret - oauth_secret = NULL, + oauth_client_secret = NULL, # OAuth2 refresh token oauth_refresh_token = NULL, + # OAuth2 auto refresh token + oauth_auto_refresh_token = FALSE, # OAuth2 {{#authMethods}} {{#isOAuth}} @@ -187,6 +190,7 @@ ApiClient <- R6::R6Class( # set the URL req <- request(url) + # make the call via httr, which comes with retry logic resp <- self$Execute(req, method, query_params, header_params, form_params, file_params, accepts, content_types, body, is_oauth = is_oauth, oauth_scopes = oauth_scopes, stream_callback = stream_callback, ...) @@ -278,10 +282,10 @@ ApiClient <- R6::R6Class( {{#hasOAuthMethods}} # use oauth authentication if the endpoint requires it - if (is_oauth && !is.null(self$oauth_client_id) && !is.null(self$oauth_secret)) { + if (is_oauth && !is.null(self$oauth_client_id) && !is.null(self$oauth_client_secret)) { client <- oauth_client( id = self$oauth_client_id, - secret = obfuscated(self$oauth_secret), + secret = obfuscated(self$oauth_client_secret), token_url = self$oauth_token_url, name = "{{packageName}}-oauth" ) diff --git a/samples/client/petstore/R-httr2/R/api_client.R b/samples/client/petstore/R-httr2/R/api_client.R index 1da085b65f8..7c7048f9736 100644 --- a/samples/client/petstore/R-httr2/R/api_client.R +++ b/samples/client/petstore/R-httr2/R/api_client.R @@ -30,13 +30,14 @@ #' @field api_keys API keys #' @field access_token Access token #' @field oauth_client_id OAuth client ID -#' @field oauth_secret OAuth secret +#' @field oauth_client_secret OAuth secret #' @field oauth_refresh_token OAuth refresh token #' @field oauth_flow_type OAuth flow type #' @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 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 @@ -63,9 +64,11 @@ ApiClient <- R6::R6Class( # OAuth2 client ID oauth_client_id = NULL, # OAuth2 secret - oauth_secret = NULL, + oauth_client_secret = NULL, # OAuth2 refresh token oauth_refresh_token = NULL, + # OAuth2 auto refresh token + oauth_auto_refresh_token = FALSE, # OAuth2 # Flow type oauth_flow_type = "implicit", @@ -180,6 +183,7 @@ ApiClient <- R6::R6Class( # set the URL req <- request(url) + # make the call via httr, which comes with retry logic resp <- self$Execute(req, method, query_params, header_params, form_params, file_params, accepts, content_types, body, is_oauth = is_oauth, oauth_scopes = oauth_scopes, stream_callback = stream_callback, ...) @@ -269,10 +273,10 @@ ApiClient <- R6::R6Class( req <- req %>% req_method(method) # use oauth authentication if the endpoint requires it - if (is_oauth && !is.null(self$oauth_client_id) && !is.null(self$oauth_secret)) { + if (is_oauth && !is.null(self$oauth_client_id) && !is.null(self$oauth_client_secret)) { client <- oauth_client( id = self$oauth_client_id, - secret = obfuscated(self$oauth_secret), + secret = obfuscated(self$oauth_client_secret), token_url = self$oauth_token_url, name = "petstore-oauth" ) 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 bd5f6d326b6..6a7789d7c64 100644 --- a/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R +++ b/samples/client/petstore/R-httr2/tests/testthat/test_petstore.R @@ -113,7 +113,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_client_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") diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index 2d32c0f9b20..152b022c3b3 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -30,7 +30,7 @@ #' @field api_keys API keys #' @field access_token Access token #' @field oauth_client_id OAuth client ID -#' @field oauth_secret OAuth secret +#' @field oauth_client_secret OAuth secret #' @field oauth_refresh_token OAuth refresh token #' @field oauth_flow_type OAuth flow type #' @field oauth_authorization_url Authoriziation URL @@ -63,7 +63,7 @@ ApiClient <- R6::R6Class( # OAuth2 client ID oauth_client_id = NULL, # OAuth2 secret - oauth_secret = NULL, + oauth_client_secret = NULL, # OAuth2 refresh token oauth_refresh_token = NULL, # OAuth2