diff --git a/.github/workflows/component-ci.yml b/.github/workflows/component-ci.yml index b1d6741..54d9987 100644 --- a/.github/workflows/component-ci.yml +++ b/.github/workflows/component-ci.yml @@ -11,4 +11,5 @@ jobs: secrets: inherit permissions: write-all with: - subfolder: '.' \ No newline at end of file + subfolder: '.' + flutter_version: '3.24.3' \ No newline at end of file diff --git a/lib/src/authentication_service.dart b/lib/src/authentication_service.dart index fbc49e0..ff31c63 100644 --- a/lib/src/authentication_service.dart +++ b/lib/src/authentication_service.dart @@ -1,4 +1,5 @@ import "dart:async"; +import "dart:convert"; /// A service that allows the api service to obtain AuthenticationInformation abstract class AuthenticationService { @@ -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 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 diff --git a/lib/src/converters.dart b/lib/src/converters.dart index 2543725..2662c0e 100644 --- a/lib/src/converters.dart +++ b/lib/src/converters.dart @@ -220,3 +220,71 @@ class ModelListJsonResponseConverter serialize: _serialize, ); } + +/// A converter that only supports serialization, +/// ideal for write only endpoints +class WriteOnlyJsonMapResponseConverter + implements ApiConverter> { + /// Creates a WriteOnlyJsonMapResponseConverter. + const WriteOnlyJsonMapResponseConverter(); + + @override + Object fromRepresentation(Map representation) => + jsonEncode(representation); + + @override + void toRepresentation(_) {} +} + +/// A converter that only supports deserialization, +/// ideal for read only endpoints +class ReadOnlyJsonMapResponseConverter + implements ApiConverter, void> { + /// Creates a ReadOnlyJsonMapResponseConverter. + const ReadOnlyJsonMapResponseConverter(); + + @override + Object fromRepresentation(_) => ""; + + @override + Map toRepresentation(Object object) => + jsonDecode(object as String) as Map; +} + +/// A converter that only supports serialization, +/// ideal for write only endpoints +class WriteOnlyModelJsonResponseConverter + implements ApiConverter { + /// Creates a write only json serializer. + WriteOnlyModelJsonResponseConverter({ + required Map Function(RequestModel) serialize, + }) : _serialize = serialize; + + final Map 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 + implements ApiConverter { + /// Creates a read only json serializer. + ReadOnlyModelJsonResponseConverter({ + required ResponseModel Function(Map) deserialize, + }) : _deserialize = deserialize; + + final ResponseModel Function(Map) _deserialize; + + @override + Object fromRepresentation(_) => ""; + + @override + ResponseModel toRepresentation(Object object) => + _deserialize(jsonDecode(object as String)); +}