24
24
#' @field apiKeys
25
25
#' @field accessToken
26
26
#' @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
27
29
#' @importFrom httr add_headers accept timeout content
28
30
{ {#useRlangExceptionHandling} }
29
31
#' @importFrom rlang abort
@@ -48,8 +50,12 @@ ApiClient <- R6::R6Class(
48
50
accessToken = NULL,
49
51
# Time Out (seconds)
50
52
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,
51
57
# 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 ){
53
59
if (!is.null(basePath)) {
54
60
self$basePath < - basePath
55
61
}
@@ -83,8 +89,42 @@ ApiClient <- R6::R6Class(
83
89
if (!is.null(timeout)) {
84
90
self$timeout < - timeout
85
91
}
92
+
93
+ if (!is.null(retryStatusCodes)) {
94
+ self$retryStatusCodes < - retryStatusCodes
95
+ }
96
+
97
+ if (!is.null(maxRetryAttempts)) {
98
+ self$maxRetryAttempts < - maxRetryAttempts
99
+ }
86
100
},
101
+
87
102
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, ...){
88
128
headers <- httr::add_headers(c(headerParams, self$defaultHeaders))
89
129
90
130
{ { ! Adding timeout that can be set at the apiClient object level} }
0 commit comments