Skip to content

Commit 4635dda

Browse files
authored
[R] Add httr2 support (work in progress) (#13005)
* add httr2 support to r client gen * fix headers * add accepts, content-types * update samples * fix req * update samples * various fixes * add data file test * fix streaming, add tests
1 parent 6b6403b commit 4635dda

File tree

128 files changed

+17842
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+17842
-24
lines changed

bin/configs/r-httr2-client.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
generatorName: r
2+
outputDir: samples/client/petstore/R-httr2
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/r
5+
httpUserAgent: PetstoreAgent
6+
library: httr2
7+
additionalProperties:
8+
packageName: petstore
9+
exceptionPackage: rlang
10+
useRlangExceptionHandling: true
11+
returnExceptionOnFailure: true
12+
errorObjectType: "ModelApiResponse"
13+
operationIdNaming: snake_case

docs/generators/r.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2121
|errorObjectType|Error object type.| |null|
2222
|exceptionPackage|Specify the exception handling package|<dl><dt>**default**</dt><dd>Use stop() for raising exceptions.</dd><dt>**rlang**</dt><dd>Use rlang package for exceptions.</dd></dl>|default|
2323
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
24+
|library|HTTP library template (sub-template) to use|<dl><dt>**httr2**</dt><dd>httr2 (https://httr2.r-lib.org/)</dd><dt>**httr**</dt><dd>httr (https://cran.r-project.org/web/packages/httr/index.html)</dd></dl>|httr|
2425
|operationIdNaming|Naming convention for operationId (function name in the API)|<dl><dt>**PascalCase**</dt><dd>Pascal case (default)</dd><dt>**snake_case**</dt><dd>Snake case</dd><dt>**camelCase**</dt><dd>Camel case</dd></dl>|null|
2526
|packageName|R package name (convention: lowercase).| |openapi|
2627
|packageVersion|R package version.| |1.0.0|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
6464
public static final String USE_RLANG_EXCEPTION = "useRlangExceptionHandling";
6565
public static final String DEFAULT = "default";
6666
public static final String RLANG = "rlang";
67+
public static final String HTTR = "httr";
68+
public static final String HTTR2 = "httr2";
6769

6870
// naming convention for operationId (function names in the API)
6971
public static final String OPERATIONID_NAMING = "operationIdNaming";
@@ -199,6 +201,16 @@ public RClientCodegen() {
199201
cliOptions.add(exceptionPackage);
200202

201203
cliOptions.add(CliOption.newString(CodegenConstants.ERROR_OBJECT_TYPE, "Error object type."));
204+
205+
supportedLibraries.put(HTTR2, "httr2 (https://httr2.r-lib.org/)");
206+
supportedLibraries.put(HTTR, "httr (https://cran.r-project.org/web/packages/httr/index.html)");
207+
208+
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "HTTP library template (sub-template) to use");
209+
libraryOption.setEnum(supportedLibraries);
210+
// set httr as the default
211+
libraryOption.setDefault(HTTR);
212+
cliOptions.add(libraryOption);
213+
setLibrary(HTTR);
202214
}
203215

204216
@Override
@@ -274,6 +286,17 @@ public void processOpts() {
274286
supportingFiles.add(new SupportingFile("r-client.mustache", File.separator + ".github" + File.separator + "workflows", "r-client.yaml"));
275287
supportingFiles.add(new SupportingFile("lintr.mustache", "", ".lintr"));
276288

289+
if (HTTR.equals(getLibrary())) {
290+
// for httr
291+
setLibrary(HTTR);
292+
} else if (HTTR2.equals(getLibrary())) {
293+
// for httr2
294+
setLibrary(HTTR2);
295+
additionalProperties.put("isHttr2", Boolean.TRUE);
296+
} else {
297+
throw new IllegalArgumentException("Invalid HTTP library " + getLibrary() + ". Only httr, httr2 are supported.");
298+
}
299+
277300
// add lambda for mustache templates to fix license field
278301
additionalProperties.put("lambdaLicense", new Mustache.Lambda() {
279302
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33

44
import(R6)
55
import(jsonlite)
6+
{{^isHttr2}}
67
import(httr)
8+
{{/isHttr2}}
9+
{{#isHttr2}}
10+
import(httr2)
11+
{{/isHttr2}}
712
import(base64enc)
813
import(stringr)
914

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
#' @export
234234
{{{operationId}}}{{WithHttpInfo}} = function({{#requiredParams}}{{paramName}}, {{/requiredParams}}{{#optionalParams}}{{paramName}} = {{^defaultValue}}NULL{{/defaultValue}}{{{defaultValue}}}, {{/optionalParams}}{{#vendorExtensions.x-streaming}}stream_callback = NULL, {{/vendorExtensions.x-streaming}}{{#returnType}}data_file = NULL, {{/returnType}}...) {
235235
args <- list(...)
236-
query_params <- list()
236+
query_params <- c()
237237
header_params <- c()
238238
239239
{{#requiredParams}}
@@ -265,7 +265,12 @@
265265
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
266266
{{/isFile}}
267267
{{#isFile}}
268+
{{^isHttr2}}
268269
"{{baseName}}" = httr::upload_file({{paramName}}){{^-last}},{{/-last}}
270+
{{/isHttr2}}
271+
{{#isHttr2}}
272+
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
273+
{{/isHttr2}}
269274
{{/isFile}}
270275
{{/formParams}}
271276
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ Encoding: UTF-8
1111
License: {{#lambdaLicense}}{{licenseInfo}}{{/lambdaLicense}}{{^licenseInfo}}Unlicense{{/licenseInfo}}
1212
LazyData: true
1313
Suggests: testthat
14-
Imports: jsonlite, httr, R6, base64enc, stringr
14+
Imports: jsonlite, httr{{#isHttr2}}2{{/isHttr2}}, R6, base64enc, stringr
1515
RoxygenNote: 7.2.0

0 commit comments

Comments
 (0)