Skip to content

Commit 19f4506

Browse files
committed
Add an example of an external web client call
1 parent e0713d3 commit 19f4506

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

counter/webapp/public/authorized/count.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ <h1><a href="#" id="logout">Logout</a></h1>
1313
fetch("/api/user")
1414
.then(response => response.text(),
1515
err => user.innerHTML = err.message)
16-
.then(text => user.innerHTML = text == '' ? 'anonymous' : text);
16+
.then(text => user.innerHTML = text === '' ? 'anonymous' : text);
1717

1818
fetch("/api/count")
1919
.then(response => response.json(),
2020
err => count.innerHTML = err.message)
2121
.then(cnt => count.innerHTML =
22-
cnt == 1 ? 'first' :
23-
cnt == 2 ? 'second' :
24-
cnt == 3 ? 'third' :
22+
cnt === 1 ? 'first' :
23+
cnt === 2 ? 'second' :
24+
cnt === 3 ? 'third' :
2525
'' + cnt + '-th');
2626
};
2727

welcome/src/main/x/welcome.x

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module welcome.examples.org {
1010

1111
@WebService("/welcome")
1212
service SimpleApi {
13+
@Inject Client client;
1314
@Inject db.WelcomeSchema schema;
1415

1516
@Get("org")
@@ -18,6 +19,13 @@ module welcome.examples.org {
1819
return org;
1920
}
2021

22+
@Get("info")
23+
@Produces(Text)
24+
String callerInfo(RequestIn request) {
25+
ResponseIn response = client.get($"http://ip-api.com/json/{request.client}");
26+
return response.body?.bytes.unpackUtf8() : "";
27+
}
28+
2129
@Get("count")
2230
Int count(RequestIn request) {
2331
using (val tx = schema.connection.createTransaction()) {

welcome/webapp/src/App.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ class App extends Component {
55

66
constructor() {
77
super();
8-
this.state = {seconds: 0, name: "", count: ""};
8+
this.state = {seconds: 0, name: "", city: "", count: ""};
99
this.handleClick = this.handleClick.bind(this);
1010

1111
fetch('/welcome/org')
1212
.then(response => response.json())
1313
.then(name => this.setState(state => ({name: name})));
1414

15+
fetch('/welcome/info')
16+
.then(response => response.json())
17+
.then(info => {
18+
if (info["status"] === "fail") {
19+
this.setState(state => ({city: "your city"}));
20+
} else {
21+
this.setState(state => ({city: info["city"]}));
22+
}
23+
})
24+
1525
fetch('/welcome/count')
1626
.then(response => response.json())
1727
.then(data =>
@@ -45,6 +55,8 @@ class App extends Component {
4555
<div>
4656
Welcome to {this.state.name}! This is your {this.state.count} visit to our site!
4757
<p/>
58+
How's the weather in {this.state.city}?
59+
<p/>
4860
<button onClick={this.handleClick}>Reset timer</button>
4961
<p/>
5062
Timer:{this.state.seconds}

0 commit comments

Comments
 (0)