diff --git a/samples/client/petstore/R/.Rbuildignore b/samples/client/petstore/R/.Rbuildignore new file mode 100644 index 00000000000..91114bf2f2b --- /dev/null +++ b/samples/client/petstore/R/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/samples/client/petstore/R/.gitignore b/samples/client/petstore/R/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/samples/client/petstore/R/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/samples/client/petstore/R/DESCRIPTION b/samples/client/petstore/R/DESCRIPTION new file mode 100644 index 00000000000..c28d2e6016c --- /dev/null +++ b/samples/client/petstore/R/DESCRIPTION @@ -0,0 +1,13 @@ +Package: petstore +Title: R Package Client for Petstore APIs +Version: 0.0.0.9000 +Authors@R: person("Ramkumar", "Chandrasekaran", email = "ramkumar.november@gmail.com", role = c("aut", "cre")) +Description: R Package Client for Petstore APIs. +Depends: R (>= 3.3.3) +Encoding: UTF-8 +License: MIT + file LICENSE +LazyData: true +Suggests: testthat +Imports: jsonlite, httr, R6 +RoxygenNote: 6.0.1.9000 + diff --git a/samples/client/petstore/R/LICENSE b/samples/client/petstore/R/LICENSE new file mode 100644 index 00000000000..ed2a5abafe6 --- /dev/null +++ b/samples/client/petstore/R/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2017 +COPYRIGHT HOLDER: Ramkumar Chandrasekaran diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE new file mode 100644 index 00000000000..5c892f91298 --- /dev/null +++ b/samples/client/petstore/R/NAMESPACE @@ -0,0 +1,6 @@ +# Generated by roxygen2: do not edit by hand + +export(Element) +export(Pet) +export(PetStoreClient) +export(Response) diff --git a/samples/client/petstore/R/R/pet.R b/samples/client/petstore/R/R/pet.R new file mode 100644 index 00000000000..e51d8a7eb23 --- /dev/null +++ b/samples/client/petstore/R/R/pet.R @@ -0,0 +1,105 @@ +#' Pet Class +#' +#' Pet Class +#' @export +Pet <- R6::R6Class( + 'Pet', + public = list( + id = NULL, + category = NULL, + name = NULL, + photoUrls = NULL, + tags = NULL, + status = NULL, + initialize = function(id,category,name,photoUrls,tags,status){ + if (!missing(id)) { + stopifnot(is.numeric(id), length(id) == 1) + self$id <- id + } + if (!missing(category)) { + stopifnot("Element" %in% class(category), !is.list(category)) + self$category <- category + } + if (!missing(name)) { + stopifnot(is.character(name), length(name) == 1) + self$name <- name + } + if (!missing(photoUrls)) { + stopifnot(is.list(photoUrls), length(photoUrls) != 0) + lapply(photoUrls, function(x) stopifnot(is.character(x))) + self$photoUrls <- photoUrls + } + if (!missing(tags)) { + stopifnot(is.list(tags), length(tags) != 0) + lapply(tags, function(x) stopifnot("Element" %in% class(x), !is.list(x))) + self$tags <- tags + } + if (!missing(status)) { + stopifnot(is.character(status), length(status) == 1) + self$status <- status + } + }, + toJson = function() { + sprintf( + '{ + "id": %d, + "category": { + "id": %d, + "name": "%s" + }, + "name": "%s", + "photoUrls": [%s], + "tags": [%s], + "status": "%s" + }', + self$id, + self$category$id, + self$category$name, + self$name, + lapply(self$photoUrls, function(x) paste(paste0('"', x, '"'), sep=",")), + lapply(self$tags, function(x) paste(x$toJson(), sep=",")), + self$status) + } + ) +) + +#' Element Class +#' +#' Element Class +#' @export +Element <- R6::R6Class( + 'Element', + public = list( + id = NULL, + name = NULL, + initialize = function(id,name){ + if (!missing(id)) { + stopifnot(is.numeric(id), length(id) == 1) + self$id <- id + } + if (!missing(name)) { + stopifnot(is.character(name), length(name) == 1) + self$name <- name + } + }, + toJson = function() { + sprintf('{"id":%d,"name":"%s"}', self$id, self$name) + } + ) +) + +#' Response Class +#' +#' Response Class +#' @export +Response <- R6::R6Class( + 'Response', + public = list( + content = NULL, + response = NULL, + initialize = function(content, response){ + self$content <- content + self$response <- response + } + ) +) \ No newline at end of file diff --git a/samples/client/petstore/R/R/petstore.R b/samples/client/petstore/R/R/petstore.R new file mode 100644 index 00000000000..65a0a686fa4 --- /dev/null +++ b/samples/client/petstore/R/R/petstore.R @@ -0,0 +1,95 @@ +#' Pet Store Client Class +#' +#' Pet Store Client Class +#' @export +PetStoreClient <- R6::R6Class( + 'PetStoreClient', + public = list( + host = NULL, + basePath = NULL, + scheme = NULL, + url = NULL, + initialize = function(host, basePath, scheme){ + self$host <- host + self$basePath <- basePath + self$scheme <- scheme + self$url <- sprintf("%s://%s/%s/pet/", scheme, host, basePath) + }, + getPetById = function(petId){ + resp <- httr::GET(paste0(self$url, petId), httr::add_headers("accept" = "application/json")) + if (httr::http_type(resp) != "application/json") { + stop("API did not return json", call. = FALSE) + } + + if (httr::status_code(resp) == 200) + { + parsed <- jsonlite::fromJSON(httr::content(resp, "text", encoding = "UTF-8"), + simplifyVector = FALSE) + pet <- Pet$new(parsed$id, + Element$new(parsed$category$id, parsed$category$name), + parsed$name, + parsed$photoUrls, + lapply(parsed$tags, function(x) Element$new(x$id, x$name)), + parsed$status) + Response$new(pet, resp) + } + else if(httr::status_code(resp) == 400) + { + Response$new("Invalid ID supplied", resp) + } + else if(httr::status_code(resp) == 404) + { + Response$new("Pet not found", resp) + } + else + { + Response$new("Unexpected response status code", resp) + } + }, + updatePetWithForm = function(petId, name, status){ + resp <- httr::POST(paste0(self$url, petId), + httr::add_headers("accept" = "application/json", + "content-type" = "application/x-www-form-urlencoded"), + body = list(name = name, + status = status)) + if (httr::http_type(resp) != "application/json") { + stop("API did not return json", call. = FALSE) + } + + if (httr::status_code(resp) == 200) + { + Response$new("Pet updated", resp) + } + else if(httr::status_code(resp) == 405) + { + Response$new("Invalid input", resp) + } + else + { + Response$new("Unexpected response status code", resp) + } + }, + addPet = function(pet){ + resp <- httr::POST(self$url, + httr::add_headers("accept" = "application/json", + "content-type" = "application/json"), + body = pet$toJson()) + if (httr::http_type(resp) != "application/json") { + stop("API did not return json", call. = FALSE) + } + + if (httr::status_code(resp) == 200) + { + Response$new("Pet added", resp) + } + else if(httr::status_code(resp) == 405) + { + Response$new("Invalid input", resp) + } + else + { + Response$new("Unexpected response status code", resp) + } + } + ) +) \ No newline at end of file diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md new file mode 100644 index 00000000000..17e1b0fed67 --- /dev/null +++ b/samples/client/petstore/R/README.md @@ -0,0 +1 @@ +https://github.com/swagger-api/swagger-codegen/issues/2231 \ No newline at end of file diff --git a/samples/client/petstore/R/man/Element.Rd b/samples/client/petstore/R/man/Element.Rd new file mode 100644 index 00000000000..38dcd7b4070 --- /dev/null +++ b/samples/client/petstore/R/man/Element.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pet.R +\docType{data} +\name{Element} +\alias{Element} +\title{Element Class} +\format{An object of class \code{R6ClassGenerator} of length 24.} +\usage{ +Element +} +\description{ +Element Class +} +\keyword{datasets} diff --git a/samples/client/petstore/R/man/Pet.Rd b/samples/client/petstore/R/man/Pet.Rd new file mode 100644 index 00000000000..8c33afacc8d --- /dev/null +++ b/samples/client/petstore/R/man/Pet.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pet.R +\docType{data} +\name{Pet} +\alias{Pet} +\title{Pet Class} +\format{An object of class \code{R6ClassGenerator} of length 24.} +\usage{ +Pet +} +\description{ +Pet Class +} +\keyword{datasets} diff --git a/samples/client/petstore/R/man/PetStoreClient.Rd b/samples/client/petstore/R/man/PetStoreClient.Rd new file mode 100644 index 00000000000..dfcce8bb369 --- /dev/null +++ b/samples/client/petstore/R/man/PetStoreClient.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/petstore.R +\docType{data} +\name{PetStoreClient} +\alias{PetStoreClient} +\title{Pet Store Client Class} +\format{An object of class \code{R6ClassGenerator} of length 24.} +\usage{ +PetStoreClient +} +\description{ +Pet Store Client Class +} +\keyword{datasets} diff --git a/samples/client/petstore/R/man/Response.Rd b/samples/client/petstore/R/man/Response.Rd new file mode 100644 index 00000000000..c9fb6beddef --- /dev/null +++ b/samples/client/petstore/R/man/Response.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/pet.R +\docType{data} +\name{Response} +\alias{Response} +\title{Response Class} +\format{An object of class \code{R6ClassGenerator} of length 24.} +\usage{ +Response +} +\description{ +Response Class +} +\keyword{datasets} diff --git a/samples/client/petstore/R/petstore.Rproj b/samples/client/petstore/R/petstore.Rproj new file mode 100644 index 00000000000..d848a9ff573 --- /dev/null +++ b/samples/client/petstore/R/petstore.Rproj @@ -0,0 +1,16 @@ +Version: 1.0 + +RestoreWorkspace: No +SaveWorkspace: No +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +Encoding: UTF-8 + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes + +BuildType: Package +PackageUseDevtools: Yes +PackageInstallArgs: --no-multiarch --with-keep.source +PackageRoxygenize: rd,collate,namespace diff --git a/samples/client/petstore/R/tests/testthat.R b/samples/client/petstore/R/tests/testthat.R new file mode 100644 index 00000000000..d67bd9cf238 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat.R @@ -0,0 +1,4 @@ +library(testthat) +library(petstore) + +test_check("petstore") diff --git a/samples/client/petstore/R/tests/testthat/test-petstore.R b/samples/client/petstore/R/tests/testthat/test-petstore.R new file mode 100644 index 00000000000..0df88ccddb2 --- /dev/null +++ b/samples/client/petstore/R/tests/testthat/test-petstore.R @@ -0,0 +1,23 @@ +context("basic functionality") +petStoreClient <- PetStoreClient$new("petstore.swagger.io", "v2", "http") +petId <- sample(1:1000, 1) +test_that("addPet", { + pet <- Pet$new(petId, + Element$new(0,"test"), + "test", + list("test"), + list(Element$new(0, "test")), + "available") + response <- petStoreClient$addPet(pet) + expect_equal(response$content, "Pet added") +}) + +test_that("getPetById", { + response <- petStoreClient$getPetById(petId) + expect_equal(response$content$id, petId) +}) + +test_that("updatePetWithForm", { + response <- petStoreClient$updatePetWithForm(petId, "test", "sold") + expect_equal(response$content, "Pet updated") +}) \ No newline at end of file