Skip to content

Commit

Permalink
[R] feat(r): Inclusion of useragent, timeout and serialization (OpenA…
Browse files Browse the repository at this point in the history
…PITools#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
  • Loading branch information
Ramanth authored and wing328 committed Jun 25, 2019
1 parent 9518e2a commit f681764
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
6 changes: 6 additions & 0 deletions modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 19 additions & 7 deletions modules/openapi-generator/src/main/resources/r/api_client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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`.")
}
Expand Down
25 changes: 18 additions & 7 deletions samples/client/petstore/R/R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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`.")
}
Expand Down
6 changes: 4 additions & 2 deletions samples/client/petstore/R/R/user_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit f681764

Please sign in to comment.