mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 06:00:52 +00:00
Adding R Petstore Client Package (#6307)
This commit is contained in:
parent
da4fe86402
commit
3de6a8f0f2
2
samples/client/petstore/R/.Rbuildignore
Normal file
2
samples/client/petstore/R/.Rbuildignore
Normal file
@ -0,0 +1,2 @@
|
||||
^.*\.Rproj$
|
||||
^\.Rproj\.user$
|
3
samples/client/petstore/R/.gitignore
vendored
Normal file
3
samples/client/petstore/R/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.Rproj.user
|
||||
.Rhistory
|
||||
.RData
|
13
samples/client/petstore/R/DESCRIPTION
Normal file
13
samples/client/petstore/R/DESCRIPTION
Normal file
@ -0,0 +1,13 @@
|
||||
Package: petstore
|
||||
Title: R Package Client for <http://swagger.io> 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 <http://swagger.io> 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
|
||||
|
2
samples/client/petstore/R/LICENSE
Normal file
2
samples/client/petstore/R/LICENSE
Normal file
@ -0,0 +1,2 @@
|
||||
YEAR: 2017
|
||||
COPYRIGHT HOLDER: Ramkumar Chandrasekaran
|
6
samples/client/petstore/R/NAMESPACE
Normal file
6
samples/client/petstore/R/NAMESPACE
Normal file
@ -0,0 +1,6 @@
|
||||
# Generated by roxygen2: do not edit by hand
|
||||
|
||||
export(Element)
|
||||
export(Pet)
|
||||
export(PetStoreClient)
|
||||
export(Response)
|
105
samples/client/petstore/R/R/pet.R
Normal file
105
samples/client/petstore/R/R/pet.R
Normal file
@ -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
|
||||
}
|
||||
)
|
||||
)
|
95
samples/client/petstore/R/R/petstore.R
Normal file
95
samples/client/petstore/R/R/petstore.R
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
1
samples/client/petstore/R/README.md
Normal file
1
samples/client/petstore/R/README.md
Normal file
@ -0,0 +1 @@
|
||||
https://github.com/swagger-api/swagger-codegen/issues/2231
|
14
samples/client/petstore/R/man/Element.Rd
Normal file
14
samples/client/petstore/R/man/Element.Rd
Normal file
@ -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}
|
14
samples/client/petstore/R/man/Pet.Rd
Normal file
14
samples/client/petstore/R/man/Pet.Rd
Normal file
@ -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}
|
14
samples/client/petstore/R/man/PetStoreClient.Rd
Normal file
14
samples/client/petstore/R/man/PetStoreClient.Rd
Normal file
@ -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}
|
14
samples/client/petstore/R/man/Response.Rd
Normal file
14
samples/client/petstore/R/man/Response.Rd
Normal file
@ -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}
|
16
samples/client/petstore/R/petstore.Rproj
Normal file
16
samples/client/petstore/R/petstore.Rproj
Normal file
@ -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
|
4
samples/client/petstore/R/tests/testthat.R
Normal file
4
samples/client/petstore/R/tests/testthat.R
Normal file
@ -0,0 +1,4 @@
|
||||
library(testthat)
|
||||
library(petstore)
|
||||
|
||||
test_check("petstore")
|
23
samples/client/petstore/R/tests/testthat/test-petstore.R
Normal file
23
samples/client/petstore/R/tests/testthat/test-petstore.R
Normal file
@ -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")
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user