[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
This commit is contained in:
Ramanth 2019-06-25 11:42:13 +05:30 committed by William Cheng
parent 9518e2a02e
commit f681764067
4 changed files with 47 additions and 16 deletions

View File

@ -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
}

View File

@ -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`.")
}

View File

@ -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`.")
}

View File

@ -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
}