Skip to content

Commit 1db1c11

Browse files
authored
Merge pull request #1183 from danielpeintner/issue-1055-v2
refactor: favor WoT.requestThingDescription instead of general fetch
2 parents be1fd88 + 237b02c commit 1db1c11

14 files changed

Lines changed: 50 additions & 92 deletions

File tree

API.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Interacting with another WoT Thing is called consuming a Thing and works by usin
245245
##### Fetch a Thing Description of a Thing given its URL
246246
247247
```javascript
248-
WoTHelpers.fetch("http://localhost:8080/counter").then(async(td) => {
248+
WoT.requestThingDescription("http://localhost:8080/counter").then(async(td) => {
249249
// Do something with the TD
250250
}
251251
```
@@ -255,13 +255,13 @@ URLs can have various schemes, including `file://` to read from the local filesy
255255
##### Consume a TD of a Thing, including parsing the TD and generating the protocol bindings in order to access lower level functionality
256256

257257
```javascript
258-
WoTHelpers.fetch("http://localhost:8080/counter").then(async (td) => {
258+
WoT.requestThingDescription("http://localhost:8080/counter").then(async (td) => {
259259
let thing = WoT.consume(td);
260260
// Do something with the consumed Thing
261261
});
262262
```
263263

264-
Things can be `consume`d no matter if they were fetched with WoTHelpers or not.
264+
Things can be `consume`d no matter if they were fetched with `WoT.requestThingDescription()`, `WoTHelpers.fetch()` or any other mean.
265265
`consume` only requires a TD as an `Object`, so you could also use `fs.readFile` and `JSON.parse` or inline it into your code.
266266
As long at it results in a TD Object, you can receive it over Fax, Morse it or use smoke signals.
267267

@@ -298,10 +298,10 @@ It is an asynchronous function that will take some time to complete.
298298
So you should handle it explicitly.
299299
Here we use the `async`/`await` functionality of NodeJS.
300300
301-
Declare the surrounding function as `async`, e.g., the `WoTHelpers.fetch()` resolve handler:
301+
Declare the surrounding function as `async`, e.g., the `WoT.requestThingDescription()` resolve handler:
302302
303303
```javascript
304-
WoTHelpers.fetch(myURI).then(async(td) => { ... });
304+
WoT.requestThingDescription(myURI).then(async(td) => { ... });
305305
```
306306
307307
Use `await` to make Promises synchronous (blocking):

README.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,21 @@ Now supposing you want to interact with the device, you have to consume its Thin
180180
```JavaScript
181181
// client.js
182182
// Required steps to create a servient for a client
183-
const { Servient, Helpers } = require("@node-wot/core");
183+
const { Servient } = require("@node-wot/core");
184184
const { HttpClientFactory } = require('@node-wot/binding-http');
185185

186186
const servient = new Servient();
187187
servient.addClientFactory(new HttpClientFactory(null));
188-
const WoTHelpers = new Helpers(servient);
189-
190-
WoTHelpers.fetch("http://localhost:8080/counter").then(async (td) => {
191-
try {
192-
const WoT = await servient.start();
193-
// Then from here on you can consume the thing
194-
let thing = await WoT.consume(td);
195-
thing.observeProperty("count", async (data) => { console.log("count:", await data.value()); });
196-
for (let i = 0; i < 5; i++) {
197-
await thing.invokeAction("increment");
198-
}
199-
}
200-
catch (err) {
201-
console.error("Script error:", err);
188+
189+
servient.start().then(async (WoT) => {
190+
const td = await WoT.requestThingDescription("http://localhost:8080/counter");
191+
// Then from here on you can consume the thing
192+
let thing = await WoT.consume(td);
193+
thing.observeProperty("count", async (data) => { console.log("count:", await data.value()); });
194+
for (let i = 0; i < 5; i++) {
195+
await thing.invokeAction("increment");
202196
}
203-
}).catch((err) => { console.error("Fetch error:", err); });
197+
}).catch((err) => { console.error(err); });
204198
```
205199

