From cf085966566141f2fd7556b401e5e828637f47a8 Mon Sep 17 00:00:00 2001 From: Ramanth Date: Tue, 14 Apr 2020 11:59:12 +0530 Subject: [PATCH 1/2] feat(r): retry configuration for status codes --- .../src/main/resources/r/api_client.mustache | 43 ++++++++++++++++++- samples/client/petstore/R/R/api_client.R | 43 ++++++++++++++++++- 2 files changed, 84 insertions(+), 2 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 22fd994ab642..ed090f3d52dd 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -24,6 +24,8 @@ #' @field apiKeys #' @field accessToken #' @field timeout Default timeout in seconds +#' @field retryStatusCodes vector of status codes to retry +#' @field maxRetryAttemps maximum number of retries for the status codes #' @importFrom httr add_headers accept timeout content {{#useRlangExceptionHandling}} #' @importFrom rlang abort @@ -48,8 +50,12 @@ ApiClient <- R6::R6Class( accessToken = NULL, # Time Out (seconds) timeout = NULL, + # Vector of status codes to retry + retryStatusCodes=NULL, + # Maximum number of retry attempts for the retry status codes + maxRetryAttempts = NULL, # constructor - initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){ + initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL, retryStatusCodes=NULL, maxRetryAttempts=NULL){ if (!is.null(basePath)) { self$basePath <- basePath } @@ -83,8 +89,43 @@ ApiClient <- R6::R6Class( if (!is.null(timeout)) { self$timeout <- timeout } + + if (!is.null(retryStatusCodes)) { + self$retryStatusCodes <- retryStatusCodes + } + + if (!is.null(maxRetryAttempts)) { + self$maxRetryAttempts <- maxRetryAttempts + } }, + CallApi = function(url, method, queryParams, headerParams, body, ...){ + + resp <- self$Execute(url, method, queryParams, headerParams, body, ...) + statusCode <- httr::status_code(resp) + + if(is.null(self$maxRetryAttempts)){ + self$maxRetryAttempts = 3 + } + + if(!is.null(self$retryStatusCodes)){ + + for(i in 1: self$maxRetryAttempts){ + if(statusCode %in% self$retryStatusCodes){ + Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1)) + resp <- self$Execute(url, method, queryParams, headerParams, body, ...) + statusCode <- httr::status_code(resp) + } + else{ + break; + } + } + } + + resp + }, + + Execute = function(url, method, queryParams, headerParams, body, ...){ headers <- httr::add_headers(c(headerParams, self$defaultHeaders)) {{! Adding timeout that can be set at the apiClient object level}} diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index f6b8544aacd5..350416dd3710 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -31,6 +31,8 @@ #' @field apiKeys #' @field accessToken #' @field timeout Default timeout in seconds +#' @field retryStatusCodes vector of status codes to retry +#' @field maxRetryAttemps maximum number of retries for the status codes #' @importFrom httr add_headers accept timeout content #' @export ApiClient <- R6::R6Class( @@ -52,8 +54,12 @@ ApiClient <- R6::R6Class( accessToken = NULL, # Time Out (seconds) timeout = NULL, + # Vector of status codes to retry + retryStatusCodes=NULL, + # Maximum number of retry attempts for the retry status codes + maxRetryAttempts = NULL, # constructor - initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){ + initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL, retryStatusCodes=NULL, maxRetryAttempts=NULL){ if (!is.null(basePath)) { self$basePath <- basePath } @@ -87,8 +93,43 @@ ApiClient <- R6::R6Class( if (!is.null(timeout)) { self$timeout <- timeout } + + if (!is.null(retryStatusCodes)) { + self$retryStatusCodes <- retryStatusCodes + } + + if (!is.null(maxRetryAttempts)) { + self$maxRetryAttempts <- maxRetryAttempts + } }, + CallApi = function(url, method, queryParams, headerParams, body, ...){ + + resp <- self$Execute(url, method, queryParams, headerParams, body, ...) + statusCode <- httr::status_code(resp) + + if(is.null(self$maxRetryAttempts)){ + self$maxRetryAttempts = 3 + } + + if(!is.null(self$retryStatusCodes)){ + + for(i in 1: self$maxRetryAttempts){ + if(statusCode %in% self$retryStatusCodes){ + Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1)) + resp <- self$Execute(url, method, queryParams, headerParams, body, ...) + statusCode <- httr::status_code(resp) + } + else{ + break; + } + } + } + + resp + }, + + Execute = function(url, method, queryParams, headerParams, body, ...){ headers <- httr::add_headers(c(headerParams, self$defaultHeaders)) httpTimeout <- NULL From 086c1409632898d737fd6b725a80b54566ce2b3b Mon Sep 17 00:00:00 2001 From: Veda Bhaskara Ramanth Addala Date: Thu, 23 Apr 2020 07:33:55 +0530 Subject: [PATCH 2/2] fix(r) : fixing review comments --- .../src/main/resources/r/api_client.mustache | 21 +++++++++---------- samples/client/petstore/R/R/api_client.R | 21 +++++++++---------- 2 files changed, 20 insertions(+), 22 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 ed090f3d52dd..48a7d54a9ae0 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -25,7 +25,7 @@ #' @field accessToken #' @field timeout Default timeout in seconds #' @field retryStatusCodes vector of status codes to retry -#' @field maxRetryAttemps maximum number of retries for the status codes +#' @field maxRetryAttempts maximum number of retries for the status codes #' @importFrom httr add_headers accept timeout content {{#useRlangExceptionHandling}} #' @importFrom rlang abort @@ -93,7 +93,7 @@ ApiClient <- R6::R6Class( if (!is.null(retryStatusCodes)) { self$retryStatusCodes <- retryStatusCodes } - + if (!is.null(maxRetryAttempts)) { self$maxRetryAttempts <- maxRetryAttempts } @@ -104,25 +104,24 @@ ApiClient <- R6::R6Class( resp <- self$Execute(url, method, queryParams, headerParams, body, ...) statusCode <- httr::status_code(resp) - if(is.null(self$maxRetryAttempts)){ + if (is.null(self$maxRetryAttempts)) { self$maxRetryAttempts = 3 } - if(!is.null(self$retryStatusCodes)){ + if (!is.null(self$retryStatusCodes)) { - for(i in 1: self$maxRetryAttempts){ - if(statusCode %in% self$retryStatusCodes){ + for (i in 1 : self$maxRetryAttempts) { + if (statusCode %in% self$retryStatusCodes) { Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1)) resp <- self$Execute(url, method, queryParams, headerParams, body, ...) statusCode <- httr::status_code(resp) - } - else{ + } else { break; } - } - } + } + } - resp + resp }, Execute = function(url, method, queryParams, headerParams, body, ...){ diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index 350416dd3710..13e7a41efa7e 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -32,7 +32,7 @@ #' @field accessToken #' @field timeout Default timeout in seconds #' @field retryStatusCodes vector of status codes to retry -#' @field maxRetryAttemps maximum number of retries for the status codes +#' @field maxRetryAttempts maximum number of retries for the status codes #' @importFrom httr add_headers accept timeout content #' @export ApiClient <- R6::R6Class( @@ -97,7 +97,7 @@ ApiClient <- R6::R6Class( if (!is.null(retryStatusCodes)) { self$retryStatusCodes <- retryStatusCodes } - + if (!is.null(maxRetryAttempts)) { self$maxRetryAttempts <- maxRetryAttempts } @@ -108,25 +108,24 @@ ApiClient <- R6::R6Class( resp <- self$Execute(url, method, queryParams, headerParams, body, ...) statusCode <- httr::status_code(resp) - if(is.null(self$maxRetryAttempts)){ + if (is.null(self$maxRetryAttempts)) { self$maxRetryAttempts = 3 } - if(!is.null(self$retryStatusCodes)){ + if (!is.null(self$retryStatusCodes)) { - for(i in 1: self$maxRetryAttempts){ - if(statusCode %in% self$retryStatusCodes){ + for (i in 1 : self$maxRetryAttempts) { + if (statusCode %in% self$retryStatusCodes) { Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1)) resp <- self$Execute(url, method, queryParams, headerParams, body, ...) statusCode <- httr::status_code(resp) - } - else{ + } else { break; } - } - } + } + } - resp + resp }, Execute = function(url, method, queryParams, headerParams, body, ...){