Skip to content

Commit

Permalink
feat!(augmented_form): expose resolvedHref as href
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 27, 2025
1 parent 06700a2 commit 5509b2d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 70 deletions.
8 changes: 4 additions & 4 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ final class CoapClient extends ProtocolClient
final code = requestMethod.code;

return _sendRequest(
form.resolvedHref,
form.href,
code,
content: content,
format: form.contentFormat,
Expand Down Expand Up @@ -232,7 +232,7 @@ final class CoapClient extends ProtocolClient
) async {
final requestMethod = (form.method ?? CoapRequestMethod.get).code;

final creationHintUri = form.resolvedHref.replace(scheme: "coap");
final creationHintUri = form.href.replace(scheme: "coap");

final request = await _createRequest(
requestMethod,
Expand Down Expand Up @@ -419,13 +419,13 @@ final class CoapClient extends ProtocolClient

final request = await _createRequest(
(form.method ?? CoapRequestMethod.get).code,
form.resolvedHref,
form.href,
format: form.contentFormat,
accept: form.accept,
);

final coapClient = coap.CoapClient(
form.resolvedHref,
form.href,
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
);

Expand Down
8 changes: 3 additions & 5 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ final class HttpClient extends ProtocolClient
return false;
}

final basicCredentials =
await _getBasicCredentials(form.resolvedHref, form);
final basicCredentials = await _getBasicCredentials(form.href, form);

if (basicCredentials == null) {
return false;
Expand All @@ -127,8 +126,7 @@ final class HttpClient extends ProtocolClient
return false;
}

final bearerCredentials =
await _getBearerCredentials(form.resolvedHref, form);
final bearerCredentials = await _getBearerCredentials(form.href, form);

if (bearerCredentials == null) {
return false;
Expand Down Expand Up @@ -222,7 +220,7 @@ final class HttpClient extends ProtocolClient
) async {
final requestMethod =
HttpRequestMethod.getRequestMethod(form, operationType);
final Uri uri = form.resolvedHref;
final Uri uri = form.href;

final request = Request(requestMethod.methodName, uri)
..headers.addAll(_getHeadersFromForm(form))
Expand Down
2 changes: 1 addition & 1 deletion lib/src/binding_http/http_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class HttpSseSubscription extends ProtocolSubscription {
void Function(Exception error)? onError,
void Function()? complete,
}) : _active = true,
_sseChannel = SseChannel.connect(form.resolvedHref) {
_sseChannel = SseChannel.connect(form.href) {
_sseChannel.stream.listen(
(data) {
if (data is! String) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class MqttClient extends ProtocolClient with MqttDiscoverer {
}

Future<MqttServerClient> _connectWithForm(AugmentedForm form) async =>
_connect(form.resolvedHref, form);
_connect(form.href, form);

Future<MqttServerClient> _connect(Uri brokerUri, AugmentedForm? form) async {
final client = brokerUri.createClient(_mqttConfig.keepAlivePeriod);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/binding_mqtt/mqtt_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ extension MqttFormExtension on AugmentedForm {
if (qosValue != null) {
throw FormatException(
"Encountered unknown QoS value $qosValue. "
"in form with resolved href $resolvedHref.",
"in form with resolved href $href.",
);
}

Expand Down
70 changes: 36 additions & 34 deletions lib/src/core/implementation/augmented_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ final class AugmentedForm implements Form {
@override
String get contentType => _form.contentType;

@override
Uri get href {
/// Resolves all [_userProvidedUriVariables] in this [Form] and returns the
/// resulting [Uri].
Uri get _resolvedHref {
final baseUri = _thingDescription.base;

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

@override
Uri get href {
final href = _resolvedHref;
final hrefUriVariables = _filterUriVariables(href);

if (hrefUriVariables.isEmpty) {
return href;
}

final Map<String, DataSchema> affordanceUriVariables = {
..._thingDescription.uriVariables ?? {},
..._interactionAffordance.uriVariables ?? {},
};

final userProvidedUriVariables = _userProvidedUriVariables;

if (userProvidedUriVariables != null) {
_validateUriVariables(
hrefUriVariables,
affordanceUriVariables,
userProvidedUriVariables,
);
}

// As "{" and "}" are "percent encoded" due to Uri.parse(), we need to
// revert the encoding first before we can insert the values.
final decodedHref = Uri.decodeFull(href.toString());

final expandedHref =
UriTemplate(decodedHref).expand(userProvidedUriVariables ?? {});
return Uri.parse(expandedHref);
}

@override
List<OperationType> get op =>
_form.op ?? OperationType.defaultOpValues(_interactionAffordance);
Expand Down Expand Up @@ -95,38 +129,6 @@ final class AugmentedForm implements Form {
.toList(growable: false);
}

/// Resolves all [_userProvidedUriVariables] in this [Form] and returns the
/// resulting [Uri].
Uri get resolvedHref {
final hrefUriVariables = _filterUriVariables(href);

if (hrefUriVariables.isEmpty) {
return href;
}

final Map<String, DataSchema> affordanceUriVariables = {
..._thingDescription.uriVariables ?? {},
..._interactionAffordance.uriVariables ?? {},
};

final userProvidedUriVariables = _userProvidedUriVariables;
if (userProvidedUriVariables != null) {
_validateUriVariables(
hrefUriVariables,
affordanceUriVariables,
userProvidedUriVariables,
);
}

// As "{" and "}" are "percent encoded" due to Uri.parse(), we need to
// revert the encoding first before we can insert the values.
final decodedHref = Uri.decodeFull(href.toString());

final expandedHref =
UriTemplate(decodedHref).expand(userProvidedUriVariables ?? {});
return Uri.parse(expandedHref);
}

void _validateUriVariables(
List<String> uriVariablesInHref,
Map<String, DataSchema> affordanceUriVariables,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/implementation/consumed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ConsumedThing implements scripting_api.ConsumedThing {
}

return servient.supportsOperation(
form.resolvedHref.scheme,
form.href.scheme,
operationType,
form.subprotocol,
);
Expand Down
27 changes: 5 additions & 22 deletions test/core/augmented_form_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,10 @@ void main() {
);

expect(
augmentedForm1.resolvedHref,
augmentedForm1.href,
Uri.parse("http://example.org/weather/?lat=5&long=10"),
);

expect(
augmentedForm1.resolvedHref != augmentedForm1.href,
isTrue,
);

final augmentedForm2 = AugmentedForm(
affordance.forms.first,
affordance,
Expand All @@ -163,7 +158,7 @@ void main() {
);

expect(
augmentedForm2.resolvedHref,
augmentedForm2.href,
Uri.parse("http://example.org/weather/?lat=5"),
);

Expand All @@ -177,7 +172,7 @@ void main() {
);

expect(
augmentedForm3.resolvedHref,
augmentedForm3.href,
Uri.parse("http://example.org/weather/?long=10"),
);

Expand All @@ -192,7 +187,7 @@ void main() {
);

expect(
() => augmentedForm4.resolvedHref,
() => augmentedForm4.href,
throwsA(isA<FormatException>()),
);

Expand All @@ -207,21 +202,9 @@ void main() {
);

expect(
() => augmentedForm5.resolvedHref,
() => augmentedForm5.href,
throwsA(isA<FormatException>()),
);

final augmentedForm6 = AugmentedForm(
affordance.forms[2],
affordance,
thingDescription,
const {},
);

expect(
augmentedForm6.href,
augmentedForm6.resolvedHref,
);
});
});
}
2 changes: 1 addition & 1 deletion test/core/discovery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ final class _MockedProtocolClient extends ProtocolClient with DirectDiscoverer {

@override
Future<Content> readResource(AugmentedForm form) async {
final href = form.resolvedHref;
final href = form.href;

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

0 comments on commit 5509b2d

Please sign in to comment.