From 88fc70d58570d0c8a4c20d53b330b374c67b37ec Mon Sep 17 00:00:00 2001 From: Matthew Pollock Date: Thu, 21 Nov 2024 06:42:57 -0500 Subject: [PATCH] move private api methods to api-client, revert breaking method name change --- .../src/main/resources/r/ApiResponse.mustache | 2 +- .../src/main/resources/r/api.mustache | 55 +-------- .../src/main/resources/r/api_client.mustache | 46 ++++++++ .../main/resources/r/api_exception.mustache | 4 +- .../r/libraries/httr2/api_client.mustache | 46 ++++++++ samples/client/echo_api/r/R/api_client.R | 46 ++++++++ samples/client/echo_api/r/R/api_response.R | 2 +- samples/client/echo_api/r/R/auth_api.R | 59 +--------- samples/client/echo_api/r/R/body_api.R | 107 +++++------------- samples/client/echo_api/r/R/form_api.R | 65 ++--------- samples/client/echo_api/r/R/header_api.R | 53 +-------- samples/client/echo_api/r/R/path_api.R | 53 +-------- samples/client/echo_api/r/R/query_api.R | 107 +++++------------- .../petstore/R-httr2-wrapper/R/api_client.R | 48 +++++++- .../R-httr2-wrapper/R/api_exception.R | 4 +- .../petstore/R-httr2-wrapper/R/api_response.R | 2 +- .../petstore/R-httr2-wrapper/R/fake_api.R | 65 ++--------- .../petstore/R-httr2-wrapper/R/pet_api.R | 99 +++++----------- .../petstore/R-httr2-wrapper/R/store_api.R | 67 ++--------- .../petstore/R-httr2-wrapper/R/user_api.R | 71 ++---------- .../client/petstore/R-httr2/R/api_client.R | 46 ++++++++ .../client/petstore/R-httr2/R/api_exception.R | 4 +- .../client/petstore/R-httr2/R/api_response.R | 2 +- samples/client/petstore/R-httr2/R/fake_api.R | 65 ++--------- samples/client/petstore/R-httr2/R/pet_api.R | 99 +++++----------- samples/client/petstore/R-httr2/R/store_api.R | 67 ++--------- samples/client/petstore/R-httr2/R/user_api.R | 71 ++---------- samples/client/petstore/R/R/api_client.R | 46 ++++++++ samples/client/petstore/R/R/api_exception.R | 4 +- samples/client/petstore/R/R/api_response.R | 2 +- samples/client/petstore/R/R/fake_api.R | 65 ++--------- samples/client/petstore/R/R/pet_api.R | 99 +++++----------- samples/client/petstore/R/R/store_api.R | 67 ++--------- samples/client/petstore/R/R/user_api.R | 71 ++---------- 34 files changed, 546 insertions(+), 1163 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache b/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache index 6236f7d2e8a7..e985255bdda8 100644 --- a/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache +++ b/modules/openapi-generator/src/main/resources/r/ApiResponse.mustache @@ -45,7 +45,7 @@ ApiResponse <- R6::R6Class( #' #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. - ResponseAsText = function(from_encoding = "", to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { if (is.null(self$response)) { self$response <- charToRaw(jsonlite::toJSON("NULL")) } diff --git a/modules/openapi-generator/src/main/resources/r/api.mustache b/modules/openapi-generator/src/main/resources/r/api.mustache index 938c728249f5..2895328c09c9 100644 --- a/modules/openapi-generator/src/main/resources/r/api.mustache +++ b/modules/openapi-generator/src/main/resources/r/api.mustache @@ -545,7 +545,7 @@ {{#isPrimitiveType}} # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } ApiResponse$new(content,resp) @@ -553,11 +553,11 @@ {{^isPrimitiveType}} # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "{{returnType}}"), + self$api_client$DeserializeResponse(local_var_resp, "{{returnType}}"), error = function(e) { {{#useDefaultExceptionHandling}} stop("Failed to deserialize response") @@ -579,7 +579,7 @@ return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { {{#returnExceptionOnFailure}} @@ -638,53 +638,6 @@ } }{{^-last}},{{/-last}} {{/operation}} - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("{{packageName}}"))) - } ) ) {{/operations}} diff --git a/modules/openapi-generator/src/main/resources/r/api_client.mustache b/modules/openapi-generator/src/main/resources/r/api_client.mustache index fb1285d0f177..b1844f4686b0 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -430,6 +430,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("{{packageName}}"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/modules/openapi-generator/src/main/resources/r/api_exception.mustache b/modules/openapi-generator/src/main/resources/r/api_exception.mustache index dadf7df449e5..5aac3eab7889 100644 --- a/modules/openapi-generator/src/main/resources/r/api_exception.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_exception.mustache @@ -32,7 +32,7 @@ ApiException <- R6::R6Class( initialize = function(status = NULL, reason = NULL, http_response = NULL) { if (!is.null(http_response)) { self$status <- http_response$status_code - errorMsg <- http_response$ResponseAsText() + errorMsg <- http_response$response_as_text() if (is.null(errorMsg) || errorMsg == "") { errorMsg <- "Api exception encountered. No details given." } @@ -40,7 +40,7 @@ ApiException <- R6::R6Class( self$headers <- http_response$headers self$reason <- http_response$http_status_desc {{#errorObjectType}} - self$error_object <- {{errorObjectType}}$new()$fromJSONString(http_response$ResponseAsText()) + self$error_object <- {{errorObjectType}}$new()$fromJSONString(http_response$response_as_text()) {{/errorObjectType}} } else { self$status <- status diff --git a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache index e888008f4597..f2e95e679465 100644 --- a/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/libraries/httr2/api_client.mustache @@ -443,6 +443,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("{{packageName}}"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/samples/client/echo_api/r/R/api_client.R b/samples/client/echo_api/r/R/api_client.R index 3c9001df6ef3..2f7bcc7a1710 100644 --- a/samples/client/echo_api/r/R/api_client.R +++ b/samples/client/echo_api/r/R/api_client.R @@ -382,6 +382,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("openapi"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/samples/client/echo_api/r/R/api_response.R b/samples/client/echo_api/r/R/api_response.R index 71ad2d7f97db..3dfb2c96a588 100644 --- a/samples/client/echo_api/r/R/api_response.R +++ b/samples/client/echo_api/r/R/api_response.R @@ -52,7 +52,7 @@ ApiResponse <- R6::R6Class( #' #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. - ResponseAsText = function(from_encoding = "", to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { if (is.null(self$response)) { self$response <- charToRaw(jsonlite::toJSON("NULL")) } diff --git a/samples/client/echo_api/r/R/auth_api.R b/samples/client/echo_api/r/R/auth_api.R index 2f27527f5f24..8b2999b3758e 100644 --- a/samples/client/echo_api/r/R/auth_api.R +++ b/samples/client/echo_api/r/R/auth_api.R @@ -133,11 +133,11 @@ AuthApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -146,7 +146,7 @@ AuthApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -224,11 +224,11 @@ AuthApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -237,7 +237,7 @@ AuthApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -249,52 +249,5 @@ AuthApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/echo_api/r/R/body_api.R b/samples/client/echo_api/r/R/body_api.R index da57045a19c5..1be1cafba8b4 100644 --- a/samples/client/echo_api/r/R/body_api.R +++ b/samples/client/echo_api/r/R/body_api.R @@ -235,11 +235,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "data.frame"), + self$api_client$DeserializeResponse(local_var_resp, "data.frame"), error = function(e) { stop("Failed to deserialize response") } @@ -248,7 +248,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -331,11 +331,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -344,7 +344,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -426,11 +426,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -439,7 +439,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -517,11 +517,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -530,7 +530,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -613,11 +613,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { stop("Failed to deserialize response") } @@ -626,7 +626,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -709,11 +709,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -722,7 +722,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -805,11 +805,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { stop("Failed to deserialize response") } @@ -818,7 +818,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -901,11 +901,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -914,7 +914,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -997,11 +997,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "StringEnumRef"), + self$api_client$DeserializeResponse(local_var_resp, "StringEnumRef"), error = function(e) { stop("Failed to deserialize response") } @@ -1010,7 +1010,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -1093,11 +1093,11 @@ BodyApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -1106,7 +1106,7 @@ BodyApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -1118,52 +1118,5 @@ BodyApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/echo_api/r/R/form_api.R b/samples/client/echo_api/r/R/form_api.R index 20b784be6701..040ce871545b 100644 --- a/samples/client/echo_api/r/R/form_api.R +++ b/samples/client/echo_api/r/R/form_api.R @@ -157,11 +157,11 @@ FormApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -170,7 +170,7 @@ FormApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -252,11 +252,11 @@ FormApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -265,7 +265,7 @@ FormApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -363,11 +363,11 @@ FormApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -376,7 +376,7 @@ FormApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -388,52 +388,5 @@ FormApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/echo_api/r/R/header_api.R b/samples/client/echo_api/r/R/header_api.R index 1beb91952eda..c20fab0df874 100644 --- a/samples/client/echo_api/r/R/header_api.R +++ b/samples/client/echo_api/r/R/header_api.R @@ -139,11 +139,11 @@ HeaderApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -152,7 +152,7 @@ HeaderApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -164,52 +164,5 @@ HeaderApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/echo_api/r/R/path_api.R b/samples/client/echo_api/r/R/path_api.R index 8c66c3edb9dd..20456181683f 100644 --- a/samples/client/echo_api/r/R/path_api.R +++ b/samples/client/echo_api/r/R/path_api.R @@ -157,11 +157,11 @@ PathApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -170,7 +170,7 @@ PathApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -182,52 +182,5 @@ PathApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/echo_api/r/R/query_api.R b/samples/client/echo_api/r/R/query_api.R index b6887ce25fe8..4fb98bd8008c 100644 --- a/samples/client/echo_api/r/R/query_api.R +++ b/samples/client/echo_api/r/R/query_api.R @@ -254,11 +254,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -267,7 +267,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -356,11 +356,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -369,7 +369,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -458,11 +458,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -471,7 +471,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -550,11 +550,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -563,7 +563,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -642,11 +642,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -655,7 +655,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -735,11 +735,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -748,7 +748,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -828,11 +828,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -841,7 +841,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -920,11 +920,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -933,7 +933,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -1012,11 +1012,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -1025,7 +1025,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -1104,11 +1104,11 @@ QueryApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { stop("Failed to deserialize response") } @@ -1117,7 +1117,7 @@ QueryApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { ApiResponse$new(paste("Server returned ", local_var_resp$status_code, " response status code."), local_var_resp) } else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) { @@ -1129,52 +1129,5 @@ QueryApi <- R6::R6Class( return(local_var_resp) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("openapi"))) - } ) ) diff --git a/samples/client/petstore/R-httr2-wrapper/R/api_client.R b/samples/client/petstore/R-httr2-wrapper/R/api_client.R index 381f135c0898..9c5bba767310 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/api_client.R +++ b/samples/client/petstore/R-httr2-wrapper/R/api_client.R @@ -46,7 +46,7 @@ ApiClient <- R6::R6Class( "ApiClient", public = list( # base path of all requests - base_path = "http://localhost/v2", + base_path = "http://petstore.swagger.io/v2", # user agent in the HTTP request user_agent = "PetstoreAgent", # default headers in the HTTP request @@ -432,6 +432,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("petstore"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/samples/client/petstore/R-httr2-wrapper/R/api_exception.R b/samples/client/petstore/R-httr2-wrapper/R/api_exception.R index 1ce92734694f..fcb62402484c 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/api_exception.R +++ b/samples/client/petstore/R-httr2-wrapper/R/api_exception.R @@ -33,14 +33,14 @@ ApiException <- R6::R6Class( initialize = function(status = NULL, reason = NULL, http_response = NULL) { if (!is.null(http_response)) { self$status <- http_response$status_code - errorMsg <- http_response$ResponseAsText() + errorMsg <- http_response$response_as_text() if (is.null(errorMsg) || errorMsg == "") { errorMsg <- "Api exception encountered. No details given." } self$body <- errorMsg self$headers <- http_response$headers self$reason <- http_response$http_status_desc - self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$ResponseAsText()) + self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$response_as_text()) } else { self$status <- status self$reason <- reason diff --git a/samples/client/petstore/R-httr2-wrapper/R/api_response.R b/samples/client/petstore/R-httr2-wrapper/R/api_response.R index 9be423a27155..605553d98a1e 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/api_response.R +++ b/samples/client/petstore/R-httr2-wrapper/R/api_response.R @@ -51,7 +51,7 @@ ApiResponse <- R6::R6Class( #' #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. - ResponseAsText = function(from_encoding = "", to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { if (is.null(self$response)) { self$response <- charToRaw(jsonlite::toJSON("NULL")) } diff --git a/samples/client/petstore/R-httr2-wrapper/R/fake_api.R b/samples/client/petstore/R-httr2-wrapper/R/fake_api.R index a34df15bd19f..5dae105c228b 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/fake_api.R +++ b/samples/client/petstore/R-httr2-wrapper/R/fake_api.R @@ -256,11 +256,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -271,7 +271,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -375,11 +375,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -390,7 +390,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -491,7 +491,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -596,7 +596,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -720,7 +720,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -745,52 +745,5 @@ FakeApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2-wrapper/R/pet_api.R b/samples/client/petstore/R-httr2-wrapper/R/pet_api.R index 70085847e00b..b19e01d9d8ab 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/pet_api.R +++ b/samples/client/petstore/R-httr2-wrapper/R/pet_api.R @@ -466,11 +466,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -481,7 +481,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -590,7 +590,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -702,11 +702,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -717,7 +717,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -817,11 +817,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -832,7 +832,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -937,11 +937,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -952,7 +952,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1068,11 +1068,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1083,7 +1083,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1197,11 +1197,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1212,7 +1212,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1318,11 +1318,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1333,7 +1333,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1442,7 +1442,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1554,11 +1554,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "ModelApiResponse"), + self$api_client$DeserializeResponse(local_var_resp, "ModelApiResponse"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1569,7 +1569,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1594,52 +1594,5 @@ PetApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2-wrapper/R/store_api.R b/samples/client/petstore/R-httr2-wrapper/R/store_api.R index 6b426cc85d0b..e49457700c0b 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/store_api.R +++ b/samples/client/petstore/R-httr2-wrapper/R/store_api.R @@ -236,7 +236,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -327,11 +327,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "map(integer)"), + self$api_client$DeserializeResponse(local_var_resp, "map(integer)"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -342,7 +342,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -455,11 +455,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -470,7 +470,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -573,11 +573,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -588,7 +588,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -613,52 +613,5 @@ StoreApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2-wrapper/R/user_api.R b/samples/client/petstore/R-httr2-wrapper/R/user_api.R index 49193740c889..4c938c7ee3ad 100644 --- a/samples/client/petstore/R-httr2-wrapper/R/user_api.R +++ b/samples/client/petstore/R-httr2-wrapper/R/user_api.R @@ -351,7 +351,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -461,7 +461,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -571,7 +571,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -676,7 +676,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -777,11 +777,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -792,7 +792,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -909,11 +909,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -924,7 +924,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1015,7 +1015,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1136,7 +1136,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1161,52 +1161,5 @@ UserApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2/R/api_client.R b/samples/client/petstore/R-httr2/R/api_client.R index 32aa262f5545..9c5bba767310 100644 --- a/samples/client/petstore/R-httr2/R/api_client.R +++ b/samples/client/petstore/R-httr2/R/api_client.R @@ -432,6 +432,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("petstore"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/samples/client/petstore/R-httr2/R/api_exception.R b/samples/client/petstore/R-httr2/R/api_exception.R index 1ce92734694f..fcb62402484c 100644 --- a/samples/client/petstore/R-httr2/R/api_exception.R +++ b/samples/client/petstore/R-httr2/R/api_exception.R @@ -33,14 +33,14 @@ ApiException <- R6::R6Class( initialize = function(status = NULL, reason = NULL, http_response = NULL) { if (!is.null(http_response)) { self$status <- http_response$status_code - errorMsg <- http_response$ResponseAsText() + errorMsg <- http_response$response_as_text() if (is.null(errorMsg) || errorMsg == "") { errorMsg <- "Api exception encountered. No details given." } self$body <- errorMsg self$headers <- http_response$headers self$reason <- http_response$http_status_desc - self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$ResponseAsText()) + self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$response_as_text()) } else { self$status <- status self$reason <- reason diff --git a/samples/client/petstore/R-httr2/R/api_response.R b/samples/client/petstore/R-httr2/R/api_response.R index 9be423a27155..605553d98a1e 100644 --- a/samples/client/petstore/R-httr2/R/api_response.R +++ b/samples/client/petstore/R-httr2/R/api_response.R @@ -51,7 +51,7 @@ ApiResponse <- R6::R6Class( #' #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. - ResponseAsText = function(from_encoding = "", to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { if (is.null(self$response)) { self$response <- charToRaw(jsonlite::toJSON("NULL")) } diff --git a/samples/client/petstore/R-httr2/R/fake_api.R b/samples/client/petstore/R-httr2/R/fake_api.R index 382a2b4e7c59..175540cf5ee6 100644 --- a/samples/client/petstore/R-httr2/R/fake_api.R +++ b/samples/client/petstore/R-httr2/R/fake_api.R @@ -256,11 +256,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -271,7 +271,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -375,11 +375,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -390,7 +390,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -491,7 +491,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -596,7 +596,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -720,7 +720,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -745,52 +745,5 @@ FakeApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2/R/pet_api.R b/samples/client/petstore/R-httr2/R/pet_api.R index 3b72202cecb9..f832c2ea5cbc 100644 --- a/samples/client/petstore/R-httr2/R/pet_api.R +++ b/samples/client/petstore/R-httr2/R/pet_api.R @@ -466,11 +466,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -481,7 +481,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -590,7 +590,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -702,11 +702,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -717,7 +717,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -817,11 +817,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -832,7 +832,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -937,11 +937,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -952,7 +952,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1068,11 +1068,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1083,7 +1083,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1197,11 +1197,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1212,7 +1212,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1318,11 +1318,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1333,7 +1333,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1442,7 +1442,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1554,11 +1554,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "ModelApiResponse"), + self$api_client$DeserializeResponse(local_var_resp, "ModelApiResponse"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1569,7 +1569,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1594,52 +1594,5 @@ PetApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2/R/store_api.R b/samples/client/petstore/R-httr2/R/store_api.R index 84dd7433375c..f445fabf3c2b 100644 --- a/samples/client/petstore/R-httr2/R/store_api.R +++ b/samples/client/petstore/R-httr2/R/store_api.R @@ -236,7 +236,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -327,11 +327,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "map(integer)"), + self$api_client$DeserializeResponse(local_var_resp, "map(integer)"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -342,7 +342,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -455,11 +455,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -470,7 +470,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -573,11 +573,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -588,7 +588,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -613,52 +613,5 @@ StoreApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R-httr2/R/user_api.R b/samples/client/petstore/R-httr2/R/user_api.R index 8ec299404fbd..9487ca7835b0 100644 --- a/samples/client/petstore/R-httr2/R/user_api.R +++ b/samples/client/petstore/R-httr2/R/user_api.R @@ -351,7 +351,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -461,7 +461,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -571,7 +571,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -676,7 +676,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -777,11 +777,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -792,7 +792,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -909,11 +909,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -924,7 +924,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1015,7 +1015,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1136,7 +1136,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1161,52 +1161,5 @@ UserApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index 631827298a30..638216216e37 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -411,6 +411,52 @@ ApiClient <- R6::R6Class( # not json mime type, simply return the first one return(headers[1]) } + }, + + #' @description + #' Deserialize the response + #' + #' @param local_var_resp The API response + #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. + #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. + DeserializeResponse = function(local_var_resp, return_type = NULL) { + text <- local_var_resp$response_as_text() + if (is.na(text)) { + return(local_var_resp$response) + } else if (is.null(return_type)) { + return(text) + } + return(self$deserialize(text, return_type, loadNamespace("petstore"))) + }, + + #' @description + #' Write response to a file + #' + #' The function will write out data. + #' + #' 1. If binary data is detected it will use `writeBin` + #' 2. If the raw response is coercable to text, the text will be written to a file + #' 3. If the raw response is not coercable to text, the raw response will be written + #' + #' @param local_var_resp The API response + #' @param file The name of the data file to save the result + WriteFile = function(local_var_resp, file) { + if (self$IsBinary(local_var_resp$response)) { + writeBin(local_var_resp$response, file) + } else { + response <- self$DeserializeResponse(local_var_resp) + base::write(response, file) + } + }, + + #' @description + #' Check response for binary content + #' + #' @param local_var_resp The API response + IsBinary = function(x) { + # ref: https://stackoverflow.com/a/17098690/1785752 + b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) + return(max(b) > 128) } ) ) diff --git a/samples/client/petstore/R/R/api_exception.R b/samples/client/petstore/R/R/api_exception.R index 1ce92734694f..fcb62402484c 100644 --- a/samples/client/petstore/R/R/api_exception.R +++ b/samples/client/petstore/R/R/api_exception.R @@ -33,14 +33,14 @@ ApiException <- R6::R6Class( initialize = function(status = NULL, reason = NULL, http_response = NULL) { if (!is.null(http_response)) { self$status <- http_response$status_code - errorMsg <- http_response$ResponseAsText() + errorMsg <- http_response$response_as_text() if (is.null(errorMsg) || errorMsg == "") { errorMsg <- "Api exception encountered. No details given." } self$body <- errorMsg self$headers <- http_response$headers self$reason <- http_response$http_status_desc - self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$ResponseAsText()) + self$error_object <- ModelApiResponse$new()$fromJSONString(http_response$response_as_text()) } else { self$status <- status self$reason <- reason diff --git a/samples/client/petstore/R/R/api_response.R b/samples/client/petstore/R/R/api_response.R index 9be423a27155..605553d98a1e 100644 --- a/samples/client/petstore/R/R/api_response.R +++ b/samples/client/petstore/R/R/api_response.R @@ -51,7 +51,7 @@ ApiResponse <- R6::R6Class( #' #' @param from_encoding The encoding of the raw response. #' @param to_encoding The target encoding of the return value. - ResponseAsText = function(from_encoding = "", to_encoding = "UTF-8") { + response_as_text = function(from_encoding = "", to_encoding = "UTF-8") { if (is.null(self$response)) { self$response <- charToRaw(jsonlite::toJSON("NULL")) } diff --git a/samples/client/petstore/R/R/fake_api.R b/samples/client/petstore/R/R/fake_api.R index 7cc676dabae0..ee88e7eba789 100644 --- a/samples/client/petstore/R/R/fake_api.R +++ b/samples/client/petstore/R/R/fake_api.R @@ -256,11 +256,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -271,7 +271,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -375,11 +375,11 @@ FakeApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -390,7 +390,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -491,7 +491,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -596,7 +596,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -720,7 +720,7 @@ FakeApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -745,52 +745,5 @@ FakeApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R/R/pet_api.R b/samples/client/petstore/R/R/pet_api.R index fb29ef7872a7..a19e01486290 100644 --- a/samples/client/petstore/R/R/pet_api.R +++ b/samples/client/petstore/R/R/pet_api.R @@ -466,11 +466,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -481,7 +481,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -590,7 +590,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -702,11 +702,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -717,7 +717,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -817,11 +817,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "array[Pet]"), + self$api_client$DeserializeResponse(local_var_resp, "array[Pet]"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -832,7 +832,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -937,11 +937,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -952,7 +952,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1068,11 +1068,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1083,7 +1083,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1197,11 +1197,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1212,7 +1212,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1318,11 +1318,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Pet"), + self$api_client$DeserializeResponse(local_var_resp, "Pet"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1333,7 +1333,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1442,7 +1442,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1554,11 +1554,11 @@ PetApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "ModelApiResponse"), + self$api_client$DeserializeResponse(local_var_resp, "ModelApiResponse"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -1569,7 +1569,7 @@ PetApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1594,52 +1594,5 @@ PetApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R/R/store_api.R b/samples/client/petstore/R/R/store_api.R index 3ed6e47dd169..6889cdf7aa70 100644 --- a/samples/client/petstore/R/R/store_api.R +++ b/samples/client/petstore/R/R/store_api.R @@ -236,7 +236,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -327,11 +327,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "map(integer)"), + self$api_client$DeserializeResponse(local_var_resp, "map(integer)"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -342,7 +342,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -455,11 +455,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -470,7 +470,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -573,11 +573,11 @@ StoreApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "Order"), + self$api_client$DeserializeResponse(local_var_resp, "Order"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -588,7 +588,7 @@ StoreApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -613,52 +613,5 @@ StoreApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) ) diff --git a/samples/client/petstore/R/R/user_api.R b/samples/client/petstore/R/R/user_api.R index 1896792fb0e4..389cc69b57e4 100644 --- a/samples/client/petstore/R/R/user_api.R +++ b/samples/client/petstore/R/R/user_api.R @@ -351,7 +351,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -461,7 +461,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -571,7 +571,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -676,7 +676,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -777,11 +777,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "User"), + self$api_client$DeserializeResponse(local_var_resp, "User"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -792,7 +792,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -909,11 +909,11 @@ UserApi <- R6::R6Class( if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { # save response in a file if (!is.null(data_file)) { - private$WriteFile(local_var_resp, data_file) + self$api_client$WriteFile(local_var_resp, data_file) } deserialized_resp_obj <- tryCatch( - private$Deserialize(local_var_resp, "character"), + self$api_client$DeserializeResponse(local_var_resp, "character"), error = function(e) { rlang::abort(message = "Failed to deserialize response", .subclass = "ApiException", @@ -924,7 +924,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1015,7 +1015,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1136,7 +1136,7 @@ UserApi <- R6::R6Class( return(local_var_resp) } - local_var_error_msg <- local_var_resp$ResponseAsText() + local_var_error_msg <- local_var_resp$response_as_text() if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { if (local_var_error_msg == "") { @@ -1161,52 +1161,5 @@ UserApi <- R6::R6Class( ApiException = ApiException$new(http_response = local_var_resp)) } } - ), - private = list( - #' @description - #' Write response to a file - #' - #' The function will write out data. - #' - #' 1. If binary data is detected it will use `writeBin` - #' 2. If the raw response is coercable to text, the text will be written to a file - #' 3. If the raw response is not coercable to text, the raw response will be written - #' - #' @param local_var_resp The API response - #' @param file The name of the data file to save the result - WriteFile = function(local_var_resp, file) { - if (private$IsBinary(local_var_resp$response)) { - writeBin(local_var_resp$response, file) - } else { - response <- private$Deserialize(local_var_resp) - base::write(response, file) - } - }, - - #' @description - #' Check response for binary content - #' - #' @param local_var_resp The API response - IsBinary = function(x) { - # ref: https://stackoverflow.com/a/17098690/1785752 - b <- readBin(x, "int", n = 1000, size=1, signed=FALSE) - return(max(b) > 128) - }, - - #' @description - #' Deserialize the response - #' - #' @param local_var_resp The API response - #' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is. - #' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne. - Deserialize = function(local_var_resp, return_type = NULL) { - text <- local_var_resp$ResponseAsText() - if (is.na(text)) { - return(local_var_resp$response) - } else if (is.null(return_type)) { - return(text) - } - return(self$api_client$deserialize(text, return_type, loadNamespace("petstore"))) - } ) )