Skip to content

Commit 1e17712

Browse files
authored
support binary response for R api client (#17626)
* support binary response for R api * rework * autogenerated samples
1 parent b2f622c commit 1e17712

File tree

22 files changed

+119
-51
lines changed

22 files changed

+119
-51
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ ApiResponse <- R6::R6Class(
3838
self$status_code <- status_code
3939
self$status_code_desc <- status_code_desc
4040
self$headers <- headers
41+
},
42+
43+
#' Return the response as text
44+
#'
45+
#' @description
46+
#' The response is stored as a raw vector. Use this to access the response after
47+
#' converting it to text. If the response is not coercible to text NA is returned.
48+
#'
49+
#' @param from_encoding The encoding of the raw response.
50+
#' @param to_encoding The target encoding of the return value.
51+
#' @export
52+
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
53+
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
54+
if (is.na(text_response)) {
55+
warning("The response is binary and will not be converted to text.")
56+
}
57+
return(text_response)
4158
}
4259
)
4360
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@
637637
}
638638
639639
deserialized_resp_obj <- tryCatch(
640-
self$api_client$deserialize(local_var_resp$response, "{{returnType}}", loadNamespace("{{packageName}}")),
640+
self$api_client$deserialize(local_var_resp$response_as_text(), "{{returnType}}", loadNamespace("{{packageName}}")),
641641
error = function(e) {
642642
{{#useDefaultExceptionHandling}}
643643
stop("Failed to deserialize response")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ ApiClient <- R6::R6Class(
318318
api_response <- ApiResponse$new()
319319
api_response$status_code <- httr::status_code(httr_response)
320320
api_response$status_code_desc <- httr::http_status(httr_response)$reason
321-
api_response$response <- httr::content(httr_response, "text", encoding = "UTF-8")
321+
api_response$response <- httr::content(httr_response, "raw")
322322
api_response$headers <- httr::headers(httr_response)
323323
324324
api_response

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ ApiClient <- R6::R6Class(
323323
api_response <- ApiResponse$new()
324324
api_response$status_code <- resp %>% resp_status()
325325
api_response$status_code_desc <- resp %>% resp_status_desc()
326-
api_response$response <- resp %>% resp_body_string()
326+
api_response$response <- resp %>% resp_body_raw()
327327
api_response$headers <- resp %>% resp_headers()
328328
329329
api_response

samples/client/petstore/R-httr2-wrapper/R/api_client.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ ApiClient <- R6::R6Class(
312312
api_response <- ApiResponse$new()
313313
api_response$status_code <- resp %>% resp_status()
314314
api_response$status_code_desc <- resp %>% resp_status_desc()
315-
api_response$response <- resp %>% resp_body_string()
315+
api_response$response <- resp %>% resp_body_raw()
316316
api_response$headers <- resp %>% resp_headers()
317317

318318
api_response

samples/client/petstore/R-httr2-wrapper/R/api_response.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ ApiResponse <- R6::R6Class(
4444
self$status_code <- status_code
4545
self$status_code_desc <- status_code_desc
4646
self$headers <- headers
47+
},
48+
49+
#' Return the response as text
50+
#'
51+
#' @description
52+
#' The response is stored as a raw vector. Use this to access the response after
53+
#' converting it to text. If the response is not coercible to text NA is returned.
54+
#'
55+
#' @param from_encoding The encoding of the raw response.
56+
#' @param to_encoding The target encoding of the return value.
57+
#' @export
58+
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
59+
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+
}
63+
return(text_response)
4764
}
4865
)
4966
)

samples/client/petstore/R-httr2-wrapper/R/fake_api.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ FakeApi <- R6::R6Class(
365365
}
366366

367367
deserialized_resp_obj <- tryCatch(
368-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
368+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
369369
error = function(e) {
370370
rlang::abort(message = "Failed to deserialize response",
371371
.subclass = "ApiException",
@@ -485,7 +485,7 @@ FakeApi <- R6::R6Class(
485485
}
486486

487487
deserialized_resp_obj <- tryCatch(
488-
self$api_client$deserialize(local_var_resp$response, "User", loadNamespace("petstore")),
488+
self$api_client$deserialize(local_var_resp$response_as_text(), "User", loadNamespace("petstore")),
489489
error = function(e) {
490490
rlang::abort(message = "Failed to deserialize response",
491491
.subclass = "ApiException",

samples/client/petstore/R-httr2-wrapper/R/pet_api.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ PetApi <- R6::R6Class(
746746
}
747747

748748
deserialized_resp_obj <- tryCatch(
749-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
749+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
750750
error = function(e) {
751751
rlang::abort(message = "Failed to deserialize response",
752752
.subclass = "ApiException",
@@ -984,7 +984,7 @@ PetApi <- R6::R6Class(
984984
}
985985

986986
deserialized_resp_obj <- tryCatch(
987-
self$api_client$deserialize(local_var_resp$response, "array[Pet]", loadNamespace("petstore")),
987+
self$api_client$deserialize(local_var_resp$response_as_text(), "array[Pet]", loadNamespace("petstore")),
988988
error = function(e) {
989989
rlang::abort(message = "Failed to deserialize response",
990990
.subclass = "ApiException",
@@ -1100,7 +1100,7 @@ PetApi <- R6::R6Class(
11001100
}
11011101

11021102
deserialized_resp_obj <- tryCatch(
1103-
self$api_client$deserialize(local_var_resp$response, "array[Pet]", loadNamespace("petstore")),
1103+
self$api_client$deserialize(local_var_resp$response_as_text(), "array[Pet]", loadNamespace("petstore")),
11041104
error = function(e) {
11051105
rlang::abort(message = "Failed to deserialize response",
11061106
.subclass = "ApiException",
@@ -1221,7 +1221,7 @@ PetApi <- R6::R6Class(
12211221
}
12221222

12231223
deserialized_resp_obj <- tryCatch(
1224-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
1224+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
12251225
error = function(e) {
12261226
rlang::abort(message = "Failed to deserialize response",
12271227
.subclass = "ApiException",
@@ -1353,7 +1353,7 @@ PetApi <- R6::R6Class(
13531353
}
13541354

13551355
deserialized_resp_obj <- tryCatch(
1356-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
1356+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
13571357
error = function(e) {
13581358
rlang::abort(message = "Failed to deserialize response",
13591359
.subclass = "ApiException",
@@ -1483,7 +1483,7 @@ PetApi <- R6::R6Class(
14831483
}
14841484

14851485
deserialized_resp_obj <- tryCatch(
1486-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
1486+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
14871487
error = function(e) {
14881488
rlang::abort(message = "Failed to deserialize response",
14891489
.subclass = "ApiException",
@@ -1605,7 +1605,7 @@ PetApi <- R6::R6Class(
16051605
}
16061606

16071607
deserialized_resp_obj <- tryCatch(
1608-
self$api_client$deserialize(local_var_resp$response, "Pet", loadNamespace("petstore")),
1608+
self$api_client$deserialize(local_var_resp$response_as_text(), "Pet", loadNamespace("petstore")),
16091609
error = function(e) {
16101610
rlang::abort(message = "Failed to deserialize response",
16111611
.subclass = "ApiException",
@@ -1843,7 +1843,7 @@ PetApi <- R6::R6Class(
18431843
}
18441844

18451845
deserialized_resp_obj <- tryCatch(
1846-
self$api_client$deserialize(local_var_resp$response, "ModelApiResponse", loadNamespace("petstore")),
1846+
self$api_client$deserialize(local_var_resp$response_as_text(), "ModelApiResponse", loadNamespace("petstore")),
18471847
error = function(e) {
18481848
rlang::abort(message = "Failed to deserialize response",
18491849
.subclass = "ApiException",

samples/client/petstore/R-httr2-wrapper/R/store_api.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ StoreApi <- R6::R6Class(
438438
}
439439

440440
deserialized_resp_obj <- tryCatch(
441-
self$api_client$deserialize(local_var_resp$response, "map(integer)", loadNamespace("petstore")),
441+
self$api_client$deserialize(local_var_resp$response_as_text(), "map(integer)", loadNamespace("petstore")),
442442
error = function(e) {
443443
rlang::abort(message = "Failed to deserialize response",
444444
.subclass = "ApiException",
@@ -567,7 +567,7 @@ StoreApi <- R6::R6Class(
567567
}
568568

569569
deserialized_resp_obj <- tryCatch(
570-
self$api_client$deserialize(local_var_resp$response, "Order", loadNamespace("petstore")),
570+
self$api_client$deserialize(local_var_resp$response_as_text(), "Order", loadNamespace("petstore")),
571571
error = function(e) {
572572
rlang::abort(message = "Failed to deserialize response",
573573
.subclass = "ApiException",
@@ -686,7 +686,7 @@ StoreApi <- R6::R6Class(
686686
}
687687

688688
deserialized_resp_obj <- tryCatch(
689-
self$api_client$deserialize(local_var_resp$response, "Order", loadNamespace("petstore")),
689+
self$api_client$deserialize(local_var_resp$response_as_text(), "Order", loadNamespace("petstore")),
690690
error = function(e) {
691691
rlang::abort(message = "Failed to deserialize response",
692692
.subclass = "ApiException",

samples/client/petstore/R-httr2-wrapper/R/user_api.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ UserApi <- R6::R6Class(
970970
}
971971

972972
deserialized_resp_obj <- tryCatch(
973-
self$api_client$deserialize(local_var_resp$response, "User", loadNamespace("petstore")),
973+
self$api_client$deserialize(local_var_resp$response_as_text(), "User", loadNamespace("petstore")),
974974
error = function(e) {
975975
rlang::abort(message = "Failed to deserialize response",
976976
.subclass = "ApiException",
@@ -1103,7 +1103,7 @@ UserApi <- R6::R6Class(
11031103
}
11041104

11051105
deserialized_resp_obj <- tryCatch(
1106-
self$api_client$deserialize(local_var_resp$response, "character", loadNamespace("petstore")),
1106+
self$api_client$deserialize(local_var_resp$response_as_text(), "character", loadNamespace("petstore")),
11071107
error = function(e) {
11081108
rlang::abort(message = "Failed to deserialize response",
11091109
.subclass = "ApiException",

0 commit comments

Comments
 (0)