add http bearer auth support to R client (#12974)

This commit is contained in:
William Cheng 2022-07-22 09:44:05 +08:00 committed by GitHub
parent d1c2230709
commit f26985c239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 16 deletions

View File

@ -97,11 +97,17 @@
{{#authMethods}} {{#authMethods}}
#' #'
{{#isBasic}} {{#isBasic}}
{{#isBasicBasic}}
#' #Configure HTTP basic authorization: {{{name}}} #' #Configure HTTP basic authorization: {{{name}}}
#' # provide your username in the user-serial format #' # provide the username
#' api.instance$api_client$username <- '<user-serial>'; #' api.instance$api_client$username <- 'TODO_YOUR_USERNAME';
#' # provide your api key generated using the developer portal #' # provide the password
#' api.instance$api_client$password <- '<api_key>'; #' api.instance$api_client$password <- 'TODO_YOUR_PASSWORD';
{{/isBasicBasic}}
{{#isBasicBearer}}
#' #Configure HTTP bearer authorization: {{{name}}}
#' api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN';
{{/isBasicBearer}}
{{/isBasic}} {{/isBasic}}
{{#isApiKey}} {{#isApiKey}}
#' #Configure API key authorization: {{{name}}} #' #Configure API key authorization: {{{name}}}
@ -302,6 +308,9 @@
# HTTP basic auth # HTTP basic auth
header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":")))) header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":"))))
{{/isBasicBasic}} {{/isBasicBasic}}
{{#isBasicBearer}}
header_params["Authorization"] <- paste("Bearer", self$api_client$bearer_token, sep = " ")
{{/isBasicBearer}}
{{/isBasic}} {{/isBasic}}
{{#isApiKey}} {{#isApiKey}}
# API key authentication # API key authentication

View File

@ -22,6 +22,7 @@
#' @field password Password for HTTP basic authentication #' @field password Password for HTTP basic authentication
#' @field api_keys API keys #' @field api_keys API keys
#' @field access_token Access token #' @field access_token Access token
#' @field bearer_token Bearer token
#' @field timeout Default timeout in seconds #' @field timeout Default timeout in seconds
#' @field retry_status_codes vector of status codes to retry #' @field retry_status_codes vector of status codes to retry
#' @field max_retry_attempts maximum number of retries for the status codes #' @field max_retry_attempts maximum number of retries for the status codes
@ -47,6 +48,8 @@ ApiClient <- R6::R6Class(
api_keys = NULL, api_keys = NULL,
# Access token # Access token
access_token = NULL, access_token = NULL,
# Bearer token
bearer_token = NULL,
# Time Out (seconds) # Time Out (seconds)
timeout = NULL, timeout = NULL,
# Vector of status codes to retry # Vector of status codes to retry
@ -65,6 +68,7 @@ ApiClient <- R6::R6Class(
#' @param password Password. #' @param password Password.
#' @param api_keys API keys. #' @param api_keys API keys.
#' @param access_token Access token. #' @param access_token Access token.
#' @param bearer_token Bearer token.
#' @param timeout Timeout. #' @param timeout Timeout.
#' @param retry_status_codes Status codes for retry. #' @param retry_status_codes Status codes for retry.
#' @param max_retry_attempts Maxmium number of retry. #' @param max_retry_attempts Maxmium number of retry.
@ -72,7 +76,7 @@ ApiClient <- R6::R6Class(
initialize = function(base_path = NULL, user_agent = NULL, initialize = function(base_path = NULL, user_agent = NULL,
default_headers = NULL, default_headers = NULL,
username = NULL, password = NULL, api_keys = NULL, username = NULL, password = NULL, api_keys = NULL,
access_token = NULL, timeout = NULL, access_token = NULL, bearer_token = NULL, timeout = NULL,
retry_status_codes = NULL, max_retry_attempts = NULL) { retry_status_codes = NULL, max_retry_attempts = NULL) {
if (!is.null(base_path)) { if (!is.null(base_path)) {
self$base_path <- base_path self$base_path <- base_path
@ -94,6 +98,10 @@ ApiClient <- R6::R6Class(
self$access_token <- access_token self$access_token <- access_token
} }
if (!is.null(bearer_token)) {
self$bearer_token <- bearer_token
}
if (!is.null(api_keys)) { if (!is.null(api_keys)) {
self$api_keys <- api_keys self$api_keys <- api_keys
} else { } else {

View File

@ -32,9 +32,15 @@ api_instance <- {{{classname}}}$new()
{{#hasAuthMethods}} {{#hasAuthMethods}}
{{#authMethods}} {{#authMethods}}
{{#isBasic}} {{#isBasic}}
{{#isBasicBasic}}
# Configure HTTP basic authorization: {{{name}}} # Configure HTTP basic authorization: {{{name}}}
api_instance$api_client$username <- 'TODO_YOUR_USERNAME'; api_instance$api_client$username <- 'TODO_YOUR_USERNAME';
api_instance$api_client$password <- 'TODO_YOUR_PASSWORD'; api_instance$api_client$password <- 'TODO_YOUR_PASSWORD';
{{/isBasicBasic}}
{{#isBasicBearer}}
# Configure HTTP bearer authorization: {{{name}}}
api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN';
{{/isBasicBearer}}
{{/isBasic}} {{/isBasic}}
{{#isApiKey}} {{#isApiKey}}
# Configure API key authorization: {{{name}}} # Configure API key authorization: {{{name}}}

View File

@ -184,7 +184,7 @@ paths:
'404': '404':
description: Pet not found description: Pet not found
security: security:
- api_key: [] - BearerToken: []
'/pet/{petId}?streaming': '/pet/{petId}?streaming':
get: get:
tags: tags:
@ -671,6 +671,9 @@ components:
description: Pet object that needs to be added to the store description: Pet object that needs to be added to the store
required: true required: true
securitySchemes: securitySchemes:
BearerToken:
type : http
scheme : bearer
petstore_auth: petstore_auth:
type: oauth2 type: oauth2
flows: flows:

View File

@ -29,6 +29,7 @@
#' @field password Password for HTTP basic authentication #' @field password Password for HTTP basic authentication
#' @field api_keys API keys #' @field api_keys API keys
#' @field access_token Access token #' @field access_token Access token
#' @field bearer_token Bearer token
#' @field timeout Default timeout in seconds #' @field timeout Default timeout in seconds
#' @field retry_status_codes vector of status codes to retry #' @field retry_status_codes vector of status codes to retry
#' @field max_retry_attempts maximum number of retries for the status codes #' @field max_retry_attempts maximum number of retries for the status codes
@ -52,6 +53,8 @@ ApiClient <- R6::R6Class(
api_keys = NULL, api_keys = NULL,
# Access token # Access token
access_token = NULL, access_token = NULL,
# Bearer token
bearer_token = NULL,
# Time Out (seconds) # Time Out (seconds)
timeout = NULL, timeout = NULL,
# Vector of status codes to retry # Vector of status codes to retry
@ -70,6 +73,7 @@ ApiClient <- R6::R6Class(
#' @param password Password. #' @param password Password.
#' @param api_keys API keys. #' @param api_keys API keys.
#' @param access_token Access token. #' @param access_token Access token.
#' @param bearer_token Bearer token.
#' @param timeout Timeout. #' @param timeout Timeout.
#' @param retry_status_codes Status codes for retry. #' @param retry_status_codes Status codes for retry.
#' @param max_retry_attempts Maxmium number of retry. #' @param max_retry_attempts Maxmium number of retry.
@ -77,7 +81,7 @@ ApiClient <- R6::R6Class(
initialize = function(base_path = NULL, user_agent = NULL, initialize = function(base_path = NULL, user_agent = NULL,
default_headers = NULL, default_headers = NULL,
username = NULL, password = NULL, api_keys = NULL, username = NULL, password = NULL, api_keys = NULL,
access_token = NULL, timeout = NULL, access_token = NULL, bearer_token = NULL, timeout = NULL,
retry_status_codes = NULL, max_retry_attempts = NULL) { retry_status_codes = NULL, max_retry_attempts = NULL) {
if (!is.null(base_path)) { if (!is.null(base_path)) {
self$base_path <- base_path self$base_path <- base_path
@ -99,6 +103,10 @@ ApiClient <- R6::R6Class(
self$access_token <- access_token self$access_token <- access_token
} }
if (!is.null(bearer_token)) {
self$bearer_token <- bearer_token
}
if (!is.null(api_keys)) { if (!is.null(api_keys)) {
self$api_keys <- api_keys self$api_keys <- api_keys
} else { } else {

View File

@ -373,8 +373,8 @@
#' #Find pet by ID #' #Find pet by ID
#' api.instance <- PetApi$new() #' api.instance <- PetApi$new()
#' #'
#' #Configure API key authorization: api_key #' #Configure HTTP bearer authorization: BearerToken
#' api.instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; #' api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN';
#' #'
#'result <- tryCatch( #'result <- tryCatch(
#' api.instance$GetPetById(var.pet_id), #' api.instance$GetPetById(var.pet_id),
@ -969,10 +969,7 @@ PetApi <- R6::R6Class(
url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), url_path) url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet_id`), reserved = TRUE), url_path)
} }
# API key authentication header_params["Authorization"] <- paste("Bearer", self$api_client$bearer_token, sep = " ")
if ("api_key" %in% names(self$api_client$api_keys) && nchar(self$api_client$api_keys["api_key"]) > 0) {
header_params["api_key"] <- paste(unlist(self$api_client$api_keys["api_key"]), collapse = "")
}
resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path), resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
method = "GET", method = "GET",

View File

@ -111,6 +111,10 @@ Class | Method | HTTP request | Description
## Documentation for Authorization ## Documentation for Authorization
### BearerToken
- **Type**: HTTP basic authentication
### api_key ### api_key
- **Type**: API key - **Type**: API key

View File

@ -271,8 +271,8 @@ var_pet_id <- 56 # integer | ID of pet to return
#Find pet by ID #Find pet by ID
api_instance <- PetApi$new() api_instance <- PetApi$new()
# Configure API key authorization: api_key # Configure HTTP bearer authorization: BearerToken
api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY'; api.instance$api_client$bearer_token <- 'TODO_YOUR_BEARER_TOKEN';
result <- tryCatch( result <- tryCatch(
# to save the result into a file, simply add the optional `data_file` parameter, e.g. # to save the result into a file, simply add the optional `data_file` parameter, e.g.
# api_instance$GetPetById(var_pet_id, data_file = "result.txt"), # api_instance$GetPetById(var_pet_id, data_file = "result.txt"),
@ -304,7 +304,7 @@ Name | Type | Description | Notes
### Authorization ### Authorization
[api_key](../README.md#api_key) [BearerToken](../README.md#BearerToken)
### HTTP request headers ### HTTP request headers