Skip to content

Commit 17f9a7c

Browse files
committed
cleanup
1 parent b3022dd commit 17f9a7c

File tree

19 files changed

+793
-302
lines changed

19 files changed

+793
-302
lines changed

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -547,24 +547,21 @@
547547
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
548548
{{#returnType}}
549549
{{#isPrimitiveType}}
550-
local_var_content <- local_var_resp$response
551-
local_var_resp, "text", encoding = "UTF-8", simplifyVector = FALSE
552-
)
553550
# save response in a file
554551
if (!is.null(data_file)) {
555-
private$WriteFile(local_var_content, data_file, local_var_accepts)
552+
private$WriteFile(local_var_resp, data_file)
556553
}
557554
558555
ApiResponse$new(content,resp)
559556
{{/isPrimitiveType}}
560557
{{^isPrimitiveType}}
561558
# save response in a file
562559
if (!is.null(data_file)) {
563-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
560+
private$WriteFile(local_var_resp, data_file)
564561
}
565562
566563
deserialized_resp_obj <- tryCatch(
567-
private$Deserialize(local_var_resp),
564+
private$Deserialize(local_var_resp, "{{returnType}}"),
568565
error = function(e) {
569566
{{#useDefaultExceptionHandling}}
570567
stop("Failed to deserialize response")
@@ -646,24 +643,50 @@
646643
{{/operation}}
647644
),
648645
private = list(
649-
WriteFile = function(x, file, accepts) {
650-
if (private$IsBinary(accepts)) {
651-
writeBin(x, file)
646+
#' @description
647+
#' Write response to a file
648+
#'
649+
#' The function will write out data.
650+
#'
651+
#' 1. If binary data is detected it will use `writeBin`
652+
#' 2. If the raw response is coercable to text, the text will be written to a file
653+
#' 3. If the raw response is not coercable to text, the raw response will be written
654+
#'
655+
#' @param local_var_resp The API response
656+
#' @param file The name of the data file to save the result
657+
WriteFile = function(local_var_resp, file) {
658+
if (private$IsBinary(local_var_resp$response)) {
659+
writeBin(local_var_resp$response, file)
652660
} else {
653-
base::write(x, file)
661+
response <- private$Deserialize(local_var_resp)
662+
base::write(response, file)
654663
}
655664
},
656665
657-
IsBinary = function(accepts) {
658-
return(any(grepl("gzip", as.character(accepts))))
666+
#' @description
667+
#' Check response for binary content
668+
#'
669+
#' @param local_var_resp The API response
670+
IsBinary = function(x) {
671+
# ref: https://stackoverflow.com/a/17098690/1785752
672+
b <- readBin(x, "int", n = 1000, size=1, signed=FALSE)
673+
return(max(b) > 128)
659674
},
660675
661-
Deserialize = function(local_var_resp) {
676+
#' @description
677+
#' Deserialize the response
678+
#'
679+
#' @param local_var_resp The API response
680+
#' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is.
681+
#' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne.
682+
Deserialize = function(local_var_resp, return_type = NULL) {
662683
text <- local_var_resp$response_as_text()
663684
if (is.na(text)) {
664685
return(local_var_resp$response)
686+
} else if (is.null(return_type)) {
687+
return(text)
665688
}
666-
return(self$api_client$deserialize(text, "object", loadNamespace("k8s.client")))
689+
return(self$api_client$deserialize(text, return_type, loadNamespace("{{packageName}}")))
667690
}
668691
)
669692
)

samples/client/echo_api/r/R/auth_api.R

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ AuthApi <- R6::R6Class(
137137
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
138138
# save response in a file
139139
if (!is.null(data_file)) {
140-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
140+
private$WriteFile(local_var_resp, data_file)
141141
}
142142

143143
deserialized_resp_obj <- tryCatch(
144-
private$Deserialize(local_var_resp),
144+
private$Deserialize(local_var_resp, "character"),
145145
error = function(e) {
146146
stop("Failed to deserialize response")
147147
}
@@ -229,11 +229,11 @@ AuthApi <- R6::R6Class(
229229
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
230230
# save response in a file
231231
if (!is.null(data_file)) {
232-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
232+
private$WriteFile(local_var_resp, data_file)
233233
}
234234

235235
deserialized_resp_obj <- tryCatch(
236-
private$Deserialize(local_var_resp),
236+
private$Deserialize(local_var_resp, "character"),
237237
error = function(e) {
238238
stop("Failed to deserialize response")
239239
}
@@ -253,24 +253,50 @@ AuthApi <- R6::R6Class(
253253
}
254254
),
255255
private = list(
256-
WriteFile = function(x, file, accepts) {
257-
if (private$IsBinary(accepts)) {
258-
writeBin(x, file)
256+
#' @description
257+
#' Write response to a file
258+
#'
259+
#' The function will write out data.
260+
#'
261+
#' 1. If binary data is detected it will use `writeBin`
262+
#' 2. If the raw response is coercable to text, the text will be written to a file
263+
#' 3. If the raw response is not coercable to text, the raw response will be written
264+
#'
265+
#' @param local_var_resp The API response
266+
#' @param file The name of the data file to save the result
267+
WriteFile = function(local_var_resp, file) {
268+
if (private$IsBinary(local_var_resp$response)) {
269+
writeBin(local_var_resp$response, file)
259270
} else {
260-
base::write(x, file)
271+
response <- private$Deserialize(local_var_resp)
272+
base::write(response, file)
261273
}
262274
},
263275

264-
IsBinary = function(accepts) {
265-
return(any(grepl("gzip", as.character(accepts))))
276+
#' @description
277+
#' Check response for binary content
278+
#'
279+
#' @param local_var_resp The API response
280+
IsBinary = function(x) {
281+
# ref: https://stackoverflow.com/a/17098690/1785752
282+
b <- readBin(x, "int", n = 1000, size=1, signed=FALSE)
283+
return(max(b) > 128)
266284
},
267285

268-
Deserialize = function(local_var_resp) {
286+
#' @description
287+
#' Deserialize the response
288+
#'
289+
#' @param local_var_resp The API response
290+
#' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is.
291+
#' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne.
292+
Deserialize = function(local_var_resp, return_type = NULL) {
269293
text <- local_var_resp$response_as_text()
270294
if (is.na(text)) {
271295
return(local_var_resp$response)
296+
} else if (is.null(return_type)) {
297+
return(text)
272298
}
273-
return(self$api_client$deserialize(text, "object", loadNamespace("k8s.client")))
299+
return(self$api_client$deserialize(text, return_type, loadNamespace("openapi")))
274300
}
275301
)
276302
)

samples/client/echo_api/r/R/body_api.R

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ BodyApi <- R6::R6Class(
239239
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
240240
# save response in a file
241241
if (!is.null(data_file)) {
242-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
242+
private$WriteFile(local_var_resp, data_file)
243243
}
244244

245245
deserialized_resp_obj <- tryCatch(
246-
private$Deserialize(local_var_resp),
246+
private$Deserialize(local_var_resp, "data.frame"),
247247
error = function(e) {
248248
stop("Failed to deserialize response")
249249
}
@@ -336,11 +336,11 @@ BodyApi <- R6::R6Class(
336336
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
337337
# save response in a file
338338
if (!is.null(data_file)) {
339-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
339+
private$WriteFile(local_var_resp, data_file)
340340
}
341341

342342
deserialized_resp_obj <- tryCatch(
343-
private$Deserialize(local_var_resp),
343+
private$Deserialize(local_var_resp, "character"),
344344
error = function(e) {
345345
stop("Failed to deserialize response")
346346
}
@@ -432,11 +432,11 @@ BodyApi <- R6::R6Class(
432432
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
433433
# save response in a file
434434
if (!is.null(data_file)) {
435-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
435+
private$WriteFile(local_var_resp, data_file)
436436
}
437437

438438
deserialized_resp_obj <- tryCatch(
439-
private$Deserialize(local_var_resp),
439+
private$Deserialize(local_var_resp, "character"),
440440
error = function(e) {
441441
stop("Failed to deserialize response")
442442
}
@@ -524,11 +524,11 @@ BodyApi <- R6::R6Class(
524524
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
525525
# save response in a file
526526
if (!is.null(data_file)) {
527-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
527+
private$WriteFile(local_var_resp, data_file)
528528
}
529529

530530
deserialized_resp_obj <- tryCatch(
531-
private$Deserialize(local_var_resp),
531+
private$Deserialize(local_var_resp, "character"),
532532
error = function(e) {
533533
stop("Failed to deserialize response")
534534
}
@@ -621,11 +621,11 @@ BodyApi <- R6::R6Class(
621621
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
622622
# save response in a file
623623
if (!is.null(data_file)) {
624-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
624+
private$WriteFile(local_var_resp, data_file)
625625
}
626626

627627
deserialized_resp_obj <- tryCatch(
628-
private$Deserialize(local_var_resp),
628+
private$Deserialize(local_var_resp, "Pet"),
629629
error = function(e) {
630630
stop("Failed to deserialize response")
631631
}
@@ -718,11 +718,11 @@ BodyApi <- R6::R6Class(
718718
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
719719
# save response in a file
720720
if (!is.null(data_file)) {
721-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
721+
private$WriteFile(local_var_resp, data_file)
722722
}
723723

724724
deserialized_resp_obj <- tryCatch(
725-
private$Deserialize(local_var_resp),
725+
private$Deserialize(local_var_resp, "character"),
726726
error = function(e) {
727727
stop("Failed to deserialize response")
728728
}
@@ -815,11 +815,11 @@ BodyApi <- R6::R6Class(
815815
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
816816
# save response in a file
817817
if (!is.null(data_file)) {
818-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
818+
private$WriteFile(local_var_resp, data_file)
819819
}
820820

821821
deserialized_resp_obj <- tryCatch(
822-
private$Deserialize(local_var_resp),
822+
private$Deserialize(local_var_resp, "Pet"),
823823
error = function(e) {
824824
stop("Failed to deserialize response")
825825
}
@@ -912,11 +912,11 @@ BodyApi <- R6::R6Class(
912912
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
913913
# save response in a file
914914
if (!is.null(data_file)) {
915-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
915+
private$WriteFile(local_var_resp, data_file)
916916
}
917917

918918
deserialized_resp_obj <- tryCatch(
919-
private$Deserialize(local_var_resp),
919+
private$Deserialize(local_var_resp, "character"),
920920
error = function(e) {
921921
stop("Failed to deserialize response")
922922
}
@@ -1009,11 +1009,11 @@ BodyApi <- R6::R6Class(
10091009
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
10101010
# save response in a file
10111011
if (!is.null(data_file)) {
1012-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
1012+
private$WriteFile(local_var_resp, data_file)
10131013
}
10141014

10151015
deserialized_resp_obj <- tryCatch(
1016-
private$Deserialize(local_var_resp),
1016+
private$Deserialize(local_var_resp, "StringEnumRef"),
10171017
error = function(e) {
10181018
stop("Failed to deserialize response")
10191019
}
@@ -1106,11 +1106,11 @@ BodyApi <- R6::R6Class(
11061106
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
11071107
# save response in a file
11081108
if (!is.null(data_file)) {
1109-
private$WriteFile(local_var_resp$response, data_file, local_var_accepts)
1109+
private$WriteFile(local_var_resp, data_file)
11101110
}
11111111

11121112
deserialized_resp_obj <- tryCatch(
1113-
private$Deserialize(local_var_resp),
1113+
private$Deserialize(local_var_resp, "character"),
11141114
error = function(e) {
11151115
stop("Failed to deserialize response")
11161116
}
@@ -1130,24 +1130,50 @@ BodyApi <- R6::R6Class(
11301130
}
11311131
),
11321132
private = list(
1133-
WriteFile = function(x, file, accepts) {
1134-
if (private$IsBinary(accepts)) {
1135-
writeBin(x, file)
1133+
#' @description
1134+
#' Write response to a file
1135+
#'
1136+
#' The function will write out data.
1137+
#'
1138+
#' 1. If binary data is detected it will use `writeBin`
1139+
#' 2. If the raw response is coercable to text, the text will be written to a file
1140+
#' 3. If the raw response is not coercable to text, the raw response will be written
1141+
#'
1142+
#' @param local_var_resp The API response
1143+
#' @param file The name of the data file to save the result
1144+
WriteFile = function(local_var_resp, file) {
1145+
if (private$IsBinary(local_var_resp$response)) {
1146+
writeBin(local_var_resp$response, file)
11361147
} else {
1137-
base::write(x, file)
1148+
response <- private$Deserialize(local_var_resp)
1149+
base::write(response, file)
11381150
}
11391151
},
11401152

1141-
IsBinary = function(accepts) {
1142-
return(any(grepl("gzip", as.character(accepts))))
1153+
#' @description
1154+
#' Check response for binary content
1155+
#'
1156+
#' @param local_var_resp The API response
1157+
IsBinary = function(x) {
1158+
# ref: https://stackoverflow.com/a/17098690/1785752
1159+
b <- readBin(x, "int", n = 1000, size=1, signed=FALSE)
1160+
return(max(b) > 128)
11431161
},
11441162

1145-
Deserialize = function(local_var_resp) {
1163+
#' @description
1164+
#' Deserialize the response
1165+
#'
1166+
#' @param local_var_resp The API response
1167+
#' @param return_type The target return type for the endpoint (e.g., `"object"`). If `NULL` text will be left as-is.
1168+
#' @return If the raw response is corecable to text, return the text. Otherwise return the raw resposne.
1169+
Deserialize = function(local_var_resp, return_type = NULL) {
11461170
text <- local_var_resp$response_as_text()
11471171
if (is.na(text)) {
11481172
return(local_var_resp$response)
1173+
} else if (is.null(return_type)) {
1174+
return(text)
11491175
}
1150-
return(self$api_client$deserialize(text, "object", loadNamespace("k8s.client")))
1176+
return(self$api_client$deserialize(text, return_type, loadNamespace("openapi")))
11511177
}
11521178
)
11531179
)

0 commit comments

Comments
 (0)