Skip to content

Commit 2e4e5c1

Browse files
authored
Merge pull request #209 from eclipse-thingweb/augmented-form-adjustment
chore!: do not expose the TD ID via AugmentedForms
2 parents fdc099a + 737bc39 commit 2e4e5c1

File tree

12 files changed

+66
-98
lines changed

12 files changed

+66
-98
lines changed

example/complex_example.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,15 @@ const thingDescriptionJson = {
7676
};
7777

7878
final Map<String, BasicCredentials> basicCredentials = {
79-
"urn:test": const BasicCredentials("username", "password"),
79+
"httpbin.org": const BasicCredentials("username", "password"),
8080
};
8181

8282
Future<BasicCredentials?> basicCredentialsCallback(
8383
Uri uri,
84-
AugmentedForm? form, [
84+
AugmentedForm? form,
8585
BasicCredentials? invalidCredentials,
86-
]) async {
87-
final id = form?.tdIdentifier;
88-
89-
return basicCredentials[id];
90-
}
86+
) async =>
87+
basicCredentials[uri.authority];
9188

9289
Future<void> main() async {
9390
final coapClientFactory = CoapClientFactory(

example/http_basic_authentication.dart

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,15 @@ const thingDescriptionJson = {
3838
const basicCredentials = BasicCredentials("username", "password");
3939

4040
final Map<String, BasicCredentials> basicCredentialsMap = {
41-
"urn:test": basicCredentials,
41+
"httpbin.org": basicCredentials,
4242
};
4343

4444
Future<BasicCredentials?> basicCredentialsCallback(
4545
Uri uri,
4646
AugmentedForm? form,
4747
BasicCredentials? invalidCredentials,
48-
) async {
49-
if (form == null) {
50-
return basicCredentials;
51-
}
52-
53-
final id = form.tdIdentifier;
54-
55-
return basicCredentialsMap[id];
56-
}
48+
) async =>
49+
basicCredentialsMap[uri.authority];
5750

5851
/// Illustrates the usage of both the basic and the automatic security scheme,
5952
/// with a server supporting basic authentication.

example/mqtt_example.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ const thingDescriptionJson = {
4646
};
4747

4848
final Map<String, BasicCredentials> basicCredentials = {
49-
"urn:test": const BasicCredentials("rw", "readwrite"),
49+
"test.mosquitto.org:1884": const BasicCredentials(
50+
"rw",
51+
"readwrite",
52+
),
5053
};
5154

5255
Future<BasicCredentials?> basicCredentialsCallback(
5356
Uri uri,
5457
AugmentedForm? form, [
5558
BasicCredentials? invalidCredentials,
56-
]) async {
57-
final id = form?.tdIdentifier;
58-
59-
return basicCredentials[id];
60-
}
59+
]) async =>
60+
basicCredentials[uri.authority];
6161

