diff --git a/examples/topics.dart b/examples/topics.dart index f590b21..fb0f1bd 100644 --- a/examples/topics.dart +++ b/examples/topics.dart @@ -12,7 +12,7 @@ void main() async { var topicClient = TopicClient( CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"), - Mobile.latest()); + MobileTopicConfiguration.latest()); // start publishing messages in 2 seconds Timer(const Duration(seconds: 2), () async { diff --git a/lib/client_sdk_dart.dart b/lib/client_sdk_dart.dart index 7b1241f..d018af6 100644 --- a/lib/client_sdk_dart.dart +++ b/lib/client_sdk_dart.dart @@ -7,10 +7,14 @@ export 'src/topic_client.dart'; export 'src/cache_client.dart'; export 'src/auth/credential_provider.dart'; export 'src/config/topic_configurations.dart'; +export 'src/config/cache_configurations.dart'; export 'src/messages/values.dart'; export 'src/messages/responses/topics/topic_publish.dart'; export 'src/messages/responses/topics/topic_subscribe.dart'; export 'src/messages/responses/topics/topic_subscription_item.dart'; export 'src/config/logger.dart' show LogLevel; +export 'src/messages/responses/cache/data/scalar/delete_response.dart'; +export 'src/messages/responses/cache/data/scalar/get_response.dart'; +export 'src/messages/responses/cache/data/scalar/set_response.dart'; // TODO: Export any libraries intended for clients of this package. diff --git a/lib/src/cache_client.dart b/lib/src/cache_client.dart index f083269..612f5d0 100644 --- a/lib/src/cache_client.dart +++ b/lib/src/cache_client.dart @@ -1,12 +1,14 @@ import 'package:client_sdk_dart/src/config/cache_configuration.dart'; +import 'package:client_sdk_dart/src/errors/errors.dart'; import 'package:client_sdk_dart/src/internal/data_client.dart'; +import 'package:client_sdk_dart/src/internal/utils/validators.dart'; import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/delete_response.dart'; import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/get_response.dart'; import 'package:client_sdk_dart/src/messages/responses/cache/data/scalar/set_response.dart'; import 'package:logging/logging.dart'; import 'auth/credential_provider.dart'; -import 'config/logger.dart'; +// import 'config/logger.dart'; import 'messages/values.dart'; abstract class ICacheClient { @@ -26,26 +28,45 @@ class CacheClient implements ICacheClient { CacheConfiguration configuration, Duration defaultTtl) : _dataClient = DataClient(credentialProvider, configuration, defaultTtl) { - _logger.level = determineLoggerLevel(configuration.logLevel); + // TODO: fix logging level issue + // _logger.level = determineLoggerLevel(configuration.logLevel); _logger.finest("initializing cache client"); } @override Future get(String cacheName, Value key) { - // TODO: add validators + try { + validateCacheName(cacheName); + } catch (e) { + if (e is SdkException) { + return Future.error(GetError(e)); + } + } return _dataClient.get(cacheName, key); } @override Future set(String cacheName, Value key, Value value, {Duration? ttl}) { - // TODO: add validators + try { + validateCacheName(cacheName); + } catch (e) { + if (e is SdkException) { + return Future.error(GetError(e)); + } + } return _dataClient.set(cacheName, key, value, ttl: ttl); } @override Future delete(String cacheName, Value key) { - // TODO: add validators + try { + validateCacheName(cacheName); + } catch (e) { + if (e is SdkException) { + return Future.error(GetError(e)); + } + } return _dataClient.delete(cacheName, key); } } diff --git a/lib/src/config/cache_configurations.dart b/lib/src/config/cache_configurations.dart index af2e6a6..e8e173a 100644 --- a/lib/src/config/cache_configurations.dart +++ b/lib/src/config/cache_configurations.dart @@ -8,7 +8,7 @@ import 'cache_configuration.dart'; abstract interface class CacheClientConfigurations {} /// Provides prebuilt configurations for the `CacheClient` on mobile platforms -class Mobile extends CacheClientConfigurations { +class MobileCacheConfiguration extends CacheClientConfigurations { static CacheClientConfiguration defaultConfig() { return latest(); } diff --git a/lib/src/config/topic_configurations.dart b/lib/src/config/topic_configurations.dart index 39566a7..89ae6ef 100644 --- a/lib/src/config/topic_configurations.dart +++ b/lib/src/config/topic_configurations.dart @@ -7,7 +7,7 @@ import 'transport/transport_strategy.dart'; abstract interface class TopicClientConfigurations {} /// Provides prebuilt configurations for the `TopicClient` on mobile platforms -class Mobile extends TopicClientConfigurations { +class MobileTopicConfiguration extends TopicClientConfigurations { static TopicClientConfiguration defaultConfig() { return latest(); } diff --git a/lib/src/internal/data_client.dart b/lib/src/internal/data_client.dart index fd5f1e5..17caeb6 100644 --- a/lib/src/internal/data_client.dart +++ b/lib/src/internal/data_client.dart @@ -22,19 +22,17 @@ abstract class AbstractDataClient { class DataClient implements AbstractDataClient { late ClientChannel _channel; late ScsClient _client; - late CacheConfiguration _configuration; - late Duration _defaultTtl; + final CacheConfiguration _configuration; + final Duration _defaultTtl; - DataClient(CredentialProvider credentialProvider, - CacheConfiguration configuration, Duration defaultTtl) { + DataClient(CredentialProvider credentialProvider, this._configuration, + this._defaultTtl) { _channel = ClientChannel(credentialProvider.cacheEndpoint); _client = ScsClient(_channel, options: CallOptions(metadata: { 'authorization': credentialProvider.apiKey, 'agent': 'dart:0.1.0', }, timeout: _configuration.transportStrategy.grpcConfig.deadline)); - _configuration = configuration; - _defaultTtl = defaultTtl; } @override @@ -44,7 +42,7 @@ class DataClient implements AbstractDataClient { try { var resp = await _client.get(request, options: CallOptions(metadata: { - 'cacheName': cacheName, + 'cache': cacheName, })); switch (resp.result) { @@ -77,7 +75,7 @@ class DataClient implements AbstractDataClient { try { await _client.set(request, options: CallOptions(metadata: { - 'cacheName': cacheName, + 'cache': cacheName, })); return SetSuccess(); } catch (e) { @@ -96,7 +94,7 @@ class DataClient implements AbstractDataClient { try { await _client.delete(request, options: CallOptions(metadata: { - 'cacheName': cacheName, + 'cache': cacheName, })); return DeleteSuccess(); } catch (e) { diff --git a/lib/src/internal/utils/validators.dart b/lib/src/internal/utils/validators.dart new file mode 100644 index 0000000..38387b9 --- /dev/null +++ b/lib/src/internal/utils/validators.dart @@ -0,0 +1,13 @@ +import 'package:client_sdk_dart/src/errors/errors.dart'; + +void _validateString(String str, String errorMessage) { + if (str.isEmpty) { + throw InvalidArgumentException(errorMessage, null, null); + } +} + +void validateCacheName(String cacheName) => + _validateString(cacheName, "Invalid cache name"); + +void validateTopicName(String topicName) => + _validateString(topicName, "Invalid topic name"); diff --git a/lib/src/messages/responses/cache/data/scalar/delete_response.dart b/lib/src/messages/responses/cache/data/scalar/delete_response.dart index c8b1f2b..bbcc948 100644 --- a/lib/src/messages/responses/cache/data/scalar/delete_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/delete_response.dart @@ -1,7 +1,7 @@ import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; /// Sealed class for a delete cache item request. -/// +/// /// Pattern matching can be used to operate on the appropriate subtype. /// ```dart /// switch (response) { @@ -17,7 +17,7 @@ sealed class DeleteResponse {} class DeleteSuccess implements DeleteResponse {} /// Indicates that an error occurred during the delete cache item request. -/// +/// /// The response object includes the following fields you can use to determine how you want to handle the error: /// - `errorCode`: a unique Momento error code indicating the type of error that occurred /// - `message`: a human-readable description of the error diff --git a/lib/src/messages/responses/cache/data/scalar/get_response.dart b/lib/src/messages/responses/cache/data/scalar/get_response.dart index 6c562fc..20cf95e 100644 --- a/lib/src/messages/responses/cache/data/scalar/get_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/get_response.dart @@ -3,7 +3,7 @@ import 'dart:convert'; import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; /// Sealed class for a get cache item request. -/// +/// /// Pattern matching can be used to operate on the appropriate subtype. /// ```dart /// switch (response) { @@ -21,7 +21,7 @@ sealed class GetResponse {} class GetMiss implements GetResponse {} /// Indicates that an error occurred during the get cache item request. -/// +/// /// The response object includes the following fields you can use to determine how you want to handle the error: /// - `errorCode`: a unique Momento error code indicating the type of error that occurred /// - `message`: a human-readable description of the error diff --git a/lib/src/messages/responses/cache/data/scalar/set_response.dart b/lib/src/messages/responses/cache/data/scalar/set_response.dart index 9f81926..4028144 100644 --- a/lib/src/messages/responses/cache/data/scalar/set_response.dart +++ b/lib/src/messages/responses/cache/data/scalar/set_response.dart @@ -1,7 +1,7 @@ import 'package:client_sdk_dart/src/messages/responses/responses_base.dart'; /// Sealed class for a set cache item request. -/// +/// /// Pattern matching can be used to operate on the appropriate subtype. /// ```dart /// switch (response) { @@ -17,7 +17,7 @@ sealed class SetResponse {} class SetSuccess implements SetResponse {} /// Indicates that an error occurred during the set cache item request. -/// +/// /// The response object includes the following fields you can use to determine how you want to handle the error: /// - `errorCode`: a unique Momento error code indicating the type of error that occurred /// - `message`: a human-readable description of the error diff --git a/lib/src/topic_client.dart b/lib/src/topic_client.dart index dfd8cde..623aeb8 100644 --- a/lib/src/topic_client.dart +++ b/lib/src/topic_client.dart @@ -23,6 +23,7 @@ class TopicClient implements ITopicClient { TopicClient( CredentialProvider credentialProvider, TopicConfiguration configuration) : _pubsubClient = ClientPubsub(credentialProvider, configuration) { + // TODO: fix logging level issue // _logger.level = determineLoggerLevel(configuration.logLevel); _logger.finest("initializing topic client"); }