Skip to content

Commit

Permalink
add validators and export more things and dart format
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Dec 27, 2023
1 parent e8243a9 commit c34f2e9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/topics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions lib/client_sdk_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
31 changes: 26 additions & 5 deletions lib/src/cache_client.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<GetResponse> 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<SetResponse> 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<DeleteResponse> 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);
}
}
2 changes: 1 addition & 1 deletion lib/src/config/cache_configurations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/config/topic_configurations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
16 changes: 7 additions & 9 deletions lib/src/internal/data_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -77,7 +75,7 @@ class DataClient implements AbstractDataClient {
try {
await _client.set(request,
options: CallOptions(metadata: {
'cacheName': cacheName,
'cache': cacheName,
}));
return SetSuccess();
} catch (e) {
Expand All @@ -96,7 +94,7 @@ class DataClient implements AbstractDataClient {
try {
await _client.delete(request,
options: CallOptions(metadata: {
'cacheName': cacheName,
'cache': cacheName,
}));
return DeleteSuccess();
} catch (e) {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/internal/utils/validators.dart
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/src/topic_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit c34f2e9

Please sign in to comment.