6262
Future<void> main(List<String> args) async {
6363
final servient = Servient.create(

lib/src/binding_coap/coap_client.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ final class CoapClient extends ProtocolClient
132132
final code = requestMethod.code;
133133

134134
return _sendRequest(
135-
form.resolvedHref,
135+
form.href,
136136
code,
137137
content: content,
138138
format: form.contentFormat,
@@ -232,7 +232,7 @@ final class CoapClient extends ProtocolClient
232232
) async {
233233
final requestMethod = (form.method ?? CoapRequestMethod.get).code;
234234

235-
final creationHintUri = form.resolvedHref.replace(scheme: "coap");
235+
final creationHintUri = form.href.replace(scheme: "coap");
236236

237237
final request = await _createRequest(
238238
requestMethod,
@@ -419,13 +419,13 @@ final class CoapClient extends ProtocolClient
419419

420420
final request = await _createRequest(
421421
(form.method ?? CoapRequestMethod.get).code,
422-
form.resolvedHref,
422+
form.href,
423423
format: form.contentFormat,
424424
accept: form.accept,
425425
);
426426

427427
final coapClient = coap.CoapClient(
428-
form.resolvedHref,
428+
form.href,
429429
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
430430
);
431431

lib/src/binding_http/http_client.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ final class HttpClient extends ProtocolClient
104104
return false;
105105
}
106106

107-
final basicCredentials =
108-
await _getBasicCredentials(form.resolvedHref, form);
107+
final basicCredentials = await _getBasicCredentials(form.href, form);
109108

110109
if (basicCredentials == null) {
111110
return false;
@@ -127,8 +126,7 @@ final class HttpClient extends ProtocolClient
127126
return false;
128127
}
129128

130-
final bearerCredentials =
131-
await _getBearerCredentials(form.resolvedHref, form);
129+
final bearerCredentials = await _getBearerCredentials(form.href, form);
132130

133131
if (bearerCredentials == null) {
134132
return false;
@@ -222,7 +220,7 @@ final class HttpClient extends ProtocolClient
222220
) async {
223221
final requestMethod =
224222
HttpRequestMethod.getRequestMethod(form, operationType);
225-
final Uri uri = form.resolvedHref;
223+
final Uri uri = form.href;
226224

227225
final request = Request(requestMethod.methodName, uri)
228226
..headers.addAll(_getHeadersFromForm(form))

lib/src/binding_http/http_subscription.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class HttpSseSubscription extends ProtocolSubscription {
2020
void Function(Exception error)? onError,
2121
void Function()? complete,
2222
}) : _active = true,
23-
_sseChannel = SseChannel.connect(form.resolvedHref) {
23+
_sseChannel = SseChannel.connect(form.href) {
2424
_sseChannel.stream.listen(
2525
(data) {
2626
if (data is! String) {

lib/src/binding_mqtt/mqtt_client.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class MqttClient extends ProtocolClient with MqttDiscoverer {
6868
}
6969

7070
Future<MqttServerClient> _connectWithForm(AugmentedForm form) async =>
71-
_connect(form.resolvedHref, form);
71+
_connect(form.href, form);
7272

7373
Future<MqttServerClient> _connect(Uri brokerUri, AugmentedForm? form) async {
7474
final client = brokerUri.createClient(_mqttConfig.keepAlivePeriod);

lib/src/binding_mqtt/mqtt_extensions.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ extension MqttFormExtension on AugmentedForm {
140140
if (qosValue != null) {
141141
throw FormatException(
142142
"Encountered unknown QoS value $qosValue. "
143-
"in form with href $href of Thing Description with Identifier "
144-
"$tdIdentifier.",
143+
"in form with resolved href $href.",
145144
);
146145
}
147146

lib/src/core/implementation/augmented_form.dart

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ final class AugmentedForm implements Form {
3131

3232
final Map<String, Object>? _userProvidedUriVariables;
3333

34-
/// The identifier of the [_thingDescription] associated with this form.
35-
String get tdIdentifier => _thingDescription.identifier;
36-
3734
@override
3835
Map<String, dynamic> get additionalFields => _form.additionalFields;
3936

@@ -47,8 +44,9 @@ final class AugmentedForm implements Form {
4744
@override
4845
String get contentType => _form.contentType;
4946

50-
@override
51-
Uri get href {
47+
/// Resolves all [_userProvidedUriVariables] in this [Form] and returns the
48+
/// resulting [Uri].
49+
Uri get _resolvedHref {
5250
final baseUri = _thingDescription.base;
5351

5452
if (baseUri != null) {
@@ -58,6 +56,39 @@ final class AugmentedForm implements Form {
5856
return _form.href;
5957
}
6058

59+
@override
60+
Uri get href {
61+
final href = _resolvedHref;
62+
final hrefUriVariables = _filterUriVariables(href);
63+
64+
if (hrefUriVariables.isEmpty) {
65+
return href;
66+
}
67+
68+
final Map<String, DataSchema> affordanceUriVariables = {
69+
..._thingDescription.uriVariables ?? {},
70+
..._interactionAffordance.uriVariables ?? {},
71+
};
72+
73+
final userProvidedUriVariables = _userProvidedUriVariables;
74+
75+
if (userProvidedUriVariables != null) {
76+
_validateUriVariables(
77+
hrefUriVariables,
78+
affordanceUriVariables,
79+
userProvidedUriVariables,
80+
);
81+
}
82+
83+
// As "{" and "}" are "percent encoded" due to Uri.parse(), we need to
84+
// revert the encoding first before we can insert the values.
85+
final decodedHref = Uri.decodeFull(href.toString());
86+
87+
final expandedHref =
88+
UriTemplate(decodedHref).expand(userProvidedUriVariables ?? {});
89+
return Uri.parse(expandedHref);
90+
}
91+
6192
@override
6293
List<OperationType> get op =>
6394
_form.op ?? OperationType.defaultOpValues(_interactionAffordance);
@@ -98,38 +129,6 @@ final class AugmentedForm implements Form {
98129
.toList(growable: false);
99130
}
100131

101-
/// Resolves all [_userProvidedUriVariables] in this [Form] and returns the
102-
/// resulting [Uri].
103-
Uri get resolvedHref {
104-
final hrefUriVariables = _filterUriVariables(href);
105-
106-
if (hrefUriVariables.isEmpty) {
107-
return href;
108-
}
109-
110-
final Map<String, DataSchema> affordanceUriVariables = {
111-
..._thingDescription.uriVariables ?? {},
112-
..._interactionAffordance.uriVariables ?? {},
113-
};
114-
115-
final userProvidedUriVariables = _userProvidedUriVariables;
116-
if (userProvidedUriVariables != null) {
117-
_validateUriVariables(
118-
hrefUriVariables,
119-
affordanceUriVariables,
120-
userProvidedUriVariables,
121-
);
122-
}
123-
124-
// As "{" and "}" are "percent encoded" due to Uri.parse(), we need to
125-
// revert the encoding first before we can insert the values.
126-
final decodedHref = Uri.decodeFull(href.toString());
127-
128-
final expandedHref =
129-
UriTemplate(decodedHref).expand(userProvidedUriVariables ?? {});
130-
return Uri.parse(expandedHref);
131-
}
132-
133132
void _validateUriVariables(
134133
List<String> uriVariablesInHref,
135134
Map<String, DataSchema> affordanceUriVariables,

lib/src/core/implementation/consumed_thing.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ConsumedThing implements scripting_api.ConsumedThing {
7878
}
7979

8080
return servient.supportsOperation(
81-
form.resolvedHref.scheme,
81+
form.href.scheme,
8282
operationType,
8383
form.subprotocol,
8484
);

0 commit comments

Comments
 (0)