@@ -133,9 +133,9 @@ ApiClient <- R6::R6Class(
133
133
# ' @param ... Other optional arguments.
134
134
# ' @return HTTP response
135
135
# ' @export
136
- CallApi = function (url , method , query_params , header_params , body , ... ) {
136
+ CallApi = function (url , method , query_params , header_params , body , stream_callback = NULL , ... ) {
137
137
138
- resp <- self $ Execute(url , method , query_params , header_params , body , ... )
138
+ resp <- self $ Execute(url , method , query_params , header_params , body , stream_callback = stream_callback , ... )
139
139
status_code <- httr :: status_code(resp )
140
140
141
141
if (is.null(self $ max_retry_attempts )) {
@@ -147,7 +147,7 @@ ApiClient <- R6::R6Class(
147
147
for (i in 1 : self $ max_retry_attempts ) {
148
148
if (status_code %in% self $ retry_status_codes ) {
149
149
Sys.sleep((2 ^ i ) + stats :: runif(n = 1 , min = 0 , max = 1 ))
150
- resp <- self $ Execute(url , method , query_params , header_params , body , ... )
150
+ resp <- self $ Execute(url , method , query_params , header_params , body , stream_callback = stream_callback , ... )
151
151
status_code <- httr :: status_code(resp )
152
152
} else {
153
153
break ;
@@ -167,10 +167,11 @@ ApiClient <- R6::R6Class(
167
167
# ' @param query_params The query parameters.
168
168
# ' @param header_params The header parameters.
169
169
# ' @param body The HTTP request body.
170
+ # ' @param stream_callback callback function to process data stream
170
171
# ' @param ... Other optional arguments.
171
172
# ' @return HTTP response
172
173
# ' @export
173
- Execute = function (url , method , query_params , header_params , body , ... ) {
174
+ Execute = function (url , method , query_params , header_params , body , stream_callback = NULL , ... ) {
174
175
headers <- httr :: add_headers(c(header_params , self $ default_headers ))
175
176
176
177
http_timeout <- NULL
@@ -179,17 +180,41 @@ ApiClient <- R6::R6Class(
179
180
}
180
181
181
182
if (method == " GET" ) {
182
- httr :: GET(url , query = query_params , headers , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
183
+ if (typeof(stream_callback ) == " closure" ) {
184
+ httr :: GET(url , query = query_params , headers , http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
185
+ } else {
186
+ httr :: GET(url , query = query_params , headers , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
187
+ }
183
188
} else if (method == " POST" ) {
184
- httr :: POST(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
189
+ if (typeof(stream_callback ) == " closure" ) {
190
+ httr :: POST(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
191
+ } else {
192
+ httr :: POST(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
193
+ }
185
194
} else if (method == " PUT" ) {
186
- httr :: PUT(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
195
+ if (typeof(stream_callback ) == " closure" ) {
196
+ httr :: PUT(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
197
+ } else {
198
+ httr :: PUT(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
199
+ }
187
200
} else if (method == " PATCH" ) {
188
- httr :: PATCH(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
201
+ if (typeof(stream_callback ) == " closure" ) {
202
+ httr :: PATCH(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
203
+ } else {
204
+ httr :: PATCH(url , query = query_params , headers , body = body , httr :: content_type(" application/json" ), http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
205
+ }
189
206
} else if (method == " HEAD" ) {
190
- httr :: HEAD(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
207
+ if (typeof(stream_callback ) == " closure" ) {
208
+ httr :: HEAD(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
209
+ } else {
210
+ httr :: HEAD(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
211
+ }
191
212
} else if (method == " DELETE" ) {
192
- httr :: DELETE(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
213
+ if (typeof(stream_callback ) == " closure" ) {
214
+ httr :: DELETE(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), write_stream(stream_callback ), ... )
215
+ } else {
216
+ httr :: DELETE(url , query = query_params , headers , http_timeout , http_timeout , httr :: user_agent(self $ `user_agent` ), ... )
217
+ }
193
218
} else {
194
219
err_msg <- " Http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`."
195
220
rlang :: abort(message = err_msg , .subclass = " ApiException" , ApiException = ApiException $ new(status = 0 , reason = err_msg ))
0 commit comments