Skip to content

Commit

Permalink
fixup! feat!: add proposal for new discovery API
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 19, 2023
1 parent f24d9e7 commit 8193366
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/core/src/wot-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Check warning on line 65 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L54-L65

Added lines #L54 - L65 were not covered by tests
}

async *directory(url: string, filter?: WoT.ThingFilter): AsyncGenerator<ThingDescription> {
// Not implemented, do nothing
async *exploreDirectory(url: string, filter?: WoT.ThingFilter): AsyncGenerator<ThingDescription> {
yield Promise.reject(new Error("Unimplemented"));
}

Check warning on line 70 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L69-L70

Added lines #L69 - L70 were not covered by tests

async requestThingDescription(url: string): Promise<ThingDescription> {
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.");

Check warning on line 83 in packages/core/src/wot-impl.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/wot-impl.ts#L73-L83

Added lines #L73 - L83 were not covered by tests
}
}

Expand Down

0 comments on commit 8193366

Please sign in to comment.