Skip to content

Commit 4fa8067

Browse files
authored
Merge pull request #625 from swagger-api/r-generator
R generator
2 parents 0a9e748 + 847606a commit 4fa8067

30 files changed

+1620
-0
lines changed

src/main/java/io/swagger/codegen/v3/generators/r/RClientCodegen.java

Lines changed: 461 additions & 0 deletions
Large diffs are not rendered by default.

src/main/resources/META-INF/services/io.swagger.codegen.v3.CodegenConfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ io.swagger.codegen.v3.generators.kotlin.KotlinServerCodegen
2525
io.swagger.codegen.v3.generators.php.PhpClientCodegen
2626
io.swagger.codegen.v3.generators.python.PythonClientCodegen
2727
io.swagger.codegen.v3.generators.python.PythonFlaskConnexionCodegen
28+
io.swagger.codegen.v3.generators.r.RClientCodegen
2829
io.swagger.codegen.v3.generators.scala.ScalaClientCodegen
2930
io.swagger.codegen.v3.generators.scala.AkkaHttpServerCodegen
3031
io.swagger.codegen.v3.generators.swift.Swift3Codegen
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Generated by swagger-codegen: https://github.com/swagger-api/swagger-codegen
2+
# Do not edit by hand
3+
4+
{{#models}}
5+
{{#model}}
6+
export({{{classname}}})
7+
{{/model}}
8+
{{/models}}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# R API client for {{packageName}}
2+
3+
{{#appDescription}}
4+
{{{appDescription}}}
5+
{{/appDescription}}
6+
7+
## Overview
8+
This API client was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [OpenAPI/Swagger spec](https://github.com/swagger-api/swagger-spec) from a remote server, you can easily generate an API client.
9+
10+
- API version: {{appVersion}}
11+
- Package version: {{packageVersion}}
12+
{{^hideGenerationTimestamp}}
13+
- Build date: {{generatedDate}}
14+
{{/hideGenerationTimestamp}}
15+
- Build package: {{generatorClass}}
16+
{{#infoUrl}}
17+
For more information, please visit [{{{infoUrl}}}]({{{infoUrl}}})
18+
{{/infoUrl}}
19+
20+
## Installation
21+
You'll need the `devtools` package in order to build the API.
22+
Make sure you have a proper CRAN repository from which you can download packages.
23+
24+
### Prerequisites
25+
Install the `devtools` package with the following command.
26+
```R
27+
if(!require(devtools)) { install.packages("devtools") }
28+
```
29+
30+
### Installation of the API package
31+
Make sure you set the working directory to where the API code is located.
32+
Then execute
33+
```R
34+
library(devtools)
35+
install(".")
36+
```
37+
38+
## Author
39+
40+
{{#apiInfo}}{{#apis}}{{^hasMore}}{{infoEmail}}
41+
{{/hasMore}}{{/apis}}{{/apiInfo}}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{{>partial_header}}
2+
{{#operations}}
3+
#' @title {{baseName}} operations
4+
#' @description {{importPath}}
5+
#'
6+
#' @field path Stores url path of the request.
7+
#' @field apiClient Handles the client-server communication.
8+
#' @field userAgent Set the user agent of the request.
9+
#'
10+
#' @importFrom R6 R6Class
11+
#'
12+
#' @section Methods:
13+
#' \describe{
14+
{{#operation}}
15+
#'
16+
#' {{operationId}} {{summary}}
17+
#'
18+
{{/operation}}
19+
#' }
20+
#'
21+
#' @export
22+
{{classname}} <- R6::R6Class(
23+
'{{classname}}',
24+
public = list(
25+
userAgent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/r{{/httpUserAgent}}",
26+
apiClient = NULL,
27+
initialize = function(apiClient){
28+
if (!missing(apiClient)) {
29+
self$apiClient <- apiClient
30+
}
31+
else {
32+
self$apiClient <- ApiClient$new()
33+
}
34+
},
35+
{{#operation}}
36+
{{operationId}} = function({{#allParams}}{{paramName}}, {{/allParams}}...){
37+
args <- list(...)
38+
queryParams <- list()
39+
headerParams <- character()
40+
41+
{{#hasHeaderParams}}
42+
{{#headerParams}}
43+
if (!missing(`{{paramName}}`)) {
44+
headerParams['{{baseName}}'] <- `{{paramName}}`
45+
}
46+
47+
{{/headerParams}}
48+
{{/hasHeaderParams}}
49+
{{#hasQueryParams}}
50+
{{#queryParams}}
51+
if (!missing(`{{paramName}}`)) {
52+
queryParams['{{baseName}}'] <- {{paramName}}
53+
}
54+
55+
{{/queryParams}}
56+
{{/hasQueryParams}}
57+
{{#hasFormParams}}
58+
body <- list(
59+
{{#formParams}}
60+
{{^isFile}}
61+
"{{baseName}}" = {{paramName}}{{#hasMore}},{{/hasMore}}
62+
{{/isFile}}
63+
{{#isFile}}
64+
"{{baseName}}" = httr::upload_file({{paramName}}){{#hasMore}},{{/hasMore}}
65+
{{/isFile}}
66+
{{/formParams}}
67+
)
68+
69+
{{/hasFormParams}}
70+
{{#hasBodyParam}}
71+
{{#bodyParams}}
72+
if (!missing(`{{paramName}}`)) {
73+
body <- `{{paramName}}`$toJSONString()
74+
} else {
75+
body <- NULL
76+
}
77+
78+
{{/bodyParams}}
79+
{{/hasBodyParam}}
80+
urlPath <- "{{path}}"
81+
{{#hasPathParams}}
82+
{{#pathParams}}
83+
if (!missing(`{{paramName}}`)) {
84+
urlPath <- gsub(paste0("\\{", "{{baseName}}", "\\}"), `{{paramName}}`, urlPath)
85+
}
86+
87+
{{/pathParams}}
88+
{{/hasPathParams}}
89+
resp <- self$apiClient$callApi(url = paste0(self$apiClient$basePath, urlPath),
90+
method = "{{httpMethod}}",
91+
queryParams = queryParams,
92+
headerParams = headerParams,
93+
body = body,
94+
...)
95+
96+
if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
97+
{{#returnType}}
98+
returnObject <- {{returnType}}$new()
99+
result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
100+
Response$new(returnObject, resp)
101+
{{/returnType}}
102+
{{^returnType}}
103+
# void response, no need to return anything
104+
{{/returnType}}
105+
} else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
106+
Response$new("API client error", resp)
107+
} else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
108+
Response$new("API server error", resp)
109+
}
110+
111+
}{{#hasMore}},{{/hasMore}}
112+
{{/operation}}
113+
)
114+
)
115+
{{/operations}}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{{>partial_header}}
2+
3+
#' ApiClient Class
4+
#'
5+
#' Generic API client for Swagger client library builds.
6+
#' Swagger generic API client. This client handles the client-
7+
#' server communication, and is invariant across implementations. Specifics of
8+
#' the methods and models for each application are generated from the Swagger
9+
#' templates.
10+
#'
11+
#' NOTE: This class is auto generated by the swagger code generator program.
12+
#' Ref: https://github.com/swagger-api/swagger-codegen
13+
#' Do not edit the class manually.
14+
#'
15+
#' @export
16+
ApiClient <- R6::R6Class(
17+
'ApiClient',
18+
public = list(
19+
basePath = "{{{basePath}}}",
20+
configuration = NULL,
21+
userAgent = NULL,
22+
defaultHeaders = NULL,
23+
initialize = function(basePath, configuration, defaultHeaders){
24+
if (!missing(basePath)) {
25+
self$basePath <- basePath
26+
}
27+
28+
if (!missing(configuration)) {
29+
self$configuration <- configuration
30+
}
31+
32+
if (!missing(defaultHeaders)) {
33+
self$defaultHeaders <- defaultHeaders
34+
}
35+
36+
self$`userAgent` <- '{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/r{{/httpUserAgent}}'
37+
},
38+
callApi = function(url, method, queryParams, headerParams, body, ...){
39+
headers <- httr::add_headers(headerParams)
40+
41+
if (method == "GET") {
42+
httr::GET(url, queryParams, headers, ...)
43+
}
44+
else if (method == "POST") {
45+
httr::POST(url, queryParams, headers, body = body, ...)
46+
}
47+
else if (method == "PUT") {
48+
httr::PUT(url, queryParams, headers, body = body, ...)
49+
}
50+
else if (method == "PATCH") {
51+
httr::PATCH(url, queryParams, headers, body = body, ...)
52+
}
53+
else if (method == "HEAD") {
54+
httr::HEAD(url, queryParams, headers, ...)
55+
}
56+
else if (method == "DELETE") {
57+
httr::DELETE(url, queryParams, headers, ...)
58+
}
59+
else {
60+
stop("http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`.")
61+
}
62+
}
63+
)
64+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# {{invokerPackage}}\{{classname}}{{#description}}
2+
{{description}}{{/description}}
3+
4+
All URIs are relative to *{{basePath}}*
5+
6+
Method | HTTP request | Description
7+
------------- | ------------- | -------------
8+
{{#operations}}{{#operation}}[**{{operationId}}**]({{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}}
9+
{{/operation}}{{/operations}}
10+
11+
{{#operations}}
12+
{{#operation}}
13+
# **{{{operationId}}}**
14+
> {{#returnType}}{{{returnType}}} {{/returnType}}{{{operationId}}}({{#authMethods}}ctx, {{/authMethods}}{{#allParams}}{{#required}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/required}}{{/allParams}}{{#hasOptionalParams}}optional{{/hasOptionalParams}})
15+
{{{summary}}}{{#notes}}
16+
17+
{{{notes}}}{{/notes}}
18+
19+
### Required Parameters
20+
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#@last}}
21+
Name | Type | Description | Notes
22+
------------- | ------------- | ------------- | -------------{{#authMethods}}
23+
**ctx** | **context.Context** | context containing the authentication | nil if no authentication{{/authMethods}}{{/@last}}{{/allParams}}{{#allParams}}{{#required}}
24+
**{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/required}}{{/allParams}}{{#hasOptionalParams}}
25+
**optional** | **map[string]interface{}** | optional parameters | nil if no parameters
26+
27+
### Optional Parameters
28+
Optional parameters are passed through a map[string]interface{}.
29+
{{#allParams}}{{#@last}}
30+
Name | Type | Description | Notes
31+
------------- | ------------- | ------------- | -------------{{/@last}}{{/allParams}}{{#allParams}}
32+
**{{paramName}}** | {{#isFile}}**{{dataType}}**{{/isFile}}{{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{^isFile}}[**{{dataType}}**]({{baseType}}.md){{/isFile}}{{/isPrimitiveType}}| {{description}} | {{#defaultValue}}[default to {{defaultValue}}]{{/defaultValue}}{{/allParams}}{{/hasOptionalParams}}
33+
34+
### Return type
35+
36+
{{#returnType}}{{#returnTypeIsPrimitive}}**{{{returnType}}}**{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}[**{{{returnType}}}**]({{returnBaseType}}.md){{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}} (empty response body){{/returnType}}
37+
38+
### Authorization
39+
40+
{{^authMethods}}No authorization required{{/authMethods}}{{#authMethods}}[{{{name}}}](../README.md#{{{name}}}){{^@last}}, {{/@last}}{{/authMethods}}
41+
42+
### HTTP request headers
43+
44+
- **Content-Type**: {{#consumes}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/consumes}}{{^consumes}}Not defined{{/consumes}}
45+
- **Accept**: {{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}{{^produces}}Not defined{{/produces}}
46+
47+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
48+
49+
{{/operation}}
50+
{{/operations}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Package: {{{packageName}}}
2+
Title: R Package Client for {{{appName}}}
3+
Version: {{packageVersion}}
4+
Authors@R: person("Swagger Codegen community", email = "[email protected]", role = c("aut", "cre"))
5+
Description: {{{appDescription}}}{{^appDescription}}R Package Client for {{{appName}}}{{/appDescription}}
6+
Depends: R (>= 3.3.3)
7+
Encoding: UTF-8
8+
License: Unlicense
9+
LazyData: true
10+
Suggests: testthat
11+
Imports: jsonlite, httr, R6
12+
RoxygenNote: 6.0.1.9000
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#' Element Class
2+
#'
3+
#' Element Class
4+
#' @export
5+
Element <- R6::R6Class(
6+
'Element',
7+
public = list(
8+
id = NULL,
9+
name = NULL,
10+
initialize = function(id,name){
11+
if (!missing(id)) {
12+
stopifnot(is.numeric(id), length(id) == 1)
13+
self$id <- id
14+
}
15+
if (!missing(name)) {
16+
stopifnot(is.character(name), length(name) == 1)
17+
self$name <- name
18+
}
19+
},
20+
toJSON = function() {
21+
sprintf('{"id":%d,"name":"%s"}', self$id, self$name)
22+
}
23+
)
24+
)

0 commit comments

Comments
 (0)