Skip to content

Commit 737bc39

Browse files
committed
feat!(augmented_form): expose resolvedHref as href
1 parent 4471213 commit 737bc39

File tree

9 files changed

+53
-70
lines changed

9 files changed

+53
-70
lines changed

lib/src/binding_coap/coap_client.dart

+4-4
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

+3-5
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

+1-1
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ extension MqttFormExtension on AugmentedForm {
140140
if (qosValue != null) {
141141
throw FormatException(
142142
"Encountered unknown QoS value $qosValue. "
143-
"in form with resolved href $resolvedHref.",
143+
"in form with resolved href $href.",
144144
);
145145
}
146146

lib/src/core/implementation/augmented_form.dart

+36-34
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ final class AugmentedForm implements Form {
4444
@override
4545
String get contentType => _form.contentType;
4646

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

5152
if (baseUri != null) {
@@ -55,6 +56,39 @@ final class AugmentedForm implements Form {
5556
return _form.href;
5657
}
5758

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+
5892
@override
5993
List<OperationType> get op =>
6094
_form.op ?? OperationType.defaultOpValues(_interactionAffordance);
@@ -95,38 +129,6 @@ final class AugmentedForm implements Form {
95129
.toList(growable: false);
96130
}
97131

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

lib/src/core/implementation/consumed_thing.dart

+1-1
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
);

test/core/augmented_form_test.dart

+5-22
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,10 @@ void main() {
144144
);
145145

146146
expect(
147-
augmentedForm1.resolvedHref,
147+
augmentedForm1.href,
148148
Uri.parse("http://example.org/weather/?lat=5&long=10"),
149149
);
150150

151-
expect(
152-
augmentedForm1.resolvedHref != augmentedForm1.href,
153-
isTrue,
154-
);
155-
156151
final augmentedForm2 = AugmentedForm(
157152
affordance.forms.first,
158153
affordance,
@@ -163,7 +158,7 @@ void main() {
163158
);
164159

165160
expect(
166-
augmentedForm2.resolvedHref,
161+
augmentedForm2.href,
167162
Uri.parse("http://example.org/weather/?lat=5"),
168163
);
169164

@@ -177,7 +172,7 @@ void main() {
177172
);
178173

179174
expect(
180-
augmentedForm3.resolvedHref,
175+
augmentedForm3.href,
181176
Uri.parse("http://example.org/weather/?long=10"),
182177
);
183178

@@ -192,7 +187,7 @@ void main() {
192187
);
193188

194189
expect(
195-
() => augmentedForm4.resolvedHref,
190+
() => augmentedForm4.href,
196191
throwsA(isA<FormatException>()),
197192
);
198193

@@ -207,21 +202,9 @@ void main() {
207202
);
208203

209204
expect(
210-
() => augmentedForm5.resolvedHref,
205+
() => augmentedForm5.href,
211206
throwsA(isA<FormatException>()),
212207
);
213-
214-
final augmentedForm6 = AugmentedForm(
215-
affordance.forms[2],
216-
affordance,
217-
thingDescription,
218-
const {},
219-
);
220-
221-
expect(
222-
augmentedForm6.href,
223-
augmentedForm6.resolvedHref,
224-
);
225208
});
226209
});
227210
}

test/core/discovery_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ final class _MockedProtocolClient extends ProtocolClient with DirectDiscoverer {
147147

148148
@override
149149
Future<Content> readResource(AugmentedForm form) async {
150-
final href = form.resolvedHref;
150+
final href = form.href;
151151

152152
if (href == directoryTestThingsUri1) {
153153
return "[$validTestThingDescription]".toContent("application/td+json");

0 commit comments

Comments
 (0)