Skip to content

Commit

Permalink
move private api methods to api-client, revert breaking method name c…
Browse files Browse the repository at this point in the history
…hange
  • Loading branch information
mattpollock committed Nov 21, 2024
1 parent 09a5f4e commit 88fc70d
Show file tree
Hide file tree
Showing 34 changed files with 546 additions and 1,163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
55 changes: 4 additions & 51 deletions modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,19 @@
{{#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)
{{/isPrimitiveType}}
{{^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")
Expand All @@ -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}}
Expand Down Expand Up @@ -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}}
46 changes: 46 additions & 0 deletions modules/openapi-generator/src/main/resources/r/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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
{{#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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
)
46 changes: 46 additions & 0 deletions samples/client/echo_api/r/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
)
2 changes: 1 addition & 1 deletion samples/client/echo_api/r/R/api_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
59 changes: 6 additions & 53 deletions samples/client/echo_api/r/R/auth_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
}
Expand All @@ -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) {
Expand All @@ -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")))
}
)
)
Loading

0 comments on commit 88fc70d

Please sign in to comment.