Skip to content

Commit

Permalink
fixup! feat!: improve TD serialization behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 3, 2024
1 parent 23898a3 commit ec9582f
Show file tree
Hide file tree
Showing 28 changed files with 656 additions and 625 deletions.
4 changes: 2 additions & 2 deletions example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ final Map<String, BasicCredentials> basicCredentials = {

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form, [
AugmentedForm? form, [
BasicCredentials? invalidCredentials,
]) async {
final id = form?.thingDescription.identifier;
final id = form?.tdIdentifier;

return basicCredentials[id];
}
Expand Down
4 changes: 2 additions & 2 deletions example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ final Map<String, BasicCredentials> basicCredentialsMap = {

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form,
AugmentedForm? form,
BasicCredentials? invalidCredentials,
) async {
if (form == null) {
return basicCredentials;
}

final id = form.thingDescription.identifier;
final id = form.tdIdentifier;

return basicCredentialsMap[id];
}
Expand Down
4 changes: 2 additions & 2 deletions example/mqtt_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ final Map<String, BasicCredentials> basicCredentials = {

Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form, [
AugmentedForm? form, [
BasicCredentials? invalidCredentials,
]) async {
final id = form?.thingDescription.identifier;
final id = form?.tdIdentifier;

return basicCredentials[id];
}
Expand Down
1 change: 1 addition & 0 deletions lib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ library core;

export "package:dcaf/dcaf.dart";

export "src/core/augmented_form.dart";
export "src/core/codecs/content_codec.dart";
export "src/core/content_serdes.dart";
export "src/core/credentials/ace_credentials.dart";
Expand Down
27 changes: 14 additions & 13 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "package:coap/coap.dart" as coap;
import "package:coap/config/coap_config_default.dart";
import "package:dcaf/dcaf.dart";

import "../core/augmented_form.dart";
import "../core/content.dart";
import "../core/credentials/ace_credentials.dart";
import "../core/credentials/callbacks.dart";
Expand Down Expand Up @@ -52,7 +53,7 @@ class _InternalCoapConfig extends CoapConfigDefault {

coap.PskCredentialsCallback? _createPskCallback(
Uri uri,
Form? form, {
AugmentedForm? form, {
ClientPskCallback? pskCredentialsCallback,
}) {
final usesPskScheme = form?.usesPskScheme ?? false;
Expand Down Expand Up @@ -126,7 +127,7 @@ final class CoapClient implements ProtocolClient {
}

Future<Content> _sendRequestFromForm(
Form form,
AugmentedForm form,
OperationType operationType, [
Content? content,
]) async {
Expand All @@ -151,7 +152,7 @@ final class CoapClient implements ProtocolClient {
Uri uri,
coap.RequestMethod method, {
Content? content,
required Form? form,
required AugmentedForm? form,
coap.CoapMediaType? format,
coap.CoapMediaType? accept,
coap.BlockSize? block1Size,
Expand Down Expand Up @@ -207,7 +208,7 @@ final class CoapClient implements ProtocolClient {
Uri uri,
coap.RequestMethod method, {
Content? content,
required Form? form,
required AugmentedForm? form,
coap.CoapMediaType? format,
coap.CoapMediaType? accept,
coap.BlockSize? block1Size,
Expand All @@ -230,7 +231,7 @@ final class CoapClient implements ProtocolClient {
}

Future<AuthServerRequestCreationHint?> _obtainCreationHintFromResourceServer(
Form form,
AugmentedForm form,
) async {
final requestMethod = (form.method ?? CoapRequestMethod.get).code;

Expand Down Expand Up @@ -259,7 +260,7 @@ final class CoapClient implements ProtocolClient {
///
/// Returns `null` if no `ACESecurityScheme` is defined.
Future<AuthServerRequestCreationHint?> _obtainAceCreationHintFromForm(
Form? form,
AugmentedForm? form,
) async {
if (form == null) {
return null;
Expand Down Expand Up @@ -301,7 +302,7 @@ final class CoapClient implements ProtocolClient {
AuthServerRequestCreationHint? creationHint,
AceSecurityCallback aceCredentialsCallback,
Uri uri,
Form? form, [
AugmentedForm? form, [
AceCredentials? invalidAceCredentials,
]) async {
final aceCredentials = await aceCredentialsCallback(
Expand Down Expand Up @@ -339,7 +340,7 @@ final class CoapClient implements ProtocolClient {
coap.CoapRequest request,
coap.CoapResponse response,
Uri uri,
Form? form,
AugmentedForm? form,
AceSecurityCallback aceCredentialsCallback, {
AceCredentials? invalidAceCredentials,
}) async {
Expand Down Expand Up @@ -372,23 +373,23 @@ final class CoapClient implements ProtocolClient {
}

@override
Future<Content> readResource(Form form) async {
Future<Content> readResource(AugmentedForm form) async {
return _sendRequestFromForm(form, OperationType.readproperty);
}

@override
Future<void> writeResource(Form form, Content content) async {
Future<void> writeResource(AugmentedForm form, Content content) async {
await _sendRequestFromForm(form, OperationType.writeproperty, content);
}

@override
Future<Content> invokeResource(Form form, Content content) async {
Future<Content> invokeResource(AugmentedForm form, Content content) async {
return _sendRequestFromForm(form, OperationType.invokeaction, content);
}

@override
Future<Subscription> subscribeResource(
Form form, {
AugmentedForm form, {
required void Function(Content content) next,
void Function(Exception error)? error,
required void Function() complete,
Expand All @@ -406,7 +407,7 @@ final class CoapClient implements ProtocolClient {
}

Future<CoapSubscription> _startObservation(
Form form,
AugmentedForm form,
OperationType operationType,
void Function(Content content) next,
void Function() complete,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "package:cbor/cbor.dart";
import "package:coap/coap.dart";
import "package:dcaf/dcaf.dart";

import "../core/augmented_form.dart";
import "../core/content.dart";
import "../definitions/expected_response.dart";
import "../definitions/form.dart";
Expand All @@ -26,7 +27,7 @@ extension InternetAddressMethods on Uri {
}

/// CoAP-specific extensions for the [Form] class.
extension CoapFormExtension on Form {
extension CoapFormExtension on AugmentedForm {
T? _obtainVocabularyTerm<T>(String vocabularyTerm) {
final curieString = coapPrefixMapping.expandCurieString(vocabularyTerm);
final formDefinition = additionalFields[curieString];
Expand Down
28 changes: 16 additions & 12 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "dart:io";

import "package:http/http.dart";

import "../core/augmented_form.dart";
import "../core/content.dart";
import "../core/credentials/basic_credentials.dart";
import "../core/credentials/bearer_credentials.dart";
Expand Down Expand Up @@ -60,7 +61,10 @@ final class HttpClient implements ProtocolClient {
final AsyncClientSecurityCallback<BearerCredentials>?
_bearerCredentialsCallback;

Future<void> _applyCredentialsFromForm(Request request, Form form) async {
Future<void> _applyCredentialsFromForm(
Request request,
AugmentedForm form,
) async {
// TODO(JKRhb): Add DigestSecurity back in
if (await _applyBearerCredentialsFromForm(request, form)) {
return;
Expand All @@ -73,7 +77,7 @@ final class HttpClient implements ProtocolClient {

Future<bool> _applyBasicCredentialsFromForm(
Request request,
Form form,
AugmentedForm form,
) async {
final basicSecuritySchemes =
form.securityDefinitions.whereType<BasicSecurityScheme>();
Expand All @@ -96,7 +100,7 @@ final class HttpClient implements ProtocolClient {

Future<bool> _applyBearerCredentialsFromForm(
Request request,
Form form,
AugmentedForm form,
) async {
final bearerSecuritySchemes =
form.securityDefinitions.whereType<BearerSecurityScheme>();
Expand Down Expand Up @@ -141,7 +145,7 @@ final class HttpClient implements ProtocolClient {

Future<StreamedResponse> _createBasicAuthRequest(
Request originalRequest,
Form? form,
AugmentedForm? form,
) async {
final request = _copyRequest(originalRequest);
final basicCredentials = await _getBasicCredentials(request.url, form);
Expand All @@ -157,7 +161,7 @@ final class HttpClient implements ProtocolClient {

Future<StreamedResponse> _createBearerAuthRequest(
Request originalRequest,
Form? form,
AugmentedForm? form,
) async {
final request = _copyRequest(originalRequest);
final bearerCredentials = await _getBearerCredentials(request.url, form);
Expand All @@ -174,7 +178,7 @@ final class HttpClient implements ProtocolClient {
Future<StreamedResponse> _handleResponse(
Request originalRequest,
StreamedResponse response, [
Form? form,
AugmentedForm? form,
]) async {
if (response.statusCode == HttpStatus.unauthorized) {
final authenticate = response.headers["www-authenticate"];
Expand All @@ -194,7 +198,7 @@ final class HttpClient implements ProtocolClient {
}

Future<StreamedResponse> _createRequest(
Form form,
AugmentedForm form,
OperationType operationType,
Content? content,
) async {
Expand All @@ -215,15 +219,15 @@ final class HttpClient implements ProtocolClient {

Future<BasicCredentials?> _getBasicCredentials(
Uri uri,
Form? form, [
AugmentedForm? form, [
BasicCredentials? invalidCredentials,
]) async {
return _basicCredentialsCallback?.call(uri, form, invalidCredentials);
}

Future<BearerCredentials?> _getBearerCredentials(
Uri uri,
Form? form, [
AugmentedForm? form, [
BearerCredentials? invalidCredentials,
]) async {
return _bearerCredentialsCallback?.call(uri, form, invalidCredentials);
Expand Down Expand Up @@ -255,14 +259,14 @@ final class HttpClient implements ProtocolClient {
}

@override
Future<Content> invokeResource(Form form, Content content) async {
Future<Content> invokeResource(AugmentedForm form, Content content) async {
final response =
await _createRequest(form, OperationType.invokeaction, content);
return _contentFromResponse(form, response);
}

@override
Future<Content> readResource(Form form) async {
Future<Content> readResource(AugmentedForm form) async {
final response =
await _createRequest(form, OperationType.readproperty, null);
return _contentFromResponse(form, response);
Expand All @@ -279,7 +283,7 @@ final class HttpClient implements ProtocolClient {
}

@override
Future<void> writeResource(Form form, Content content) async {
Future<void> writeResource(AugmentedForm form, Content content) async {
await _createRequest(form, OperationType.writeproperty, content);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import "package:mqtt_client/mqtt_client.dart";
import "package:mqtt_client/mqtt_server_client.dart";
import "package:typed_data/typed_buffers.dart";

import "../core/augmented_form.dart";
import "../core/content.dart";
import "../core/credentials/basic_credentials.dart";
import "../core/credentials/callbacks.dart";
import "../core/protocol_interfaces/protocol_client.dart";
import "../definitions/form.dart";
import "../scripting_api/subscription.dart" as scripting_api;
import "constants.dart";
import "mqtt_binding_exception.dart";
Expand All @@ -40,7 +40,7 @@ final class MqttClient implements ProtocolClient {

Future<BasicCredentials?> _obtainCredentials(
Uri uri,
Form? form, [
AugmentedForm? form, [
BasicCredentials? invalidCredentials,
bool unauthorized = false,
]) async {
Expand Down Expand Up @@ -71,10 +71,10 @@ final class MqttClient implements ProtocolClient {
);
}

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

Future<MqttServerClient> _connect(Uri brokerUri, Form? form) async {
Future<MqttServerClient> _connect(Uri brokerUri, AugmentedForm? form) async {
final client = brokerUri.createClient(_mqttConfig.keepAlivePeriod);
final credentials = await _obtainCredentials(brokerUri, form);

Expand All @@ -99,7 +99,7 @@ final class MqttClient implements ProtocolClient {
}

@override
Future<Content> invokeResource(Form form, Content content) async {
Future<Content> invokeResource(AugmentedForm form, Content content) async {
final client = await _connectWithForm(form);
final topic = form.topicName;
final qualityOfService =
Expand All @@ -118,7 +118,7 @@ final class MqttClient implements ProtocolClient {
}

@override
Future<Content> readResource(Form form) async {
Future<Content> readResource(AugmentedForm form) async {
final client = await _connectWithForm(form);
final topic = form.topicFilter;
final qualityOfService =
Expand Down Expand Up @@ -154,7 +154,7 @@ final class MqttClient implements ProtocolClient {
}

@override
Future<void> writeResource(Form form, Content content) async {
Future<void> writeResource(AugmentedForm form, Content content) async {
final client = await _connectWithForm(form);
final topic = form.topicName;
final qualityOfService =
Expand Down Expand Up @@ -182,7 +182,7 @@ final class MqttClient implements ProtocolClient {

@override
Future<scripting_api.Subscription> subscribeResource(
Form form, {
AugmentedForm form, {
required void Function(Content content) next,
void Function(Exception error)? error,
required void Function() complete,
Expand Down
Loading

0 comments on commit ec9582f

Please sign in to comment.