From 8193366d6c2ad22107ac2770719593c5ac2480ee Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Tue, 19 Sep 2023 09:15:33 +0200 Subject: [PATCH] fixup! feat!: add proposal for new discovery API --- packages/core/src/wot-impl.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/core/src/wot-impl.ts b/packages/core/src/wot-impl.ts index 05a3d5bf9..ebb87da3e 100644 --- a/packages/core/src/wot-impl.ts +++ b/packages/core/src/wot-impl.ts @@ -54,20 +54,33 @@ class ThingDiscovery { const uriScheme = new URL(url).protocol.split(":")[0]; const client = this.servient.getClientFor(uriScheme); const result = await client.discoverDirectly(url); - const data = await ProtocolHelpers.readStreamFully(result.body); // TODO: Add TD validation // FIXME: application/td+json can't be handled at the moment - const value = ContentManager.contentToValue({ type: "application/json", body: data }, {}); + const value = ContentManager.contentToValue({ type: "application/json", body: await result.toBuffer() }, {}); if (value instanceof Object) { yield value as ThingDescription; } } - async *directory(url: string, filter?: WoT.ThingFilter): AsyncGenerator { - // Not implemented, do nothing + async *exploreDirectory(url: string, filter?: WoT.ThingFilter): AsyncGenerator { + yield Promise.reject(new Error("Unimplemented")); + } + + async requestThingDescription(url: string): Promise { + const uriScheme = new URL(url).protocol.split(":")[0]; + const client = this.servient.getClientFor(uriScheme); + const result = await client.discoverDirectly(url); + + const value = ContentManager.contentToValue({ type: result.type, body: await result.toBuffer() }, {}); + + if (value instanceof Object) { + return value as ThingDescription; + } + + throw new Error("Not found."); } }