Skip to content

Commit

Permalink
[R] Add httr2 support (work in progress) (#13005)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wing328 authored Jul 31, 2022
1 parent 6b6403b commit 4635dda
Show file tree
Hide file tree
Showing 128 changed files with 17,842 additions and 24 deletions.
13 changes: 13 additions & 0 deletions bin/configs/r-httr2-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
generatorName: r
outputDir: samples/client/petstore/R-httr2
inputSpec: modules/openapi-generator/src/test/resources/3_0/r/petstore.yaml
templateDir: modules/openapi-generator/src/main/resources/r
httpUserAgent: PetstoreAgent
library: httr2
additionalProperties:
packageName: petstore
exceptionPackage: rlang
useRlangExceptionHandling: true
returnExceptionOnFailure: true
errorObjectType: "ModelApiResponse"
operationIdNaming: snake_case
1 change: 1 addition & 0 deletions docs/generators/r.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|errorObjectType|Error object type.| |null|
|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|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|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|
|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|
|packageName|R package name (convention: lowercase).| |openapi|
|packageVersion|R package version.| |1.0.0|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
public static final String USE_RLANG_EXCEPTION = "useRlangExceptionHandling";
public static final String DEFAULT = "default";
public static final String RLANG = "rlang";
public static final String HTTR = "httr";
public static final String HTTR2 = "httr2";

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

cliOptions.add(CliOption.newString(CodegenConstants.ERROR_OBJECT_TYPE, "Error object type."));

supportedLibraries.put(HTTR2, "httr2 (https://httr2.r-lib.org/)");
supportedLibraries.put(HTTR, "httr (https://cran.r-project.org/web/packages/httr/index.html)");

CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "HTTP library template (sub-template) to use");
libraryOption.setEnum(supportedLibraries);
// set httr as the default
libraryOption.setDefault(HTTR);
cliOptions.add(libraryOption);
setLibrary(HTTR);
}

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

if (HTTR.equals(getLibrary())) {
// for httr
setLibrary(HTTR);
} else if (HTTR2.equals(getLibrary())) {
// for httr2
setLibrary(HTTR2);
additionalProperties.put("isHttr2", Boolean.TRUE);
} else {
throw new IllegalArgumentException("Invalid HTTP library " + getLibrary() + ". Only httr, httr2 are supported.");
}

// add lambda for mustache templates to fix license field
additionalProperties.put("lambdaLicense", new Mustache.Lambda() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import(R6)
import(jsonlite)
{{^isHttr2}}
import(httr)
{{/isHttr2}}
{{#isHttr2}}
import(httr2)
{{/isHttr2}}
import(base64enc)
import(stringr)

Expand Down
7 changes: 6 additions & 1 deletion modules/openapi-generator/src/main/resources/r/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
#' @export
{{{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}}...) {
args <- list(...)
query_params <- list()
query_params <- c()
header_params <- c()
{{#requiredParams}}
Expand Down Expand Up @@ -265,7 +265,12 @@
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
{{/isFile}}
{{#isFile}}
{{^isHttr2}}
"{{baseName}}" = httr::upload_file({{paramName}}){{^-last}},{{/-last}}
{{/isHttr2}}
{{#isHttr2}}
"{{baseName}}" = {{paramName}}{{^-last}},{{/-last}}
{{/isHttr2}}
{{/isFile}}
{{/formParams}}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ Encoding: UTF-8
License: {{#lambdaLicense}}{{licenseInfo}}{{/lambdaLicense}}{{^licenseInfo}}Unlicense{{/licenseInfo}}
LazyData: true
Suggests: testthat
Imports: jsonlite, httr, R6, base64enc, stringr
Imports: jsonlite, httr{{#isHttr2}}2{{/isHttr2}}, R6, base64enc, stringr
RoxygenNote: 7.2.0
Loading

0 comments on commit 4635dda

Please sign in to comment.