206200
If you execute both scripts you will see `count: ${count}` printed 5 times. We host a more complex version of this example at [http://plugfest.thingweb.io/examples/counter.html](http://plugfest.thingweb.io/examples/counter.html) and you can find the source code in the [counter example](./examples/browser) folder. You can also find more examples in the [examples folder](./examples/scripts) for JavaScript and in the [examples folder](./packages/examples/) for TypeScript. Finally, for your convenience, we host a set of online Things that you can use to test your applications. You can find more information about them in the [Online Things](#online-things) section.

examples/scripts/counter-client.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ function getFormIndexForDecrementWithCoAP(thing) {
2626
// return formIndex: 0 if no CoAP target IRI found
2727
return 0;
2828
}
29-
WoTHelpers.fetch("coap://localhost:5683/counter")
29+
WoT.requestThingDescription("coap://localhost:5683/counter")
3030
.then(async (td) => {
31-
// using await for serial execution (note 'async' in then() of fetch())
3231
try {
3332
const thing = await WoT.consume(td);
3433
console.info("=== TD ===");

examples/scripts/smart-coffee-machine-client.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
// This is an example of Web of Things consumer ("client" mode) Thing script.
1616
// It considers a fictional smart coffee machine in order to demonstrate the capabilities of Web of Things.
1717
// An accompanying tutorial is available at http://www.thingweb.io/smart-coffee-machine.html.
18-
1918
// Print data and an accompanying message in a distinguishable way
2019
function log(msg, data) {
2120
console.info("======================");
2221
console.info(msg);
2322
console.dir(data);
2423
console.info("======================");
2524
}
26-
WoTHelpers.fetch("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) => {
25+
WoT.requestThingDescription("http://127.0.0.1:8080/smart-coffee-machine").then(async (td) => {
2726
try {
2827
const thing = await WoT.consume(td);
2928
log("Thing Description:", td);

examples/security/oauth/consumer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
1414
********************************************************************************/
15-
WoTHelpers.fetch("https://localhost:8080/oauth").then((td) => {
15+
WoT.requestThingDescription("https://localhost:8080/oauth").then((td) => {
1616
WoT.consume(td).then(async (thing) => {
1717
try {
1818
const resp = await thing.invokeAction("sayOk");

examples/testthing/testclient.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ async function testPropertyWrite(thing, name, value, shouldFail) {
3939
else console.info("PASS " + name + " WRITE (" + displayValue + "):", JSON.stringify(err));
4040
}
4141
}
42-
WoTHelpers.fetch("http://localhost:8080/testthing")
42+
WoT.requestThingDescription("http://localhost:8080/testthing")
4343
.then(async (td) => {
44-
// using await for serial execution (note 'async' in then() of fetch())
4544
try {
4645
const thing = await WoT.consume(td);
4746
console.info("=== TD ===");

packages/binding-coap/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ The Thing Description is located under the following CoAP URI `coap://plugfest.t
2626

2727
```js
2828
// example-client.js
29-
const { Servient, Helpers } = require("@node-wot/core");
29+
const { Servient } = require("@node-wot/core");
3030
const { CoapClientFactory } = require("@node-wot/binding-coap");
3131

3232
// create Servient and add CoAP binding
3333
const servient = new Servient();
3434
servient.addClientFactory(new CoapClientFactory());
3535

36-
const wotHelper = new Helpers(servient);
37-
wotHelper
38-
.fetch("coap://plugfest.thingweb.io:5683/testthing")
39-
.then(async (td) => {
40-
// using await for serial execution (note 'async' in then() of fetch())
36+
servient
37+
.start()
38+
.then(async (WoT) => {
4139
try {
42-
const WoT = await servient.start();
40+
const td = await WoT.requestThingDescription("coap://plugfest.thingweb.io:5683/testthing");
4341
const thing = await WoT.consume(td);
4442

4543
// read property
@@ -50,7 +48,7 @@ wotHelper
5048
}
5149
})
5250
.catch((err) => {
53-
console.error("Fetch error:", err);
51+
console.error("Start error:", err);
5452
});
5553
```
5654

packages/binding-http/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,15 @@ The Thing Description is located under the following uri <http://plugfest.thingw
2929
Servient = require("@node-wot/core").Servient;
3030
HttpClientFactory = require("@node-wot/binding-http").HttpClientFactory;
3131

32-
Helpers = require("@node-wot/core").Helpers;
33-
3432
// create Servient and add HTTP binding
3533
let servient = new Servient();
3634
servient.addClientFactory(new HttpClientFactory(null));
3735

38-
let wotHelper = new Helpers(servient);
39-
wotHelper
40-
.fetch("http://plugfest.thingweb.io:8083/testthing")
41-
.then(async (td) => {
42-
// using await for serial execution (note 'async' in then() of fetch())
36+
servient
37+
.start()
38+
.then(async (WoT) => {
4339
try {
44-
const WoT = await servient.start();
40+
const td = await WoT.requestThingDescription("http://plugfest.thingweb.io:8083/testthing");
4541
const thing = await WoT.consume(td);
4642

4743
// read property
@@ -52,7 +48,7 @@ wotHelper
5248
}
5349
})
5450
.catch((err) => {
55-
console.error("Fetch error:", err);
51+
console.error("Start error:", err);
5652
});
5753
```
5854

packages/examples/src/bindings/coap/example-client.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@
1313
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
1414
********************************************************************************/
1515

16-
import { Servient, Helpers } from "@node-wot/core";
16+
import { Servient } from "@node-wot/core";
1717
import { CoapClientFactory } from "@node-wot/binding-coap";
18-
import { ThingDescription } from "wot-typescript-definitions";
1918

2019
// create Servient and add CoAP binding
2120
const servient = new Servient();
2221
servient.addClientFactory(new CoapClientFactory());
2322

24-
const wotHelper = new Helpers(servient);
25-
wotHelper
26-
.fetch("coap://plugfest.thingweb.io:5683/testthing")
27-
.then(async (fetched) => {
28-
const td: ThingDescription = fetched as ThingDescription;
29-
// using await for serial execution (note 'async' in then() of fetch())
23+
servient
24+
.start()
25+
.then(async (WoT) => {
3026
try {
31-
const WoT = await servient.start();
27+
const td = await WoT.requestThingDescription("coap://plugfest.thingweb.io:5683/testthing");
3228
const thing = await WoT.consume(td);
3329

3430
// read property
@@ -39,5 +35,5 @@ wotHelper
3935
}
4036
})
4137
.catch((err) => {
42-
console.error("Fetch error:", err);
38+
console.error("Start error:", err);
4339
});

packages/examples/src/bindings/http/example-client.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@
1313
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
1414
********************************************************************************/
1515

16-
import { Servient, Helpers } from "@node-wot/core";
16+
import { Servient } from "@node-wot/core";
1717
import { HttpClientFactory } from "@node-wot/binding-http";
18-
import { ThingDescription } from "wot-typescript-definitions";
1918

2019
// create Servient and add HTTP binding
2120
const servient = new Servient();
2221
servient.addClientFactory(new HttpClientFactory());
2322

24-
const wotHelper = new Helpers(servient);
25-
wotHelper
26-
.fetch("http://plugfest.thingweb.io:8083/testthing")
27-
.then(async (fetched) => {
28-
const td: ThingDescription = fetched as ThingDescription;
29-
// using await for serial execution (note 'async' in then() of fetch())
23+
servient
24+
.start()
25+
.then(async (WoT) => {
3026
try {
31-
const WoT = await servient.start();
27+
const td = await WoT.requestThingDescription("http://plugfest.thingweb.io:8083/testthing");
3228
const thing = await WoT.consume(td);
3329

3430
// read property
@@ -39,5 +35,5 @@ wotHelper
3935
}
4036
})
4137
.catch((err) => {
42-
console.error("Fetch error:", err);
38+
console.error("Start error:", err);
4339
});

0 commit comments

Comments
 (0)