Skip to content

Commit b750729

Browse files
committed
Adds ExampleClient API
1 parent e07d23d commit b750729

File tree

17 files changed

+424
-13
lines changed

17 files changed

+424
-13
lines changed

README.md

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ http://localhost:8080/swagger-ui/index.html
4646

4747
## Demo Application
4848

49-
The [demo-application](demo-application) folder contains a simple applications, that consumes the custom API _(`/custom-api/MyApi`)_. The `/custom-api/MyApi` is a TypeScript API, that exposes the `io.dirigible.samples.MyFacade` Java class. The Java class has both `static` and `instance` methods.
49+
The [demo-application](demo-application) folder contains a simple applications, that consumes the custom APIs _(`custom-api/MyApi` and `custom-api/ExampleClient`)_ . The `custom-api/MyApi` is a TypeScript API, that exposes the `io.dirigible.samples.MyFacade` Java class. The Java class has both `static` and `instance` methods. The `custom-api/ExampleClient` contains more sophisticated usage of multiple Java classes.
50+
51+
### Basic Example
5052

5153
**demo-application/demo.ts**
5254
```ts
5355
import { response } from "sdk/http";
54-
import { MyApi } from "../custom-api/MyApi";
56+
import { MyApi } from "custom-api/MyApi";
5557

5658
const myApiInstance = new MyApi();
5759

@@ -70,7 +72,7 @@ const data = {
7072
response.println(JSON.stringify(data, null, 2));
7173
```
7274

