Skip to content

Commit 726765a

Browse files
authored
improve r documentation, readme, code sample (#12700)
1 parent 3b15bb8 commit 726765a

7 files changed

Lines changed: 71 additions & 33 deletions

File tree

modules/openapi-generator/src/main/resources/r/README.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ library(devtools)
5252
install_github("{{{gitUserId}}}/{{{gitRepoId}}}")
5353
```
5454

55+
To install the package from a local file:
56+
```R
57+
install.packages("{{{packageName}}}_{{{packageVersion}}}.tar.gz", repos = NULL, type = "source")
58+
```
59+
5560
### Usage
5661

5762
```R

modules/openapi-generator/src/main/resources/r/api_doc.mustache

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Method | HTTP request | Description
1111
{{#operations}}
1212
{{#operation}}
1313
# **{{operationId}}**
14-
> {{#returnType}}{{.}} {{/returnType}}{{operationId}}({{#requiredParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}{{#defaultValue}}={{{.}}}{{/defaultValue}}{{^defaultValue}}=var.{{{paramName}}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/optionalParams}})
14+
> {{#returnType}}{{.}} {{/returnType}}{{operationId}}({{#requiredParams}}{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{^defaultValue}} = var.{{{paramName}}}{{/defaultValue}}{{^-last}}, {{/-last}}{{/optionalParams}})
1515

1616
{{{summary}}}{{#notes}}
1717

@@ -49,11 +49,15 @@ api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
4949
{{#returnExceptionOnFailure}}
5050
{{#useRlangExceptionHandling}}
5151
result <- tryCatch(
52-
api_instance${{{operationId}}}({{#requiredParams}}var_{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}=var_{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}}),
52+
{{#returnType}}
53+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
54+
# api_instance${{{operationId}}}({{#requiredParams}}var_{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}} = var_{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}}{{#allParams}}{{#-first}}, {{/-first}}{{/allParams}}data_file = "result.txt"),
55+
{{/returnType}}
56+
api_instance${{{operationId}}}({{#requiredParams}}var_{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}} = var_{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}}),
5357
ApiException = function(ex) ex
5458
)
5559
# In case of error, print the error object
56-
if(!is.null(result$ApiException)) {
60+
if (!is.null(result$ApiException)) {
5761
cat(result$ApiException$toString())
5862
} else {
5963
{{#returnType}}
@@ -68,7 +72,7 @@ if(!is.null(result$ApiException)) {
6872
{{/useRlangExceptionHandling}}
6973
{{/returnExceptionOnFailure}}
7074
{{^useRlangExceptionHandling}}
71-
{{#returnType}}result <- {{/returnType}}api_instance${{{operationId}}}({{#requiredParams}}var_{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}}=var_{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}})
75+
{{#returnType}}result <- {{/returnType}}api_instance${{{operationId}}}({{#requiredParams}}var_{{{paramName}}}{{^-last}}, {{/-last}}{{/requiredParams}}{{#optionalParams}}{{#-first}}{{#requiredParams.0}}, {{/requiredParams.0}}{{/-first}}{{{paramName}}} = var_{{{paramName}}}{{^-last}}, {{/-last}}{{/optionalParams}})
7276
{{#returnType}}
7377
dput(result)
7478
{{/returnType}}

samples/client/petstore/R/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ library(devtools)
4444
install_github("GIT_USER_ID/GIT_REPO_ID")
4545
```
4646

47+
To install the package from a local file:
48+
```R
49+
install.packages("petstore_1.0.0.tar.gz", repos = NULL, type = "source")
50+
```
51+
4752
### Usage
4853

4954
```R

samples/client/petstore/R/docs/FakeApi.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Method | HTTP request | Description
88

99

1010
# **FakeDataFile**
11-
> User FakeDataFile(dummy, var_data_file=var.var_data_file)
11+
> User FakeDataFile(dummy, var_data_file = var.var_data_file)
1212
1313
test data_file to ensure it's escaped correctly
1414

@@ -24,11 +24,13 @@ var_var_data_file <- 'var_data_file_example' # character | header data file
2424
#test data_file to ensure it's escaped correctly
2525
api_instance <- FakeApi$new()
2626
result <- tryCatch(
27-
api_instance$FakeDataFile(var_dummy, var_data_file=var_var_data_file),
27+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
28+
# api_instance$FakeDataFile(var_dummy, var_data_file = var_var_data_file, data_file = "result.txt"),
29+
api_instance$FakeDataFile(var_dummy, var_data_file = var_var_data_file),
2830
ApiException = function(ex) ex
2931
)
3032
# In case of error, print the error object
31-
if(!is.null(result$ApiException)) {
33+
if (!is.null(result$ApiException)) {
3234
cat(result$ApiException$toString())
3335
} else {
3436
# deserialized response object

samples/client/petstore/R/docs/PetApi.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ api_instance <- PetApi$new()
3232
# Configure OAuth2 access token for authorization: petstore_auth
3333
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
3434
result <- tryCatch(
35+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
36+
# api_instance$AddPet(var_pet, data_file = "result.txt"),
3537
api_instance$AddPet(var_pet),
3638
ApiException = function(ex) ex
3739
)
3840
# In case of error, print the error object
39-
if(!is.null(result$ApiException)) {
41+
if (!is.null(result$ApiException)) {
4042
cat(result$ApiException$toString())
4143
} else {
4244
# deserialized response object
@@ -74,7 +76,7 @@ Name | Type | Description | Notes
7476
| **405** | Invalid input | - |
7577

7678
# **DeletePet**
77-
> DeletePet(pet_id, api_key=var.api_key)
79+
> DeletePet(pet_id, api_key = var.api_key)
7880
7981
Deletes a pet
8082

@@ -92,11 +94,11 @@ api_instance <- PetApi$new()
9294
# Configure OAuth2 access token for authorization: petstore_auth
9395
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
9496
result <- tryCatch(
95-
api_instance$DeletePet(var_pet_id, api_key=var_api_key),
97+
api_instance$DeletePet(var_pet_id, api_key = var_api_key),
9698
ApiException = function(ex) ex
9799
)
98100
# In case of error, print the error object
99-
if(!is.null(result$ApiException)) {
101+
if (!is.null(result$ApiException)) {
100102
cat(result$ApiException$toString())
101103
} else {
102104
# response headers
@@ -149,11 +151,13 @@ api_instance <- PetApi$new()
149151
# Configure OAuth2 access token for authorization: petstore_auth
150152
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
151153
result <- tryCatch(
154+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
155+
# api_instance$FindPetsByStatus(var_status, data_file = "result.txt"),
152156
api_instance$FindPetsByStatus(var_status),
153157
ApiException = function(ex) ex
154158
)
155159
# In case of error, print the error object
156-
if(!is.null(result$ApiException)) {
160+
if (!is.null(result$ApiException)) {
157161
cat(result$ApiException$toString())
158162
} else {
159163
# deserialized response object
@@ -208,11 +212,13 @@ api_instance <- PetApi$new()
208212
# Configure OAuth2 access token for authorization: petstore_auth
209213
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
210214
result <- tryCatch(
215+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
216+
# api_instance$FindPetsByTags(var_tags, data_file = "result.txt"),
211217
api_instance$FindPetsByTags(var_tags),
212218
ApiException = function(ex) ex
213219
)
214220
# In case of error, print the error object
215-
if(!is.null(result$ApiException)) {
221+
if (!is.null(result$ApiException)) {
216222
cat(result$ApiException$toString())
217223
} else {
218224
# deserialized response object
@@ -267,11 +273,13 @@ api_instance <- PetApi$new()
267273
# Configure API key authorization: api_key
268274
api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
269275
result <- tryCatch(
276+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
277+
# api_instance$GetPetById(var_pet_id, data_file = "result.txt"),
270278
api_instance$GetPetById(var_pet_id),
271279
ApiException = function(ex) ex
272280
)
273281
# In case of error, print the error object
274-
if(!is.null(result$ApiException)) {
282+
if (!is.null(result$ApiException)) {
275283
cat(result$ApiException$toString())
276284
} else {
277285
# deserialized response object
@@ -327,11 +335,13 @@ api_instance <- PetApi$new()
327335
# Configure OAuth2 access token for authorization: petstore_auth
328336
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
329337
result <- tryCatch(
338+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
339+
# api_instance$UpdatePet(var_pet, data_file = "result.txt"),
330340
api_instance$UpdatePet(var_pet),
331341
ApiException = function(ex) ex
332342
)
333343
# In case of error, print the error object
334-
if(!is.null(result$ApiException)) {
344+
if (!is.null(result$ApiException)) {
335345
cat(result$ApiException$toString())
336346
} else {
337347
# deserialized response object
@@ -371,7 +381,7 @@ Name | Type | Description | Notes
371381
| **405** | Validation exception | - |
372382

373383
# **UpdatePetWithForm**
374-
> UpdatePetWithForm(pet_id, name=var.name, status=var.status)
384+
> UpdatePetWithForm(pet_id, name = var.name, status = var.status)
375385
376386
Updates a pet in the store with form data
377387

@@ -390,11 +400,11 @@ api_instance <- PetApi$new()
390400
# Configure OAuth2 access token for authorization: petstore_auth
391401
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
392402
result <- tryCatch(
393-
api_instance$UpdatePetWithForm(var_pet_id, name=var_name, status=var_status),
403+
api_instance$UpdatePetWithForm(var_pet_id, name = var_name, status = var_status),
394404
ApiException = function(ex) ex
395405
)
396406
# In case of error, print the error object
397-
if(!is.null(result$ApiException)) {
407+
if (!is.null(result$ApiException)) {
398408
cat(result$ApiException$toString())
399409
} else {
400410
# response headers
@@ -431,7 +441,7 @@ void (empty response body)
431441
| **405** | Invalid input | - |
432442

433443
# **UploadFile**
434-
> ModelApiResponse UploadFile(pet_id, additional_metadata=var.additional_metadata, file=var.file)
444+
> ModelApiResponse UploadFile(pet_id, additional_metadata = var.additional_metadata, file = var.file)
435445
436446
uploads an image
437447

@@ -450,11 +460,13 @@ api_instance <- PetApi$new()
450460
# Configure OAuth2 access token for authorization: petstore_auth
451461
api_instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
452462
result <- tryCatch(
453-
api_instance$UploadFile(var_pet_id, additional_metadata=var_additional_metadata, file=var_file),
463+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
464+
# api_instance$UploadFile(var_pet_id, additional_metadata = var_additional_metadata, file = var_file, data_file = "result.txt"),
465+
api_instance$UploadFile(var_pet_id, additional_metadata = var_additional_metadata, file = var_file),
454466
ApiException = function(ex) ex
455467
)
456468
# In case of error, print the error object
457-
if(!is.null(result$ApiException)) {
469+
if (!is.null(result$ApiException)) {
458470
cat(result$ApiException$toString())
459471
} else {
460472
# deserialized response object

samples/client/petstore/R/docs/StoreApi.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ result <- tryCatch(
3030
ApiException = function(ex) ex
3131
)
3232
# In case of error, print the error object
33-
if(!is.null(result$ApiException)) {
33+
if (!is.null(result$ApiException)) {
3434
cat(result$ApiException$toString())
3535
} else {
3636
# response headers
@@ -82,11 +82,13 @@ api_instance <- StoreApi$new()
8282
# Configure API key authorization: api_key
8383
api_instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
8484
result <- tryCatch(
85+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
86+
# api_instance$GetInventory(data_file = "result.txt"),
8587
api_instance$GetInventory(),
8688
ApiException = function(ex) ex
8789
)
8890
# In case of error, print the error object
89-
if(!is.null(result$ApiException)) {
91+
if (!is.null(result$ApiException)) {
9092
cat(result$ApiException$toString())
9193
} else {
9294
# deserialized response object
@@ -135,11 +137,13 @@ var_order_id <- 56 # integer | ID of pet that needs to be fetched
135137
#Find purchase order by ID
136138
api_instance <- StoreApi$new()
137139
result <- tryCatch(
140+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
141+
# api_instance$GetOrderById(var_order_id, data_file = "result.txt"),
138142
api_instance$GetOrderById(var_order_id),
139143
ApiException = function(ex) ex
140144
)
141145
# In case of error, print the error object
142-
if(!is.null(result$ApiException)) {
146+
if (!is.null(result$ApiException)) {
143147
cat(result$ApiException$toString())
144148
} else {
145149
# deserialized response object
@@ -193,11 +197,13 @@ var_order <- Order$new(123, 123, 123, "shipDate_example", "placed", "complete_ex
193197
#Place an order for a pet
194198
api_instance <- StoreApi$new()
195199
result <- tryCatch(
200+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
201+
# api_instance$PlaceOrder(var_order, data_file = "result.txt"),
196202
api_instance$PlaceOrder(var_order),
197203
ApiException = function(ex) ex
198204
)
199205
# In case of error, print the error object
200-
if(!is.null(result$ApiException)) {
206+
if (!is.null(result$ApiException)) {
201207
cat(result$ApiException$toString())
202208
} else {
203209
# deserialized response object

samples/client/petstore/R/docs/UserApi.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ result <- tryCatch(
3636
ApiException = function(ex) ex
3737
)
3838
# In case of error, print the error object
39-
if(!is.null(result$ApiException)) {
39+
if (!is.null(result$ApiException)) {
4040
cat(result$ApiException$toString())
4141
} else {
4242
# response headers
@@ -92,7 +92,7 @@ result <- tryCatch(
9292
ApiException = function(ex) ex
9393
)
9494
# In case of error, print the error object
95-
if(!is.null(result$ApiException)) {
95+
if (!is.null(result$ApiException)) {
9696
cat(result$ApiException$toString())
9797
} else {
9898
# response headers
@@ -148,7 +148,7 @@ result <- tryCatch(
148148
ApiException = function(ex) ex
149149
)
150150
# In case of error, print the error object
151-
if(!is.null(result$ApiException)) {
151+
if (!is.null(result$ApiException)) {
152152
cat(result$ApiException$toString())
153153
} else {
154154
# response headers
@@ -204,7 +204,7 @@ result <- tryCatch(
204204
ApiException = function(ex) ex
205205
)
206206
# In case of error, print the error object
207-
if(!is.null(result$ApiException)) {
207+
if (!is.null(result$ApiException)) {
208208
cat(result$ApiException$toString())
209209
} else {
210210
# response headers
@@ -255,11 +255,13 @@ var_username <- 'username_example' # character | The name that needs to be fetch
255255
#Get user by user name
256256
api_instance <- UserApi$new()
257257
result <- tryCatch(
258+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
259+
# api_instance$GetUserByName(var_username, data_file = "result.txt"),
258260
api_instance$GetUserByName(var_username),
259261
ApiException = function(ex) ex
260262
)
261263
# In case of error, print the error object
262-
if(!is.null(result$ApiException)) {
264+
if (!is.null(result$ApiException)) {
263265
cat(result$ApiException$toString())
264266
} else {
265267
# deserialized response object
@@ -314,11 +316,13 @@ var_password <- 'password_example' # character | The password for login in clear
314316
#Logs user into the system
315317
api_instance <- UserApi$new()
316318
result <- tryCatch(
319+
# to save the result into a file, simply add the optional `data_file` parameter, e.g.
320+
# api_instance$LoginUser(var_username, var_password, data_file = "result.txt"),
317321
api_instance$LoginUser(var_username, var_password),
318322
ApiException = function(ex) ex
319323
)
320324
# In case of error, print the error object
321-
if(!is.null(result$ApiException)) {
325+
if (!is.null(result$ApiException)) {
322326
cat(result$ApiException$toString())
323327
} else {
324328
# deserialized response object
@@ -377,7 +381,7 @@ result <- tryCatch(
377381
ApiException = function(ex) ex
378382
)
379383
# In case of error, print the error object
380-
if(!is.null(result$ApiException)) {
384+
if (!is.null(result$ApiException)) {
381385
cat(result$ApiException$toString())
382386
} else {
383387
# response headers
@@ -431,7 +435,7 @@ result <- tryCatch(
431435
ApiException = function(ex) ex
432436
)
433437
# In case of error, print the error object
434-
if(!is.null(result$ApiException)) {
438+
if (!is.null(result$ApiException)) {
435439
cat(result$ApiException$toString())
436440
} else {
437441
# response headers

0 commit comments

Comments
 (0)