Skip to content

Commit 10fbadf

Browse files
experimental java exporter
1 parent 91f9a9c commit 10fbadf

File tree

5 files changed

+419
-2
lines changed

5 files changed

+419
-2
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,29 @@ jobs:
6969
- name: Tests
7070
run: |
7171
npm run test
72+
integration-tests-java:
73+
runs-on: ubuntu-latest
74+
steps:
75+
- uses: actions/checkout@v4
76+
with:
77+
ref: "9.2"
78+
- name: Setup node.js
79+
uses: actions/setup-node@v4
80+
- name: Install
81+
run: |
82+
npm install
83+
- name: Build
84+
run: |
85+
npm run build
86+
- name: Install Java
87+
uses: actions/setup-java@v5
88+
with:
89+
distribution: temurin
90+
java-version: 21
91+
- name: Prepare for testing
92+
run: |
93+
npm run test:setup-php
94+
- name: Integration tests
95+
run: |
96+
npm run test:integration-php
97+

scripts/compile-templates.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const sourceDir = (process.platform !== "win32")
55
? "src/exporters"
66
: "src\\exporters";
77
process.chdir(sourceDir);
8-
const cmd = "handlebars python.tpl javascript.tpl php.tpl ruby.tpl -f templates.js -c";
8+
const cmd = "handlebars python.tpl javascript.tpl php.tpl ruby.tpl java.tpl -f templates.js -c";
99
exec(cmd, (error, stdout, stderr) => {
1010
if (error) {
1111
console.log(stdout);

src/convert.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CurlExporter } from "./exporters/curl";
66
import { JavaScriptExporter } from "./exporters/javascript";
77
import { PHPExporter } from "./exporters/php";
88
import { RubyExporter } from "./exporters/ruby";
9+
import { JavaExporter } from "./exporters/java";
910
import util from "util";
1011

1112
const isBrowser = typeof window !== "undefined";
@@ -44,9 +45,10 @@ const EXPORTERS: Record<string, FormatExporter> = {
4445
php: new PHPExporter(),
4546
python: new PythonExporter(),
4647
ruby: new RubyExporter(),
48+
java: new JavaExporter(),
4749
curl: new CurlExporter(),
4850
};
49-
const LANGUAGES = ["JavaScript", "PHP", "Python", "Ruby", "curl"];
51+
const LANGUAGES = ["JavaScript", "PHP", "Python", "Ruby", "Java", "curl"];
5052

5153
/**
5254
* Return the list of available export formats.

src/exporters/java.tpl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{{#if complete}}
2+
package com.example;
3+
import java.io.IOException;
4+
import co.elastic.clients.elasticsearch.ElasticsearchClient;
5+
6+
public class App {
7+
public static void main(String[] args) throws java.io.IOException {
8+
ElasticsearchClient client = ElasticsearchClient.of(_client -> _client
9+
.host({{#if elasticsearchUrl}}"{{elasticsearchUrl}}"{{else}}System.getenv("ELASTICSEARCH_URL"){{/if}})
10+
.apiKey(System.getenv("ELASTIC_API_KEY"))
11+
.usernameAndPassword("elastic", "xaqafzvl")
12+
);
13+
14+
try {
15+
{{/if}}
16+
{{#each requests}}
17+
{{#supportedApi}}
18+
{{#hasArgs}}
19+
{{../baseIndent}}var resp{{#if @index}}{{@index}}{{/if}} = client.{{nsAndApi this.api}}(_{{api this.api}} -> _{{api this.api}}
20+
{{#each this.params}}
21+
{{../../baseIndent}}.{{alias @key ../this.request.path}}("{{{this}}}")
22+
{{/each}}
23+
{{#each this.query}}
24+
{{../../baseIndent}}.{{alias @key ../this.request.query}}({{{javaprint this @key ../this.request.query}}})
25+
{{/each}}
26+
{{#ifRequestBodyKind "properties"}}
27+
{{#each this.body}}
28+
{{../../baseIndent}}.{{alias @key ../this.request.body.properties}}({{{javaprint this @key ../this.request.body.properties}}})
29+
{{/each}}
30+
{{else ifRequestBodyKind "value"}}
31+
{{#if this.request.body.codegenName}}{{this.request.body.codegenName}}{{else}}body{{/if}}={{{javaprint this.body}}},
32+
{{/ifRequestBodyKind}}
33+
{{../baseIndent}});
34+
{{else}}
35+
{{../baseIndent}}var resp{{#if @index}}{{@index}}{{/if}} = client.{{this.api}}();
36+
{{/hasArgs}}
37+
{{else}}
38+
resp{{#if @index}}{{@index}}{{/if}} = client.perform_request(
39+
"{{this.method}}",
40+
"{{this.path}}",
41+
{{#if this.query}}
42+
params={{{javaprint this.query}}},
43+
{{/if}}
44+
{{#if this.body}}
45+
headers={"Content-Type": "application/json"},
46+
body={{{javaprint this.body}}},
47+
{{/if}}
48+
)
49+
{{/supportedApi}}
50+
{{#if ../printResponse}}
51+
{{../baseIndent}}System.out.println(resp{{#if @index}}{{@index}}{{/if}}.toString());
52+
{{/if}}
53+
54+
{{/each}}
55+
{{#if complete}}
56+
}
57+
catch(Exception e) {
58+
System.out.println(e.toString());
59+
}
60+
client.close();
61+
}
62+
}
63+
{{/if}}

0 commit comments

Comments
 (0)