Skip to content

Commit b3022dd

Browse files
committed
[R client] better support for binary/compressed responses
1 parent 96adf2f commit b3022dd

File tree

24 files changed

+1555
-735
lines changed

24 files changed

+1555
-735
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ ApiResponse <- R6::R6Class(
5050
self$response <- charToRaw(jsonlite::toJSON("NULL"))
5151
}
5252
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
53-
if (is.na(text_response)) {
54-
warning("The response is binary and will not be converted to text.")
55-
}
5653
return(text_response)
5754
}
5855
)

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,17 @@
153153
154154
{{/vendorExtensions.x-streaming}}
155155
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
156-
local_var_response$content
156+
if (is.raw(local_var_response$content)) {
157+
return(local_var_response)
158+
} else {
159+
return(local_var_response$content)
160+
}
157161
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
158-
local_var_response
162+
return(local_var_response)
159163
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
160-
local_var_response
164+
return(local_var_response)
161165
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
162-
local_var_response
166+
return(local_var_response)
163167
}
164168
},
165169
@@ -548,19 +552,19 @@
548552
)
549553
# save response in a file
550554
if (!is.null(data_file)) {
551-
write(local_var_content, data_file)
555+
private$WriteFile(local_var_content, data_file, local_var_accepts)
552556
}
553557
554558
ApiResponse$new(content,resp)
555559
{{/isPrimitiveType}}
556560
{{^isPrimitiveType}}
557561
# save response in a file
558562
if (!is.null(data_file)) {
559-
write(local_var_resp$response, data_file)
563+
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
560564
}
561565
562566
deserialized_resp_obj <- tryCatch(
563-
self$api_client$deserialize(local_var_resp$response_as_text(), "{{returnType}}", loadNamespace("{{packageName}}")),
567+
private$Deserialize(local_var_resp),
564568
error = function(e) {
565569
{{#useDefaultExceptionHandling}}
566570
stop("Failed to deserialize response")
@@ -579,7 +583,7 @@
579583
{{! Returning the ApiResponse object with NULL object when the endpoint doesn't return anything}}
580584
local_var_resp$content <- NULL
581585
{{/returnType}}
582-
local_var_resp
586+
return(local_var_resp)
583587
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
584588
{{#returnExceptionOnFailure}}
585589
local_var_error_msg <- local_var_resp$response
@@ -635,11 +639,32 @@
635639
if (is.null(local_var_resp$response) || local_var_resp$response == "") {
636640
local_var_resp$response <- "API server error"
637641
}
638-
local_var_resp
642+
return(local_var_resp)
639643
{{/returnExceptionOnFailure}}
640644
}
641645
}{{^-last}},{{/-last}}
642646
{{/operation}}
647+
),
648+
private = list(
649+
WriteFile = function(x, file, accepts) {
650+
if (private$IsBinary(accepts)) {
651+
writeBin(x, file)
652+
} else {
653+
base::write(x, file)
654+
}
655+
},
656+
657+
IsBinary = function(accepts) {
658+
return(any(grepl("gzip", as.character(accepts))))
659+
},
660+
661+
Deserialize = function(local_var_resp) {
662+
text <- local_var_resp$response_as_text()
663+
if (is.na(text)) {
664+
return(local_var_resp$response)
665+
}
666+
return(self$api_client$deserialize(text, "object", loadNamespace("k8s.client")))
667+
}
643668
)
644669
)
645670
{{/operations}}

samples/client/echo_api/r/R/api_response.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ ApiResponse <- R6::R6Class(
5757
self$response <- charToRaw(jsonlite::toJSON("NULL"))
5858
}
5959
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
60-
if (is.na(text_response)) {
61-
warning("The response is binary and will not be converted to text.")
62-
}
6360
return(text_response)
6461
}
6562
)

samples/client/echo_api/r/R/auth_api.R

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ AuthApi <- R6::R6Class(
7878
TestAuthHttpBasic = function(data_file = NULL, ...) {
7979
local_var_response <- self$TestAuthHttpBasicWithHttpInfo(data_file = data_file, ...)
8080
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
81-
local_var_response$content
81+
if (is.raw(local_var_response$content)) {
82+
return(local_var_response)
83+
} else {
84+
return(local_var_response$content)
85+
}
8286
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
83-
local_var_response
87+
return(local_var_response)
8488
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
85-
local_var_response
89+
return(local_var_response)
8690
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
87-
local_var_response
91+
return(local_var_response)
8892
}
8993
},
9094

@@ -133,17 +137,17 @@ AuthApi <- R6::R6Class(
133137
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
134138
# save response in a file
135139
if (!is.null(data_file)) {
136-
write(local_var_resp$response, data_file)
140+
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
137141
}
138142

139143
deserialized_resp_obj <- tryCatch(
140-
self$api_client$deserialize(local_var_resp$response_as_text(), "character", loadNamespace("openapi")),
144+
private$Deserialize(local_var_resp),
141145
error = function(e) {
142146
stop("Failed to deserialize response")
143147
}
144148
)
145149
local_var_resp$content <- deserialized_resp_obj
146-
local_var_resp
150+
return(local_var_resp)
147151
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
148152
ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp)
149153
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
@@ -152,7 +156,7 @@ AuthApi <- R6::R6Class(
152156
if (is.null(local_var_resp$response) || local_var_resp$response == "") {
153157
local_var_resp$response <- "API server error"
154158
}
155-
local_var_resp
159+
return(local_var_resp)
156160
}
157161
},
158162

@@ -166,13 +170,17 @@ AuthApi <- R6::R6Class(
166170
TestAuthHttpBearer = function(data_file = NULL, ...) {
167171
local_var_response <- self$TestAuthHttpBearerWithHttpInfo(data_file = data_file, ...)
168172
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
169-
local_var_response$content
173+
if (is.raw(local_var_response$content)) {
174+
return(local_var_response)
175+
} else {
176+
return(local_var_response$content)
177+
}
170178
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
171-
local_var_response
179+
return(local_var_response)
172180
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
173-
local_var_response
181+
return(local_var_response)
174182
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
175-
local_var_response
183+
return(local_var_response)
176184
}
177185
},
178186

@@ -221,17 +229,17 @@ AuthApi <- R6::R6Class(
221229
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
222230
# save response in a file
223231
if (!is.null(data_file)) {
224-
write(local_var_resp$response, data_file)
232+
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
225233
}
226234

227235
deserialized_resp_obj <- tryCatch(
228-
self$api_client$deserialize(local_var_resp$response_as_text(), "character", loadNamespace("openapi")),
236+
private$Deserialize(local_var_resp),
229237
error = function(e) {
230238
stop("Failed to deserialize response")
231239
}
232240
)
233241
local_var_resp$content <- deserialized_resp_obj
234-
local_var_resp
242+
return(local_var_resp)
235243
} else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
236244
ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp)
237245
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
@@ -240,8 +248,29 @@ AuthApi <- R6::R6Class(
240248
if (is.null(local_var_resp$response) || local_var_resp$response == "") {
241249
local_var_resp$response <- "API server error"
242250
}
243-
local_var_resp
251+
return(local_var_resp)
252+
}
253+
}
254+
),
255+
private = list(
256+
WriteFile = function(x, file, accepts) {
257+
if (private$IsBinary(accepts)) {
258+
writeBin(x, file)
259+
} else {
260+
base::write(x, file)
261+
}
262+
},
263+
264+
IsBinary = function(accepts) {
265+
return(any(grepl("gzip", as.character(accepts))))
266+
},
267+
268+
Deserialize = function(local_var_resp) {
269+
text <- local_var_resp$response_as_text()
270+
if (is.na(text)) {
271+
return(local_var_resp$response)
244272
}
273+
return(self$api_client$deserialize(text, "object", loadNamespace("k8s.client")))
245274
}
246275
)
247276
)

0 commit comments

Comments
 (0)