Skip to content

support binary response for R api client #17626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ ApiResponse <- R6::R6Class(
self$status_code <- status_code
self$status_code_desc <- status_code_desc
self$headers <- headers
},

#' Return the response as text
#'
#' @description
#' The response is stored as a raw vector. Use this to access the response after
#' converting it to text. If the response is not coercible to text NA is returned.
#'
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspired by httr2::resp_body_string

if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
}
return(text_response)
}
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@
}

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "{{returnType}}", loadNamespace("{{packageName}}")),
self$api_client$deserialize(local_var_resp$response_as_text(), "{{returnType}}", loadNamespace("{{packageName}}")),
error = function(e) {
{{#useDefaultExceptionHandling}}
stop("Failed to deserialize response")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- httr::status_code(httr_response)
api_response$status_code_desc <- httr::http_status(httr_response)$reason
api_response$response <- httr::content(httr_response, "text", encoding = "UTF-8")
api_response$response <- httr::content(httr_response, "raw")
api_response$headers <- httr::headers(httr_response)

api_response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_string()
api_response$response <- resp %>% resp_body_raw()
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R-httr2-wrapper/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_string()
api_response$response <- resp %>% resp_body_raw()
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
17 changes: 17 additions & 0 deletions samples/client/petstore/R-httr2-wrapper/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ ApiResponse <- R6::R6Class(
self$status_code <- status_code
self$status_code_desc <- status_code_desc
self$headers <- headers
},

#' Return the response as text
#'
#' @description
#' The response is stored as a raw vector. Use this to access the response after
#' converting it to text. If the response is not coercible to text NA is returned.
#'
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
}
return(text_response)
}
)
)
4 changes: 2 additions & 2 deletions samples/client/petstore/R-httr2-wrapper/R/fake_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ FakeApi <- R6::R6Class(
}

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "User", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "User", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/R-httr2-wrapper/R/pet_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ PetApi <- R6::R6Class(
}

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

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

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

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

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

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

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "ModelApiResponse", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "ModelApiResponse", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore/R-httr2-wrapper/R/store_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ StoreApi <- R6::R6Class(
}

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

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "Order", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "Order", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/R-httr2-wrapper/R/user_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ UserApi <- R6::R6Class(
}

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "character", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "character", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R-httr2/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- resp %>% resp_status()
api_response$status_code_desc <- resp %>% resp_status_desc()
api_response$response <- resp %>% resp_body_string()
api_response$response <- resp %>% resp_body_raw()
api_response$headers <- resp %>% resp_headers()

api_response
Expand Down
17 changes: 17 additions & 0 deletions samples/client/petstore/R-httr2/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ ApiResponse <- R6::R6Class(
self$status_code <- status_code
self$status_code_desc <- status_code_desc
self$headers <- headers
},

#' Return the response as text
#'
#' @description
#' The response is stored as a raw vector. Use this to access the response after
#' converting it to text. If the response is not coercible to text NA is returned.
#'
#' @param from_encoding The encoding of the raw response.
#' @param to_encoding The target encoding of the return value.
#' @export
response_as_text = function(from_encoding = NULL, to_encoding = "UTF-8") {
text_response <- iconv(readBin(self$response, character()), from = from_encoding, to = to_encoding)
if (is.na(text_response)) {
warning("The response is binary and will not be converted to text.")
}
return(text_response)
}
)
)
4 changes: 2 additions & 2 deletions samples/client/petstore/R-httr2/R/fake_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ FakeApi <- R6::R6Class(
}

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "User", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "User", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
16 changes: 8 additions & 8 deletions samples/client/petstore/R-httr2/R/pet_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ PetApi <- R6::R6Class(
}

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

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

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

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

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

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

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "ModelApiResponse", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "ModelApiResponse", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
6 changes: 3 additions & 3 deletions samples/client/petstore/R-httr2/R/store_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ StoreApi <- R6::R6Class(
}

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

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "Order", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "Order", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/R-httr2/R/user_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ UserApi <- R6::R6Class(
}

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

deserialized_resp_obj <- tryCatch(
self$api_client$deserialize(local_var_resp$response, "character", loadNamespace("petstore")),
self$api_client$deserialize(local_var_resp$response_as_text(), "character", loadNamespace("petstore")),
error = function(e) {
rlang::abort(message = "Failed to deserialize response",
.subclass = "ApiException",
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/R/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ ApiClient <- R6::R6Class(
api_response <- ApiResponse$new()
api_response$status_code <- httr::status_code(httr_response)
api_response$status_code_desc <- httr::http_status(httr_response)$reason
api_response$response <- httr::content(httr_response, "text", encoding = "UTF-8")
api_response$response <- httr::content(httr_response, "raw")
api_response$headers <- httr::headers(httr_response)

api_response
Expand Down
Loading