-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[R] improve binary/text response handling #20131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
b3022dd
17f9a7c
3c847fb
47c9dca
09a5f4e
88fc70d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -153,13 +153,17 @@ | |
|
|
||
| {{/vendorExtensions.x-streaming}} | ||
| if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) { | ||
| local_var_response$content | ||
| if (is.raw(local_var_response$content)) { | ||
| return(local_var_response) | ||
| } else { | ||
| return(local_var_response$content) | ||
| } | ||
| } else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) { | ||
| local_var_response | ||
| return(local_var_response) | ||
| } else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) { | ||
| local_var_response | ||
| return(local_var_response) | ||
| } else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) { | ||
| local_var_response | ||
| return(local_var_response) | ||
| } | ||
| }, | ||
|
|
||
|
|
@@ -543,24 +547,21 @@ | |
| if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) { | ||
| {{#returnType}} | ||
| {{#isPrimitiveType}} | ||
| local_var_content <- local_var_resp$response | ||
| local_var_resp, "text", encoding = "UTF-8", simplifyVector = FALSE | ||
| ) | ||
mattpollock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # save response in a file | ||
| if (!is.null(data_file)) { | ||
| write(local_var_content, data_file) | ||
| private$WriteFile(local_var_resp, data_file) | ||
mattpollock marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| ApiResponse$new(content,resp) | ||
| {{/isPrimitiveType}} | ||
| {{^isPrimitiveType}} | ||
| # save response in a file | ||
| if (!is.null(data_file)) { | ||
| write(local_var_resp$response, data_file) | ||
| private$WriteFile(local_var_resp, data_file) | ||
| } | ||
|
|
||
| deserialized_resp_obj <- tryCatch( | ||
| self$api_client$deserialize(local_var_resp$response_as_text(), "{{returnType}}", loadNamespace("{{packageName}}")), | ||
| private$Deserialize(local_var_resp, "{{returnType}}"), | ||
mattpollock marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| error = function(e) { | ||
| {{#useDefaultExceptionHandling}} | ||
| stop("Failed to deserialize response") | ||
|
|
@@ -579,7 +580,7 @@ | |
| {{! Returning the ApiResponse object with NULL object when the endpoint doesn't return anything}} | ||
| local_var_resp$content <- NULL | ||
| {{/returnType}} | ||
| local_var_resp | ||
| return(local_var_resp) | ||
| } else if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) { | ||
| {{#returnExceptionOnFailure}} | ||
| local_var_error_msg <- local_var_resp$response | ||
|
|
@@ -635,11 +636,58 @@ | |
| if (is.null(local_var_resp$response) || local_var_resp$response == "") { | ||
| local_var_resp$response <- "API server error" | ||
| } | ||
| local_var_resp | ||
| return(local_var_resp) | ||
| {{/returnExceptionOnFailure}} | ||
| } | ||
| }{{^-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$response_as_text() | ||
| 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}} | ||
Uh oh!
There was an error while loading. Please reload this page.