Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: favor WoT.requestThingDescription instead of general fetch #1183

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Interacting with another WoT Thing is called consuming a Thing and works by usin
##### Fetch a Thing Description of a Thing given its URL

```javascript
WoTHelpers.fetch("http://localhost:8080/counter").then(async(td) => {
WoT.requestThingDescription("http://localhost:8080/counter").then(async(td) => {
// Do something with the TD
}
```
Expand All @@ -255,13 +255,13 @@ URLs can have various schemes, including `file://` to read from the local filesy
##### Consume a TD of a Thing, including parsing the TD and generating the protocol bindings in order to access lower level functionality

```javascript
WoTHelpers.fetch("http://localhost:8080/counter").then(async (td) => {
WoT.requestThingDescription("http://localhost:8080/counter").then(async (td) => {
let thing = WoT.consume(td);
// Do something with the consumed Thing
});
```

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

Expand Down Expand Up @@ -298,10 +298,10 @@ It is an asynchronous function that will take some time to complete.
So you should handle it explicitly.
Here we use the `async`/`await` functionality of NodeJS.

Declare the surrounding function as `async`, e.g., the `WoTHelpers.fetch()` resolve handler:
Declare the surrounding function as `async`, e.g., the `WoT.requestThingDescription()` resolve handler:

```javascript
WoTHelpers.fetch(myURI).then(async(td) => { ... });
WoT.requestThingDescription(myURI).then(async(td) => { ... });
```

Use `await` to make Promises synchronous (blocking):
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,13 @@ Now supposing you want to interact with the device, you have to consume its Thin
```JavaScript
// client.js
// Required steps to create a servient for a client
const { Servient, Helpers } = require("@node-wot/core");
const { Servient } = require("@node-wot/core");
const { HttpClientFactory } = require('@node-wot/binding-http');

const servient = new Servient();
servient.addClientFactory(new HttpClientFactory(null));
const WoTHelpers = new Helpers(servient);

WoTHelpers.fetch("http://localhost:8080/counter").then(async (td) => {
WoT.requestThingDescription("http://localhost:8080/counter").then(async (td) => {
try {
const WoT = await servient.start();
// Then from here on you can consume the thing
Expand Down
7 changes: 2 additions & 5 deletions packages/binding-coap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ The Thing Description is located under the following CoAP URI `coap://plugfest.t

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

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

const wotHelper = new Helpers(servient);
wotHelper
.fetch("coap://plugfest.thingweb.io:5683/testthing")
WoT.requestThingDescription("coap://plugfest.thingweb.io:5683/testthing")
JKRhb marked this conversation as resolved.
Show resolved Hide resolved
.then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
try {
const WoT = await servient.start();
const thing = await WoT.consume(td);
Expand Down
7 changes: 1 addition & 6 deletions packages/binding-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ The Thing Description is located under the following uri <http://plugfest.thingw
Servient = require("@node-wot/core").Servient;
HttpClientFactory = require("@node-wot/binding-http").HttpClientFactory;

Helpers = require("@node-wot/core").Helpers;

// create Servient and add HTTP binding
let servient = new Servient();
servient.addClientFactory(new HttpClientFactory(null));

let wotHelper = new Helpers(servient);
wotHelper
.fetch("http://plugfest.thingweb.io:8083/testthing")
WoT.requestThingDescription("http://plugfest.thingweb.io:8083/testthing")
.then(async (td) => {
// using await for serial execution (note 'async' in then() of fetch())
try {
const WoT = await servient.start();
const thing = await WoT.consume(td);
Expand Down
Loading