From f6817640675771dc3537150d7d04c6c1f3fd7ab5 Mon Sep 17 00:00:00 2001 From: Ramanth Date: Tue, 25 Jun 2019 11:42:13 +0530 Subject: [PATCH] [R] feat(r): Inclusion of useragent, timeout and serialization (#3084) * feat(r): Inclusion of useragent timeout and serialization * fix(r): fixing name of timeout parameter * fix(r): fixing unit tests * fix(r): fixing minor issues * fix(r): updated r petstore batch command * fix(r): minor unit test fix * fix(r): refactor of useragent passing and other minor --- .../src/main/resources/r/api.mustache | 6 +++++ .../src/main/resources/r/api_client.mustache | 26 ++++++++++++++----- samples/client/petstore/R/R/api_client.R | 25 +++++++++++++----- samples/client/petstore/R/R/user_api.R | 6 +++-- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/r/api.mustache b/modules/openapi-generator/src/main/resources/r/api.mustache index 955bd104bbe4..b56c7827999c 100644 --- a/modules/openapi-generator/src/main/resources/r/api.mustache +++ b/modules/openapi-generator/src/main/resources/r/api.mustache @@ -79,7 +79,13 @@ {{#hasBodyParam}} {{#bodyParams}} if (!missing(`{{paramName}}`)) { + {{#isListContainer}} + body.items = paste(unlist(lapply({{paramName}}, function(param){param$toJSONString()})), collapse = ",") + body <- paste0('[', body.items, ']') + {{/isListContainer}} + {{^isListContainer}} body <- `{{paramName}}`$toJSONString() + {{/isListContainer}} } else { body <- NULL } 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 585ddee77f36..4a7e01b588ae 100644 --- a/modules/openapi-generator/src/main/resources/r/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/r/api_client.mustache @@ -38,8 +38,10 @@ ApiClient <- R6::R6Class( apiKeys = NULL, # Access token accessToken = NULL, + # Time Out (seconds) + timeout = NULL, # constructor - initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL){ + initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){ if (!is.null(basePath)) { self$basePath <- basePath } @@ -69,22 +71,32 @@ ApiClient <- R6::R6Class( if (!is.null(userAgent)) { self$`userAgent` <- userAgent } + + if (!is.null(timeout)) { + self$timeout <- timeout + } }, CallApi = function(url, method, queryParams, headerParams, body, ...){ headers <- httr::add_headers(c(headerParams, self$defaultHeaders)) + {{! Adding timeout that can be set at the apiClient object level}} + httpTimeout <- NULL + if (!is.null(self$timeout)) { + httpTimeout <- httr::timeout(self$timeout) + } + if (method == "GET") { - httr::GET(url, query = queryParams, headers, ...) + httr::GET(url, query = queryParams, headers, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "POST") { - httr::POST(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::POST(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "PUT") { - httr::PUT(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::PUT(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "PATCH") { - httr::PATCH(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::PATCH(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "HEAD") { - httr::HEAD(url, query = queryParams, headers, ...) + httr::HEAD(url, query = queryParams, headers, httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "DELETE") { - httr::DELETE(url, query = queryParams, headers, ...) + httr::DELETE(url, query = queryParams, headers, httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else { stop("http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`.") } diff --git a/samples/client/petstore/R/R/api_client.R b/samples/client/petstore/R/R/api_client.R index 8d3d0fcd8b26..ec5af87dd39b 100644 --- a/samples/client/petstore/R/R/api_client.R +++ b/samples/client/petstore/R/R/api_client.R @@ -45,8 +45,10 @@ ApiClient <- R6::R6Class( apiKeys = NULL, # Access token accessToken = NULL, + # Time Out (seconds) + timeout = NULL, # constructor - initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL){ + initialize = function(basePath=NULL, userAgent=NULL, defaultHeaders=NULL, username=NULL, password=NULL, apiKeys=NULL, accessToken=NULL, timeout=NULL){ if (!is.null(basePath)) { self$basePath <- basePath } @@ -76,22 +78,31 @@ ApiClient <- R6::R6Class( if (!is.null(userAgent)) { self$`userAgent` <- userAgent } + + if (!is.null(timeout)) { + self$timeout <- timeout + } }, CallApi = function(url, method, queryParams, headerParams, body, ...){ headers <- httr::add_headers(c(headerParams, self$defaultHeaders)) + httpTimeout <- NULL + if (!is.null(self$timeout)) { + httpTimeout <- httr::timeout(self$timeout) + } + if (method == "GET") { - httr::GET(url, query = queryParams, headers, ...) + httr::GET(url, query = queryParams, headers, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "POST") { - httr::POST(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::POST(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "PUT") { - httr::PUT(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::PUT(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "PATCH") { - httr::PATCH(url, query = queryParams, headers, body = body, httr::content_type("application/json"), ...) + httr::PATCH(url, query = queryParams, headers, body = body, httr::content_type("application/json"), httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "HEAD") { - httr::HEAD(url, query = queryParams, headers, ...) + httr::HEAD(url, query = queryParams, headers, httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else if (method == "DELETE") { - httr::DELETE(url, query = queryParams, headers, ...) + httr::DELETE(url, query = queryParams, headers, httpTimeout, httpTimeout, httr::user_agent(self$`userAgent`), ...) } else { stop("http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`.") } diff --git a/samples/client/petstore/R/R/user_api.R b/samples/client/petstore/R/R/user_api.R index a3696c701a9f..656e1acd6839 100644 --- a/samples/client/petstore/R/R/user_api.R +++ b/samples/client/petstore/R/R/user_api.R @@ -122,7 +122,8 @@ UserApi <- R6::R6Class( } if (!missing(`body`)) { - body <- `body`$toJSONString() + body.items = paste(unlist(lapply(body, function(param){param$toJSONString()})), collapse = ",") + body <- paste0('[', body.items, ']') } else { body <- NULL } @@ -166,7 +167,8 @@ UserApi <- R6::R6Class( } if (!missing(`body`)) { - body <- `body`$toJSONString() + body.items = paste(unlist(lapply(body, function(param){param$toJSONString()})), collapse = ",") + body <- paste0('[', body.items, ']') } else { body <- NULL }