Skip to content

Commit c39aef2

Browse files
authored
feat(r): retry configuration for status codes (#5925)
* feat(r): retry configuration for status codes * fix(r) : fixing review comments
1 parent f951470 commit c39aef2

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

modules/openapi-generator/src/main/resources/r/api_client.mustache

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#' @field apiKeys
2525
#' @field accessToken
2626
#' @field timeout Default timeout in seconds
27+
#' @field retryStatusCodes vector of status codes to retry
28+
#' @field maxRetryAttempts maximum number of retries for the status codes
2729
#' @importFrom httr add_headers accept timeout content
2830
{{#useRlangExceptionHandling}}
2931
#' @importFrom rlang abort
@@ -48,8 +50,12 @@ ApiClient <- R6::R6Class(
4850
accessToken = NULL,
4951
# Time Out (seconds)
5052
timeout = NULL,
53+
# Vector of status codes to retry
54+
retryStatusCodes=NULL,
55+
# Maximum number of retry attempts for the retry status codes
56+
maxRetryAttempts = NULL,
5157
# constructor
52-
initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){
58+
initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL, retryStatusCodes=NULL, maxRetryAttempts=NULL){
5359
if (!is.null(basePath)) {
5460
self$basePath <- basePath
5561
}
@@ -83,8 +89,42 @@ ApiClient <- R6::R6Class(
8389
if (!is.null(timeout)) {
8490
self$timeout <- timeout
8591
}
92+
93+
if (!is.null(retryStatusCodes)) {
94+
self$retryStatusCodes <- retryStatusCodes
95+
}
96+
97+
if (!is.null(maxRetryAttempts)) {
98+
self$maxRetryAttempts <- maxRetryAttempts
99+
}
86100
},
101+
87102
CallApi = function(url, method, queryParams, headerParams, body, ...){
103+
104+
resp <- self$Execute(url, method, queryParams, headerParams, body, ...)
105+
statusCode <- httr::status_code(resp)
106+
107+
if (is.null(self$maxRetryAttempts)) {
108+
self$maxRetryAttempts = 3
109+
}
110+
111+
if (!is.null(self$retryStatusCodes)) {
112+
113+
for (i in 1 : self$maxRetryAttempts) {
114+
if (statusCode %in% self$retryStatusCodes) {
115+
Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1))
116+
resp <- self$Execute(url, method, queryParams, headerParams, body, ...)
117+
statusCode <- httr::status_code(resp)
118+
} else {
119+
break;
120+
}
121+
}
122+
}
123+
124+
resp
125+
},
126+
127+
Execute = function(url, method, queryParams, headerParams, body, ...){
88128
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))
89129

90130
{{! Adding timeout that can be set at the apiClient object level}}

samples/client/petstore/R/R/api_client.R

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#' @field apiKeys
3232
#' @field accessToken
3333
#' @field timeout Default timeout in seconds
34+
#' @field retryStatusCodes vector of status codes to retry
35+
#' @field maxRetryAttempts maximum number of retries for the status codes
3436
#' @importFrom httr add_headers accept timeout content
3537
#' @export
3638
ApiClient <- R6::R6Class(
@@ -52,8 +54,12 @@ ApiClient <- R6::R6Class(
5254
accessToken = NULL,
5355
# Time Out (seconds)
5456
timeout = NULL,
57+
# Vector of status codes to retry
58+
retryStatusCodes=NULL,
59+
# Maximum number of retry attempts for the retry status codes
60+
maxRetryAttempts = NULL,
5561
# constructor
56-
initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){
62+
initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL, retryStatusCodes=NULL, maxRetryAttempts=NULL){
5763
if (!is.null(basePath)) {
5864
self$basePath <- basePath
5965
}
@@ -87,8 +93,42 @@ ApiClient <- R6::R6Class(
8793
if (!is.null(timeout)) {
8894
self$timeout <- timeout
8995
}
96+
97+
if (!is.null(retryStatusCodes)) {
98+
self$retryStatusCodes <- retryStatusCodes
99+
}
100+
101+
if (!is.null(maxRetryAttempts)) {
102+
self$maxRetryAttempts <- maxRetryAttempts
103+
}
90104
},
105+
91106
CallApi = function(url, method, queryParams, headerParams, body, ...){
107+
108+
resp <- self$Execute(url, method, queryParams, headerParams, body, ...)
109+
statusCode <- httr::status_code(resp)
110+
111+
if (is.null(self$maxRetryAttempts)) {
112+
self$maxRetryAttempts = 3
113+
}
114+
115+
if (!is.null(self$retryStatusCodes)) {
116+
117+
for (i in 1 : self$maxRetryAttempts) {
118+
if (statusCode %in% self$retryStatusCodes) {
119+
Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1))
120+
resp <- self$Execute(url, method, queryParams, headerParams, body, ...)
121+
statusCode <- httr::status_code(resp)
122+
} else {
123+
break;
124+
}
125+
}
126+
}
127+
128+
resp
129+
},
130+
131+
Execute = function(url, method, queryParams, headerParams, body, ...){
92132
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))
93133

94134
httpTimeout <- NULL

0 commit comments

Comments
 (0)