Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/component-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ jobs:
secrets: inherit
permissions: write-all
with:
subfolder: '.'
subfolder: '.'
flutter_version: '3.24.3'
21 changes: 21 additions & 0 deletions lib/src/authentication_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "dart:async";
import "dart:convert";

/// A service that allows the api service to obtain AuthenticationInformation
abstract class AuthenticationService<T extends AuthCredentials> {
Expand Down Expand Up @@ -66,6 +67,26 @@ class JWTAuthCredentials implements AuthCredentials {
};
}

/// A representation of a basic authentication
class BasicAuthCredentials implements AuthCredentials {
/// Creates a basic auth credential.
BasicAuthCredentials({required this.username, required this.password});

/// The username used for basic authentication
final String username;

/// The password used for basic authentication
final String password;

@override
Map<String, String> get headers {
var accessToken = base64Encode(utf8.encode("$username:$password"));
return {
"Authorization": "Basic $accessToken",
};
}
}

/// A representation of a token authentication
class TokenAuthCredentials implements AuthCredentials {
/// Creates a tokenAuth object
Expand Down
68 changes: 68 additions & 0 deletions lib/src/converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,71 @@ class ModelListJsonResponseConverter<ResponseModel, RequestModel>
serialize: _serialize,
);
}

/// A converter that only supports serialization,
/// ideal for write only endpoints
class WriteOnlyJsonMapResponseConverter
implements ApiConverter<void, Map<String, dynamic>> {
/// Creates a WriteOnlyJsonMapResponseConverter.
const WriteOnlyJsonMapResponseConverter();

@override
Object fromRepresentation(Map<String, dynamic> representation) =>
jsonEncode(representation);

@override
void toRepresentation(_) {}
}

/// A converter that only supports deserialization,
/// ideal for read only endpoints
class ReadOnlyJsonMapResponseConverter
implements ApiConverter<Map<String, dynamic>, void> {
/// Creates a ReadOnlyJsonMapResponseConverter.
const ReadOnlyJsonMapResponseConverter();

@override
Object fromRepresentation(_) => "";

@override
Map<String, dynamic> toRepresentation(Object object) =>
jsonDecode(object as String) as Map<String, dynamic>;
}

/// A converter that only supports serialization,
/// ideal for write only endpoints
class WriteOnlyModelJsonResponseConverter<RequestModel>
implements ApiConverter<void, RequestModel> {
/// Creates a write only json serializer.
WriteOnlyModelJsonResponseConverter({
required Map<String, dynamic> Function(RequestModel) serialize,
}) : _serialize = serialize;

final Map<String, dynamic> Function(RequestModel) _serialize;

@override
Object fromRepresentation(RequestModel representation) =>
jsonEncode(_serialize(representation));

@override
void toRepresentation(_) {}
}

/// A converter that only supports deserialization,
/// ideal for read only endpoints
class ReadOnlyModelJsonResponseConverter<ResponseModel>
implements ApiConverter<ResponseModel, void> {
/// Creates a read only json serializer.
ReadOnlyModelJsonResponseConverter({
required ResponseModel Function(Map<String, dynamic>) deserialize,
}) : _deserialize = deserialize;

final ResponseModel Function(Map<String, dynamic>) _deserialize;

@override
Object fromRepresentation(_) => "";

@override
ResponseModel toRepresentation(Object object) =>
_deserialize(jsonDecode(object as String));
}