73-
**/custom-api/MyApi**
75+
**custom-api/MyApi**
7476
```ts
7577
const MyFacade = Java.type("io.dirigible.samples.MyFacade");
7678

@@ -94,4 +96,68 @@ export class MyApi {
9496
return this.facadeInstance.customMethod(input);
9597
}
9698
}
99+
```
100+
101+
### Advanced
102+
103+
**demo-application/demo-client.ts**
104+
```ts
105+
import { response } from "sdk/http";
106+
import { ExampleClient } from "custom-api/ExampleClient";
107+
import { ExampleRequest } from "custom-api/ExampleRequest";
108+
109+
const exampleRequest = new ExampleRequest();
110+
exampleRequest.setExampleId('example-id-1234');
111+
exampleRequest.setExampleName('Custom Stack Example');
112+
113+
const exampleClient = new ExampleClient();
114+
const exampleResponse = exampleClient.doExample(exampleRequest);
115+
116+
response.println(JSON.stringify(exampleResponse, null, 2));
117+
```
118+
119+
**custom-api/ExampleClient**
120+
121+
```ts
122+
import { ExampleResponse } from "./ExampleResponse";
123+
import { ExampleRequest } from "./ExampleRequest";
124+
import { Example } from "./Example";
125+
import { SubExample } from "./SubExample";
126+
127+
const ExampleClientClass = Java.type("io.dirigible.samples.api.client.ExampleClient");
128+
const ExampleRequestClass = Java.type("io.dirigible.samples.api.domain.input.ExampleRequest");
129+
130+
export class ExampleClient {
131+
132+
public doExample(request: ExampleRequest): ExampleResponse {
133+
const requestObj = new ExampleRequestClass();
134+
requestObj.setExampleId(request.getExampleId());
135+
requestObj.setExampleName(request.getExampleName());
136+
137+
const responseObj = new ExampleClientClass().doExample(requestObj);
138+
139+
const examples: Example[] = [];
140+
141+
for (const exampleObj of responseObj.getExamples()) {
142+
const example = new Example();
143+
const subExamples: SubExample[] = [];
144+
145+
example.setId(exampleObj.getId());
146+
example.setName(exampleObj.getName());
147+
148+
for (const subexampleObj of exampleObj.getSubexamples()) {
149+
const subexample = new SubExample();
150+
subexample.setDate(subexampleObj.getDate());
151+
subExamples.push(subexample);
152+
}
153+
example.setSubexamples(subExamples)
154+
155+
examples.push(example);
156+
}
157+
158+
const response = new ExampleResponse();
159+
response.setExamples(examples);
160+
return response;
161+
}
162+
}
97163
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.dirigible.samples.api.client;
2+
3+
import java.time.ZonedDateTime;
4+
5+
import io.dirigible.samples.api.domain.Example;
6+
import io.dirigible.samples.api.domain.SubExample;
7+
import io.dirigible.samples.api.domain.input.ExampleRequest;
8+
import io.dirigible.samples.api.domain.output.ExampleResponse;
9+
import io.dirigible.samples.api.service.ExampleService;
10+
11+
public class ExampleClient implements ExampleService {
12+
13+
@Override
14+
public ExampleResponse doExample(ExampleRequest request) {
15+
final var exampleResponse = new ExampleResponse();
16+
final var subexample = new SubExample().withDate(ZonedDateTime.now());
17+
final var example = new Example().withId(request.getExampleId()).withName("Example Name");
18+
example.getSubexamples().add(subexample);
19+
exampleResponse.getExamples().add(example);
20+
return exampleResponse;
21+
}
22+
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.dirigible.samples.api.domain;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Example {
7+
8+
private String id;
9+
10+
private String name;
11+
12+
private List<SubExample> subexamples = new ArrayList<>();
13+
14+
public String getId() {
15+
return id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public List<SubExample> getSubexamples() {
23+
return subexamples;
24+
}
25+
26+
public void setId(String id) {
27+
this.id = id;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public void setSubexamples(List<SubExample> subexamples) {
35+
this.subexamples = subexamples;
36+
}
37+
38+
public Example withId(String id) {
39+
setId(id);
40+
return this;
41+
}
42+
43+
public Example withName(String name) {
44+
setName(name);
45+
return this;
46+
}
47+
48+
public Example withSubexamples(List<SubExample> subexamples) {
49+
setSubexamples(subexamples);
50+
return this;
51+
}
52+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.dirigible.samples.api.domain;
2+
3+
import java.time.ZonedDateTime;
4+
5+
public class SubExample {
6+
7+
private ZonedDateTime date;
8+
9+
public ZonedDateTime getDate() {
10+
return date;
11+
}
12+
13+
public void setDate(ZonedDateTime date) {
14+
this.date = date;
15+
}
16+
17+
public SubExample withDate(ZonedDateTime date) {
18+
setDate(date);
19+
return this;
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.dirigible.samples.api.domain.input;
2+
3+
public class ExampleRequest {
4+
5+
private String exampleId;
6+
private String exampleName;
7+
8+
public String getExampleId() {
9+
return exampleId;
10+
}
11+
12+
public void setExampleId(String exampleId) {
13+
this.exampleId = exampleId;
14+
}
15+
16+
public String getExampleName() {
17+
return exampleName;
18+
}
19+
20+
public void setExampleName(String exampleName) {
21+
this.exampleName = exampleName;
22+
}
23+
24+
public ExampleRequest withExampleId(String exampleId) {
25+
setExampleId(exampleId);
26+
return this;
27+
}
28+
29+
public ExampleRequest withExampleName(String exampleName) {
30+
setExampleName(exampleName);
31+
return this;
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.dirigible.samples.api.domain.output;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import io.dirigible.samples.api.domain.Example;
7+
8+
public class ExampleResponse {
9+
10+
private List<Example> examples = new ArrayList<>();
11+
12+
public List<Example> getExamples() {
13+
return examples;
14+
}
15+
16+
public void setExamples(List<Example> examples) {
17+
this.examples = examples;
18+
}
19+
20+
public ExampleResponse withExamples(List<Example> examples) {
21+
setExamples(examples);
22+
return this;
23+
}
24+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.dirigible.samples.api.service;
2+
3+
import io.dirigible.samples.api.domain.input.ExampleRequest;
4+
import io.dirigible.samples.api.domain.output.ExampleResponse;
5+
6+
public interface ExampleService {
7+
8+
ExampleResponse doExample(ExampleRequest request);
9+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { SubExample } from "./SubExample";
2+
3+
export class Example {
4+
5+
// @ts-ignore
6+
private id: string;
7+
8+
// @ts-ignore
9+
private name: string;
10+
11+
// @ts-ignore
12+
private subexamples: SubExample[] = [];
13+
14+
public getId(): string {
15+
return this.id;
16+
}
17+
18+
public getName(): string {
19+
return this.name;
20+
}
21+
22+
public getSubexamples(): SubExample[] {
23+
return this.subexamples;
24+
}
25+
26+
public setId(id: string): void {
27+
this.id = id;
28+
}
29+
30+
public setName(name: string): void {
31+
this.name = name;
32+
}
33+
34+
public setSubexamples(subexamples: SubExample[]): void {
35+
this.subexamples = subexamples;
36+
}
37+
38+
public withId(id: string): Example {
39+
this.setId(id);
40+
return this;
41+
}
42+
43+
public withName(name: string): Example {
44+
this.setName(name);
45+
return this;
46+
}
47+
48+
public withSubexamples(subexamples: SubExample[]): Example {
49+
this.setSubexamples(subexamples);
50+
return this;
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ExampleResponse } from "./ExampleResponse";
2+
import { ExampleRequest } from "./ExampleRequest";
3+
import { Example } from "./Example";
4+
import { SubExample } from "./SubExample";
5+
6+
const ExampleClientClass = Java.type("io.dirigible.samples.api.client.ExampleClient");
7+
const ExampleRequestClass = Java.type("io.dirigible.samples.api.domain.input.ExampleRequest");
8+
9+
export class ExampleClient {
10+
11+
public doExample(request: ExampleRequest): ExampleResponse {
12+
const requestObj = new ExampleRequestClass();
13+
requestObj.setExampleId(request.getExampleId());
14+
requestObj.setExampleName(request.getExampleName());
15+
16+
const responseObj = new ExampleClientClass().doExample(requestObj);
17+
18+
const examples: Example[] = [];
19+
20+
for (const exampleObj of responseObj.getExamples()) {
21+
const example = new Example();
22+
const subExamples: SubExample[] = [];
23+
24+
example.setId(exampleObj.getId());
25+
example.setName(exampleObj.getName());
26+
27+
for (const subexampleObj of exampleObj.getSubexamples()) {
28+
const subexample = new SubExample();
29+
subexample.setDate(subexampleObj.getDate());
30+
subExamples.push(subexample);
31+
}
32+
example.setSubexamples(subExamples)
33+
34+
examples.push(example);
35+
}
36+
37+
const response = new ExampleResponse();
38+
response.setExamples(examples);
39+
return response;
40+
}
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export class ExampleRequest {
2+
3+
// @ts-ignore
4+
private exampleId: string;
5+
6+
// @ts-ignore
7+
private exampleName: string;
8+
9+
public getExampleId(): string {
10+
return this.exampleId;
11+
}
12+
13+
public setExampleId(exampleId: string): void {
14+
this.exampleId = exampleId;
15+
}
16+
17+
public getExampleName(): string {
18+
return this.exampleName;
19+
}
20+
21+
public setExampleName(exampleName: string): void {
22+
this.exampleName = exampleName;
23+
}
24+
25+
public withExampleId(exampleId: string): ExampleRequest {
26+
this.setExampleId(exampleId);
27+
return this;
28+
}
29+
30+
public withExampleName(exampleName: string): ExampleRequest {
31+
this.setExampleName(exampleName);
32+
return this;
33+
}
34+
}

0 commit comments

Comments
 (0)