Skip to content
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

[R][httr2] Fix form parameter support #13057

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@
args <- list(...)
query_params <- list()
header_params <- c()
form_params <- list()
file_params <- list()
local_var_body <- NULL

{{#requiredParams}}
if (missing(`{{paramName}}`)) {
Expand All @@ -255,32 +258,29 @@

{{/headerParams}}
{{#queryParams}}
query_params["{{baseName}}"] <- {{paramName}}
query_params["{{baseName}}"] <- `{{paramName}}`

{{/queryParams}}
{{#hasFormParams}}
local_var_body <- list(
{{#formParams}}
{{^isFile}}
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
{{/isFile}}
{{#isFile}}
{{^isHttr2}}
"{{baseName}}" = httr::upload_file({{paramName}}){{^-last}},{{/-last}}
{{/isHttr2}}
{{#isHttr2}}
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
{{/isHttr2}}
{{/isFile}}
{{/formParams}}
)

{{#formParams}}
{{^isFile}}
form_params["{{baseName}}"] <- `{{paramName}}`
{{/isFile}}
{{#isFile}}
{{^isHttr2}}
file_params["{{baseName}}"] <- httr::upload_file(`{{paramName}}`)
{{/isHttr2}}
{{#isHttr2}}
file_params["{{baseName}}"] <- curl::form_file(`{{paramName}}`)
{{/isHttr2}}
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{#hasBodyParam}}
{{#bodyParams}}
if (!missing(`{{paramName}}`)) {
{{#isArray}}
body.items <- paste(unlist(lapply({{paramName}}, function(param) {
body.items <- paste(unlist(lapply(`{{paramName}}`, function(param) {
param$toJSONString()
})), collapse = ",")
local_var_body <- paste0("[", body.items, "]")
Expand All @@ -294,11 +294,6 @@

{{/bodyParams}}
{{/hasBodyParam}}
{{^hasBodyParam}}
{{^hasFormParams}}
local_var_body <- NULL
{{/hasFormParams}}
{{/hasBodyParam}}
local_var_url_path <- "{{path}}"
{{#hasPathParams}}
{{#pathParams}}
Expand Down Expand Up @@ -358,6 +353,8 @@
method = "{{httpMethod}}",
query_params = query_params,
header_params = header_params,
form_params = form_params,
file_params = file_params,
accepts = local_var_accepts,
content_types = local_var_content_types,
body = local_var_body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ ApiClient <- R6::R6Class(
if (resp$status_code %in% self$retry_status_codes) {
Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1))
resp <- self$Execute(url, method, query_params, header_params,
form_params, file_params, body,
stream_callback = stream_callback, ...)
form_params, file_params, accepts, content_types,
body, stream_callback = stream_callback, ...)
} else {
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,20 @@ ApiClient <- R6::R6Class(
#' @param method HTTP method.
#' @param query_params The query parameters.
#' @param header_params The header parameters.
#' @param form_params The form parameters.
#' @param file_params The form parameters to upload files
#' @param accepts The HTTP accpet headers.
#' @param content_types The HTTP content-type headers.
#' @param body The HTTP request body.
#' @param stream_callback Callback function to process the data stream
#' @param ... Other optional arguments.
#' @return HTTP response
#' @export
CallApi = function(url, method, query_params, header_params, accepts,
content_types, body, stream_callback = NULL, ...) {
CallApi = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {

resp <- self$Execute(url, method, query_params, header_params,
accepts, content_types,
resp <- self$Execute(url, method, query_params, header_params, form_params,
file_params, accepts, content_types,
body, stream_callback = stream_callback, ...)
#status_code <- httr::status_code(resp)

Expand Down Expand Up @@ -174,13 +178,17 @@ ApiClient <- R6::R6Class(
#' @param method HTTP method.
#' @param query_params The query parameters.
#' @param header_params The header parameters.
#' @param form_params The form parameters.
#' @param file_params The form parameters for uploading files.
#' @param accepts The HTTP accpet headers.
#' @param content_types The HTTP content-type headers.
#' @param body The HTTP request body.
#' @param stream_callback Callback function to process data stream
#' @param ... Other optional arguments.
#' @return HTTP response
#' @export
Execute = function(url, method, query_params, header_params, accepts, content_types,
body, stream_callback = NULL, ...) {
Execute = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {
req <- request(url)

## add headers and default headers
Expand Down Expand Up @@ -210,6 +218,21 @@ ApiClient <- R6::R6Class(
}
}

# has file upload?
if (!is.null(file_params) && length(file_params) != 0) {
req <- req %>% req_body_multipart(!!!file_params)

# add form parameters via req_body_multipart
if (!is.null(form_params) && length(form_params) != 0) {
req <- req %>% req_body_multipart(!!!form_params)
}
} else { # no file upload
# add form parameters via req_body_form
if (!is.null(form_params) && length(form_params) != 0) {
req <- req %>% req_body_form(!!!form_params)
}
}

# add body parameters
if (!is.null(body)) {
req <- req %>% req_body_raw(body)
Expand Down
35 changes: 29 additions & 6 deletions samples/client/petstore/R-httr2/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,20 @@ ApiClient <- R6::R6Class(
#' @param method HTTP method.
#' @param query_params The query parameters.
#' @param header_params The header parameters.
#' @param form_params The form parameters.
#' @param file_params The form parameters to upload files
#' @param accepts The HTTP accpet headers.
#' @param content_types The HTTP content-type headers.
#' @param body The HTTP request body.
#' @param stream_callback Callback function to process the data stream
#' @param ... Other optional arguments.
#' @return HTTP response
#' @export
CallApi = function(url, method, query_params, header_params, accepts,
content_types, body, stream_callback = NULL, ...) {
CallApi = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {

resp <- self$Execute(url, method, query_params, header_params,
accepts, content_types,
resp <- self$Execute(url, method, query_params, header_params, form_params,
file_params, accepts, content_types,
body, stream_callback = stream_callback, ...)
#status_code <- httr::status_code(resp)

Expand Down Expand Up @@ -179,13 +183,17 @@ ApiClient <- R6::R6Class(
#' @param method HTTP method.
#' @param query_params The query parameters.
#' @param header_params The header parameters.
#' @param form_params The form parameters.
#' @param file_params The form parameters for uploading files.
#' @param accepts The HTTP accpet headers.
#' @param content_types The HTTP content-type headers.
#' @param body The HTTP request body.
#' @param stream_callback Callback function to process data stream
#' @param ... Other optional arguments.
#' @return HTTP response
#' @export
Execute = function(url, method, query_params, header_params, accepts, content_types,
body, stream_callback = NULL, ...) {
Execute = function(url, method, query_params, header_params, form_params, file_params,
accepts, content_types, body, stream_callback = NULL, ...) {
req <- request(url)

## add headers and default headers
Expand Down Expand Up @@ -215,6 +223,21 @@ ApiClient <- R6::R6Class(
}
}

# has file upload?
if (!is.null(file_params) && length(file_params) != 0) {
req <- req %>% req_body_multipart(!!!file_params)

# add form parameters via req_body_multipart
if (!is.null(form_params) && length(form_params) != 0) {
req <- req %>% req_body_multipart(!!!form_params)
}
} else { # no file upload
# add form parameters via req_body_form
if (!is.null(form_params) && length(form_params) != 0) {
req <- req %>% req_body_form(!!!form_params)
}
}

# add body parameters
if (!is.null(body)) {
req <- req %>% req_body_raw(body)
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/R-httr2/R/fake_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ FakeApi <- R6::R6Class(
args <- list(...)
query_params <- list()
header_params <- c()
form_params <- list()
file_params <- list()
local_var_body <- NULL

if (missing(`dummy`)) {
rlang::abort(message = "Missing required parameter `dummy`.",
Expand All @@ -137,7 +140,6 @@ FakeApi <- R6::R6Class(

header_params["data_file"] <- `var_data_file`

local_var_body <- NULL
local_var_url_path <- "/fake/data_file"

# The Accept request HTTP header
Expand All @@ -150,6 +152,8 @@ FakeApi <- R6::R6Class(
method = "GET",
query_params = query_params,
header_params = header_params,
form_params = form_params,
file_params = file_params,
accepts = local_var_accepts,
content_types = local_var_content_types,
body = local_var_body,
Expand Down
Loading