Skip to content

Commit d0c5f8c

Browse files
authored
Update text and examples regarding the fetching of TDs (#524)
1 parent 6482a8c commit d0c5f8c

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

index.html

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@
382382
Implementations that use TypeScript or ECMAScript in a runtime to implement the APIs defined in this document MUST implement them in a manner consistent with the TypeScript Bindings defined in the TypeScript specification [[!TYPESCRIPT]].
383383
</p>
384384
</section>
385-
385+
386386
<section> <h2>Terminology and conventions</h2>
387387
<p>
388388
The generic WoT terminology is defined in [[!WOT-ARCHITECTURE]]: <dfn data-lt="Things">Thing</dfn>, <dfn data-lt="Thing Descriptions">Thing Description</dfn> (in short <dfn>TD</dfn>), <dfn>Partial TD</dfn>, <dfn>Web of Things</dfn> (in short <b><i>WoT</i></b>), <dfn>WoT Interface</dfn>, <dfn>Protocol Bindings</dfn>, <dfn>WoT Runtime</dfn>, <dfn data-lt="consume|consume a TD|consuming a TD">Consuming a Thing Description</dfn>, <dfn>TD Directory</dfn>, <dfn data-lt="Properties">Property</dfn>, <dfn data-lt="Actions">Action</dfn>, <dfn data-lt="Events|WoT-Event">Event</dfn>,
@@ -489,19 +489,20 @@ <h2>The <dfn>ThingDescription</dfn> type</h2>
489489
[[!WOT-TD]]. It is expected to be
490490
a <a data-cite="infra#parse-json-bytes-to-a-javascript-value">parsed JSON object</a> that is validated using <a data-cite="WOT-TD#json-schema-for-validation">JSON Schema validation</a>.
491491
</p>
492-
<section> <h3> Fetching a Thing Description</h3>
492+
<section> <h3> Requesting a Thing Description</h3>
493493
<p>
494-
Fetching a <a>TD</a> given a URL should be done with an external method, such as the <a data-cite="fetch#fetch-api">Fetch API</a> or a HTTP client library, which offer already standardized options on specifying fetch details.
494+
Requesting a <a>TD</a> given a URL should be done with the
495+
<a href="#dom-wot-requestthingdescription">requestThingDescription()</a>
496+
method.
497+
Alternatively, external methods, such as the <a data-cite="fetch#fetch-api">Fetch API</a> or an HTTP client library, can be used.
495498
</p>
496-
<pre class="example" title="Fetching a Thing Description">
499+
<pre class="example" title="Requesting a Thing Description">
497500
try {
498-
let res = await fetch('https://tds.mythings.biz/sensor11');
499-
// ... additional checks possible on res.headers
500-
let td = await res.json();
501-
let thing = await WOT.consume(td);
501+
const td = await requestThingDescription('https://tds.mythings.biz/sensor11');
502+
const thing = await WoT.consume(td);
502503
console.log("Thing name: " + thing.getThingDescription().title);
503504
} catch (err) {
504-
console.log("Fetching TD failed", err.message);
505+
console.log("Requesting TD failed", err.message);
505506
}
506507
</pre>
507508
</section>
@@ -844,7 +845,7 @@ <h3>Validating an ExposedThingInit</h3>
844845
and stop.
845846
</li>
846847
<li>
847-
If fetching a <a>Thing Description</a> is not supported by the
848+
If requesting a <a>Thing Description</a> is not supported by the
848849
implementation, [=reject=] |promise| with {{NotSupportedError}} and
849850
stop.
850851
</li>
@@ -2023,13 +2024,13 @@ <h3>The <dfn>subscribeEvent()</dfn> method</h3>
20232024
It returns a {{Promise}} to signal success or failure.
20242025
<p class="ednote">
20252026
This algorithm allows for only one active {{Subscription}} per <a>Event</a>. If a new
2026-
{{Subscription}} is made while an existing {{Subscription}} is active the runtime
2027-
will throw an {{NotAllowedError}}.
2027+
{{Subscription}} is made while an existing {{Subscription}} is active the runtime
2028+
will throw an {{NotAllowedError}}.
20282029
</p>
20292030
The method MUST run the following steps:
20302031
<ol>
20312032
<li>
2032-
Let |thing| be the reference of this {{ConsumedThing}} object.
2033+
Let |thing| be the reference of this {{ConsumedThing}} object.
20332034
</li>
20342035
<li>
20352036
Return a {{Promise}} |promise:Promise| and execute the next steps [=in parallel=].
@@ -2322,7 +2323,7 @@ <h4>The <dfn>stop()</dfn> method</h4>
23222323
<li>
23232324
[=Resolve=] |promise|.
23242325
</li>
2325-
</ul>
2326+
</ul>
23262327
</li>
23272328
<li>
23282329
If the underlying platform receives further notifications for this
@@ -2772,7 +2773,7 @@ <h3>The <dfn>PropertyReadHandler</dfn> callback</h3>
27722773
Let |handler:function| be `null`.
27732774
</li>
27742775
<li>
2775-
If there is a user provided {{PropertyReadHandler}} in {{ExposedThing/[[readHandlers]]}} <a>internal slot</a>
2776+
If there is a user provided {{PropertyReadHandler}} in {{ExposedThing/[[readHandlers]]}} <a>internal slot</a>
27762777
for |interaction|, let |handler| be that.
27772778
</li>
27782779
<li>
@@ -3480,7 +3481,7 @@ <h3>The <dfn>EventSubscriptionHandler</dfn> callback</h3>
34803481
</ol>
34813482
</div>
34823483
</section>
3483-
3484+
34843485
<section> <h3>Handling <a>Events</a></h3>
34853486
<div>
34863487
When an <a>Event</a> with name |name| is emitted with
@@ -3500,8 +3501,8 @@ <h3>The <dfn>EventSubscriptionHandler</dfn> callback</h3>
35003501
its |options|.
35013502
</li>
35023503
<li>
3503-
If |data| is `undefined`, assume that the notification |response|
3504-
will contain an empty data payload as specified by <a>Protocol Bindings</a>.
3504+
If |data| is `undefined`, assume that the notification |response|
3505+
will contain an empty data payload as specified by <a>Protocol Bindings</a>.
35053506
</li>
35063507
<li>
35073508
If the underlying protocol stack permits conveying event errors and
@@ -4259,9 +4260,8 @@ <h2>API design rationale</h2>
42594260
represented indirectly.
42604261
</p>
42614262
<pre class="example" title="Open a lock">
4262-
let res = await fetch(‘https://td.my.com/lock-00123’);
4263-
let td = await res.json();
4264-
let lock = new ConsumedThing(td);
4263+
const lockTd = await WoT.requestThingDescription('https://td.my.com/lock-00123');
4264+
const lock = WoT.consume(lockTd);
42654265
console.log(lock.readProperty('status'));
42664266
lock.invokeAction('open', 'withThisKey');
42674267
</pre>
@@ -4280,16 +4280,31 @@ <h2>API design rationale</h2>
42804280
into consideration when designing a Scripting API for WoT.
42814281
</p>
42824282
</section>
4283-
<section> <h4>Fetching and validating a TD</h4>
4283+
<section>
4284+
<h4>Requesting and validating a TD</h4>
42844285
<p>
4285-
The <code>fetch(url)</code> method has been part of this API in earlier versions. However, now fetching a <a>TD</a> given a URL should be done with an external method, such as the <a data-cite="fetch#fetch-api">Fetch API</a> or a HTTP client library, which offer already standardized options on specifying fetch details. The reason is that while simple fetch operations (covering most use cases) could be done in this API, when various fetch options were needed, there was no point in duplicating existing work to re-expose those options in this API.
4286+
The current version of this specification defines a new
4287+
`requestThingDescription` method that simplifies the process of
4288+
retrieving and validating a Thing Description.
4289+
However, it only covers simple use cases that do not require additional
4290+
parameters or special HTTP headers for the retrieval.
42864291
</p>
42874292
<p>
4288-
Since fetching a <a>TD</a> has been scoped out, and <a>TD</a> validation
4289-
is defined externally in the [[[WOT-TD]]] specification, that is scoped out, too.
4290-
This specification expects a <a>TD</a> as
4291-
<a data-cite="infra#parse-json-bytes-to-a-javascript-value">parsed JSON object</a> that has been validated according to the [[[WOT-TD]]] specification.
4293+
More sophisticated use cases need to be covered by external methods, such
4294+
as the <a data-cite="fetch#fetch-api">Fetch API</a> or an
4295+
HTTP client library, which offer already standardized options on
4296+
specifying fetch details.
4297+
In these cases, the user is required to perform validation manually as
4298+
described by the [[[WoT-TD]]] specification.
42924299
</p>
4300+
<p class="ednote" title="Extending the `requestThingDescription()` method">
4301+
In the future, `requestThingDescription()` might be extended with an
4302+
`options` argument, including frequently used `fetch` options.
4303+
Please
4304+
<a href="https://github.com/w3c/wot-scripting-api/issues/new">
4305+
open an issue
4306+
</a> to request support for options.
4307+
</p>
42934308
</section>
42944309
<section> <h4>Observers</h4>
42954310
<p>

0 commit comments

Comments
 (0)