Skip to content

Commit

Permalink
feat!: simplify usage of clientFactories
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 19, 2023
1 parent 3be4625 commit 2f20660
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 36 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ import 'package:dart_wot/dart_wot.dart';
Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory = CoapClientFactory();
final servient = Servient()..addClientFactory(coapClientFactory);
final servient = Servient(
protocolClients: [coapClientFactory]
);
final wot = await servient.start();
final thingDescriptionJson = '''
Expand Down
6 changes: 5 additions & 1 deletion example/coap_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ Future<void> handleThingDescription(
ThingDescription thingDescription,
) async {
final consumedThing = await wot.consume(thingDescription);

await consumedThing.writeProperty(propertyName, 'Hello World!');

var output = await consumedThing.readProperty(propertyName);
await output.printValue();

await consumedThing.writeProperty(propertyName, 'Bye World!');

output = await consumedThing.readProperty(propertyName);
await output.printValue();
}

Future<void> main(List<String> args) async {
final servient = Servient()..addClientFactory(CoapClientFactory());
final servient = Servient(clientFactories: [CoapClientFactory()]);

final wot = await servient.start();
final uri = Uri.parse('coap://plugfest.thingweb.io:5683/testthing');
Expand Down
6 changes: 5 additions & 1 deletion example/coap_dns_sd_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ void handleThingDescription(ThingDescription thingDescription) =>
print('Discovered TD with title "${thingDescription.title}".');

Future<void> main(List<String> args) async {
final servient = Servient()..addClientFactory(CoapClientFactory());
final servient = Servient(
clientFactories: [
CoapClientFactory(),
],
);

final wot = await servient.start();
final uri = Uri.parse('_wot._udp.local');
Expand Down
7 changes: 6 additions & 1 deletion example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ Future<void> main(List<String> args) async {
),
pskCredentialsCallback: _pskCredentialsCallback,
);
final servient = Servient()..addClientFactory(coapClientFactory);

final servient = Servient(
clientFactories: [
coapClientFactory,
],
);

final wot = await servient.start();

Expand Down
18 changes: 11 additions & 7 deletions example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,19 @@ Future<BasicCredentials?> basicCredentialsCallback(
}

Future<void> main() async {
const coapConfig = CoapConfig(blocksize: 64);
final CoapClientFactory coapClientFactory = CoapClientFactory(
coapConfig: coapConfig,
final coapClientFactory = CoapClientFactory(
coapConfig: const CoapConfig(blocksize: 64),
);
final HttpClientFactory httpClientFactory =

final httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final servient = Servient()
..addClientFactory(coapClientFactory)
..addClientFactory(httpClientFactory);

final servient = Servient(
clientFactories: [
coapClientFactory,
httpClientFactory,
],
);
final wot = await servient.start();

final thingDescription = ThingDescription(thingDescriptionJson);
Expand Down
2 changes: 1 addition & 1 deletion example/core_link_format_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const propertyName = 'status';
const actionName = 'toggle';

Future<void> main(List<String> args) async {
final servient = Servient()..addClientFactory(CoapClientFactory());
final servient = Servient(clientFactories: [CoapClientFactory()]);

final wot = await servient.start();

Expand Down
19 changes: 12 additions & 7 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ Future<BasicCredentials?> basicCredentialsCallback(
}

Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory = CoapClientFactory();
final HttpClientFactory httpClientFactory =
final coapClientFactory = CoapClientFactory();
final httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final MqttClientFactory mqttClientFactory = MqttClientFactory();
final servient = Servient()
..addClientFactory(coapClientFactory)
..addClientFactory(httpClientFactory)
..addClientFactory(mqttClientFactory);
final mqttClientFactory = MqttClientFactory();

final servient = Servient(
clientFactories: [
coapClientFactory,
httpClientFactory,
mqttClientFactory,
],
);

final wot = await servient.start();

const thingDescriptionJson = '''
Expand Down
11 changes: 8 additions & 3 deletions example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,14 @@ Future<BasicCredentials?> basicCredentialsCallback(
/// Illustrates the usage of both the basic and the automatic security scheme,
/// with a server supporting basic authentication.
Future<void> main(List<String> args) async {
final HttpClientFactory httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final servient = Servient()..addClientFactory(httpClientFactory);
final httpClientFactory = HttpClientFactory(
basicCredentialsCallback: basicCredentialsCallback,
);
final servient = Servient(
clientFactories: [
httpClientFactory,
],
);
final wot = await servient.start();

final thingDescription = ThingDescription(thingDescriptionJson);
Expand Down
25 changes: 16 additions & 9 deletions lib/src/core/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ class Servient {
/// A custom [contentSerdes] can be passed that supports other media types
/// than the default ones.
Servient({
required List<ProtocolClientFactory> clientFactories,
ServerSecurityCallback? serverSecurityCallback,
ContentSerdes? contentSerdes,
}) : contentSerdes = contentSerdes ?? ContentSerdes(),
}) : _clientFactories = _processProtocolClientFactories(clientFactories),
contentSerdes = contentSerdes ?? ContentSerdes(),
_serverSecurityCallback = serverSecurityCallback;

final List<ProtocolServer> _servers = [];
final Map<String, ProtocolClientFactory> _clientFactories = {};
final Map<String, ProtocolClientFactory> _clientFactories;
final Map<String, ExposedThing> _things = {};
final Map<String, ConsumedThing> _consumedThings = {};

Expand All @@ -55,6 +57,18 @@ class Servient {
/// The [ContentSerdes] object that is used for serializing/deserializing.
final ContentSerdes contentSerdes;

static Map<String, ProtocolClientFactory> _processProtocolClientFactories(
List<ProtocolClientFactory> protocolFactories,
) {
return Map.fromEntries(
protocolFactories.expand((protocolFactory) sync* {
for (final scheme in protocolFactory.schemes) {
yield MapEntry(scheme, protocolFactory);
}
}),
);
}

/// Starts this [Servient] and returns a [WoT] runtime object.
///
/// The [WoT] runtime can be used for consuming, procuding, and discovering
Expand Down Expand Up @@ -184,13 +198,6 @@ class Servient {
List<String> get clientSchemes =>
_clientFactories.keys.toList(growable: false);

/// Adds a new [clientFactory] to this [Servient.]
void addClientFactory(ProtocolClientFactory clientFactory) {
for (final scheme in clientFactory.schemes) {
_clientFactories[scheme] = clientFactory;
}
}

/// Checks whether a [ProtocolClient] is avaiable for a given [scheme].
bool hasClientFor(String scheme) => _clientFactories.containsKey(scheme);

Expand Down
7 changes: 4 additions & 3 deletions test/binding_http/http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ void main() {
]) async =>
bearerCredentialsStore[uri.host];

final servient = Servient()
..addClientFactory(
final servient = Servient(
clientFactories: [
HttpClientFactory(
basicCredentialsCallback: basicCredentialsCallback,
bearerCredentialsCallback: bearerCredentialsCallback,
),
);
],
);
final wot = await servient.start();

final consumedThing = await wot.consume(parsedTd);
Expand Down
2 changes: 1 addition & 1 deletion test/core/consumed_thing_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void main() {

final parsedTd = ThingDescription(thingDescriptionJson);

final servient = Servient()..addClientFactory(HttpClientFactory());
final servient = Servient(clientFactories: [HttpClientFactory()]);
final wot = await servient.start();

final uriVariables = {'value': 'SFRUUEJJTiBpcyBhd2Vzb21l'};
Expand Down
2 changes: 1 addition & 1 deletion test/core/dart_wot_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void main() {
});

test('Parse incomplete Thing Description', () async {
final servient = Servient();
final servient = Servient(clientFactories: []);
final wot = await servient.start();
final Map<String, dynamic> exposedThingInit = <String, dynamic>{
'@context': 'https://www.w3.org/2022/wot/td/v1.1',
Expand Down

0 comments on commit 2f20660

Please sign in to comment.