Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(r): retry configuration for status codes #5925

Merged
merged 2 commits into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Ramanth marked this conversation as resolved.
Show resolved Hide resolved
#' @importFrom httr add_headers accept timeout content
{{#useRlangExceptionHandling}}
#' @importFrom rlang abort
Expand All @@ -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
}
Expand Down Expand Up @@ -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){
Ramanth marked this conversation as resolved.
Show resolved Hide resolved
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}}
Expand Down
43 changes: 42 additions & 1 deletion samples/client/petstore/R/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
Expand Down Expand Up @@ -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{
Ramanth marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
Ramanth marked this conversation as resolved.
Show resolved Hide resolved

resp
},

Execute = function(url, method, queryParams, headerParams, body, ...){
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))

httpTimeout <- NULL
Expand Down