Skip to content

Commit

Permalink
feat: implement requestThingDescription method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 20, 2023
1 parent 6c415fe commit 4e890e0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -538,4 +538,12 @@ final class CoapClient implements ProtocolClient {
yield content;
}
}

@override
Future<Content> requestThingDescription(Uri url) async => _sendRequest(
url,
coap.RequestMethod.get,
form: null,
accept: coap.CoapMediaType.applicationTdJson,
);
}
14 changes: 14 additions & 0 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,18 @@ final class HttpClient implements ProtocolClient {

yield encodedLinks;
}

@override
Future<Content> requestThingDescription(Uri url) async {
final request = Request(HttpRequestMethod.get.methodName, url);
const tdContentType = 'application/td+json';
request.headers['Accept'] = tdContentType;

final response = await _client.send(request);

return Content(
response.headers['Content-Type'] ?? tdContentType,
response.stream,
);
}
}
6 changes: 6 additions & 0 deletions lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,10 @@ final class MqttClient implements ProtocolClient {
// TODO: implement discoverWithCoreLinkFormat
throw UnimplementedError();
}

@override
Future<Content> requestThingDescription(Uri url) {
// TODO: implement requestThingDescription
throw UnimplementedError();
}
}
3 changes: 3 additions & 0 deletions lib/src/core/protocol_interfaces/protocol_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ abstract interface class ProtocolClient {
void Function(Exception error)? error,
required void Function() complete,
});

/// Requests a Thing Description as [Content] from a [url].
Future<Content> requestThingDescription(Uri url);
}
17 changes: 17 additions & 0 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'exposed_thing.dart';
import 'protocol_interfaces/protocol_client.dart';
import 'protocol_interfaces/protocol_client_factory.dart';
import 'protocol_interfaces/protocol_server.dart';
import 'thing_discovery.dart';
import 'wot.dart';

/// Exception that is thrown by a [Servient].
Expand Down Expand Up @@ -225,4 +226,20 @@ class Servient {

return clientFactory.createClient();
}

/// Requests a [ThingDescription] from a [url].
Future<ThingDescription> requestThingDescription(Uri url) async {
final client = clientFor(url.scheme);
final content = await client.requestThingDescription(url);

final value = await contentSerdes.contentToValue(content, null);

if (value is! Map<String, dynamic>) {
throw DiscoveryException(
'Could not parse Thing Description obtained from $url',
);
}

return ThingDescription.fromJson(value);
}
}
5 changes: 5 additions & 0 deletions lib/src/core/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ class WoT implements scripting_api.WoT {
}) {
return ThingDiscovery(url, thingFilter, _servient, method: method);
}

@override
Future<ThingDescription> requestThingDescription(Uri url) {
return _servient.requestThingDescription(url);
}
}
3 changes: 3 additions & 0 deletions lib/src/scripting_api/wot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ abstract interface class WoT {
/// based on the underlying impementation.
Future<ExposedThing> produce(ExposedThingInit exposedThingInit);

/// Requests a [ThingDescription] from the given [url].
Future<ThingDescription> requestThingDescription(Uri url);

/// Discovers [ThingDescription]s from a given [url] using the specified
/// [method].
///
Expand Down

0 comments on commit 4e890e0

Please sign in to comment.