Skip to content

Commit

Permalink
Merge branch 'main' into bruuuuuuuce-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce committed Dec 15, 2023
2 parents 45116ff + 00b44ee commit 729cdd5
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 139 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ and the Flutter guide for
4. `brew info dart` to determine where the dart sdk is installed
5. followed [these](https://fluttermaster.com/config-dart-sdk-inside-intellij-idea-on-macos/) instructions on how to setup sdk inside of intellij

In VSCode, there's an extension for Dart and Flutter files.
To install dependencies using CLI: `dart pub get`
Running tests: `dart test`
Linting and formatting: `dart format` and `dart fix`

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

Expand All @@ -40,6 +45,17 @@ to `/example` folder.
const like = 'sample';
```

## Logging

We use the [Dart logging package](https://github.com/dart-lang/logging) to create internal Loggers for producing Momento-related logs. The default logger does not do anything with the logs, so you must configure the logging level and handler at the beginning of your program:

```
Logger.root.level = Level.ALL; // defaults to Level.INFO
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
Expand Down
2 changes: 1 addition & 1 deletion lib/client_sdk_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
/// More dartdocs go here.
library;

export 'src/client_sdk_dart_base.dart';
export 'src/topic_client.dart';

// TODO: Export any libraries intended for clients of this package.
31 changes: 0 additions & 31 deletions lib/src/client_sdk_dart_base.dart

This file was deleted.

37 changes: 37 additions & 0 deletions lib/src/config/topic_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';

abstract interface class TopicConfiguration {
/// Configures low-level options for network interactions with the Momento service
late TransportStrategy transportStrategy;

/// Constructor for a TopicConfiguration
TopicConfiguration(this.transportStrategy);

/// Copy constructor for overriding TransportStrategy
TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy);

/// Convenience copy constructor that updates the client-side
/// timeout setting in the transport strategy
TopicConfiguration withDeadline(Duration deadline);
}

/// Configuration options for Momento TopicClient.
/// The easiest way to get a `TopicClientConfiguration` object is
/// to use one of the prebuilt TopicClientConfigurations classes.
class TopicClientConfiguration implements TopicConfiguration {
@override
late TransportStrategy transportStrategy;

TopicClientConfiguration(this.transportStrategy);

@override
TopicConfiguration withTransportStrategy(TransportStrategy transportStrategy) {
return TopicClientConfiguration(transportStrategy);
}

@override
TopicConfiguration withDeadline(Duration deadline) {
return TopicClientConfiguration(StaticTransportStrategy(StaticGrpcConfiguration(deadline)));
}
}
17 changes: 17 additions & 0 deletions lib/src/config/topic_configurations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'topic_configuration.dart';
import 'transport/grpc_configuration.dart';
import 'transport/transport_strategy.dart';

/// Prebuilt configurations for Momento Topics clients
abstract interface class TopicClientConfigurations {}

/// Provides prebuilt configurations for the `TopicClient` on mobile platforms
class Mobile extends TopicClientConfigurations {
static TopicClientConfiguration defaultConfig() {
return latest();
}

static TopicClientConfiguration latest() {
return TopicClientConfiguration(StaticTransportStrategy(StaticGrpcConfiguration(Duration(seconds: 15))));
}
}
21 changes: 21 additions & 0 deletions lib/src/config/transport/grpc_configuration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
abstract interface class GrpcConfiguration {
/// Duration of time the client is willing to wait for an RPC to
/// complete before it is terminated with a DeadlineExceeded error
late Duration deadline;

/// Copy constructor for overriding the client-side deadline
GrpcConfiguration withDeadline(Duration deadline);
}

/// Encapsulates gRPC configuration tunables
class StaticGrpcConfiguration implements GrpcConfiguration {
@override
late Duration deadline;

StaticGrpcConfiguration(this.deadline);

@override
GrpcConfiguration withDeadline(Duration deadline) {
return StaticGrpcConfiguration(deadline);
}
}
30 changes: 30 additions & 0 deletions lib/src/config/transport/transport_strategy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'grpc_configuration.dart';

abstract interface class TransportStrategy {
/// Low-level gRPC settings for communication with the Momento server
late GrpcConfiguration grpcConfig;

/// Copy constructor to update the client-side timeout
TransportStrategy withGrpcConfig(GrpcConfiguration grpcConfig);

/// Copy constructor to update the client-side timeout
TransportStrategy withClientTimeout(Duration timeout);
}

class StaticTransportStrategy implements TransportStrategy {
@override
late GrpcConfiguration grpcConfig;

StaticTransportStrategy(this.grpcConfig);

@override
TransportStrategy withClientTimeout(Duration timeout) {
return StaticTransportStrategy(StaticGrpcConfiguration(timeout));
}

@override
TransportStrategy withGrpcConfig(GrpcConfiguration grpcConfig) {
return StaticTransportStrategy(grpcConfig);
}

}
45 changes: 20 additions & 25 deletions lib/src/errors/errors.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
abstract class AbstractErrorResponseBase {
String _message;
Error? _innerException;
AbstractErrorResponseBase(this._message, this._innerException);

String get message => _message;
Error? get innerException => _innerException;
enum MomentoErrorCode {
INVALID_ARGUMENT_ERROR,
UNKNOWN_SERVICE_ERROR,
ALREADY_EXISTS_ERROR,
NOT_FOUND_ERROR,
}

class SdkError implements Error, AbstractErrorResponseBase {
@override
// TODO: implement stackTrace
StackTrace? get stackTrace => _innerException?.stackTrace;

@override
Error? _innerException;
abstract class AbstractExceptionResponseBase {
final String _message;
final Exception? _innerException;
AbstractExceptionResponseBase(this._message, this._innerException);

@override
String _message;

SdkError(this._message, this._innerException);
String get message => _message;
Exception? get innerException => _innerException;
}

@override
// TODO: implement innerException
Error? get innerException => _innerException;
class SdkException extends AbstractExceptionResponseBase implements Exception {
String _messageWrapper;
SdkException(String message, Exception? innerException, String messageWrapper) : _messageWrapper = messageWrapper, super(message, innerException);

@override
// TODO: implement message
String get message => _message;
String get messageWrapper => _messageWrapper;
}

}
class UnknownException extends SdkException {
UnknownException(String message, Exception? innerException) : super(message, innerException, "Unknown error has occurred");
}
28 changes: 11 additions & 17 deletions lib/src/internal/pubsub_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@ import 'dart:typed_data';
import 'package:client_sdk_dart/generated/cachepubsub.pb.dart';
import 'package:client_sdk_dart/generated/cachepubsub.pbgrpc.dart';
import 'package:client_sdk_dart/src/auth/credential_provider.dart';
import 'package:client_sdk_dart/src/errors/errors.dart';
import 'package:grpc/grpc.dart';
import 'package:protobuf/protobuf.dart';

import '../messages/Values.dart';
import '../messages/responses/topics/topic_publish.dart';

sealed class Value {}
class StringValue implements Value {
String _value;
StringValue(String v) : _value = v;
String get value => _value;
}

class BinaryValue implements Value {
List<int> _value;
BinaryValue(List<int> v) : _value = v;
List<int> get value => _value;
}

abstract class AbstractPubsubClient {
Future<TopicPublishResponse> publish(String cacheName, String topicName, Value value);
}
Expand All @@ -40,9 +28,15 @@ class ClientPubsub implements AbstractPubsubClient {
request.cacheName = cacheName;
request.topic = topicName;
request.value = _valueToTopicValue(value);
await _client.publish(request);
// TODO: implement publish
throw UnimplementedError();
try {
await _client.publish(request);
return TopicPublishSuccess();
} catch (e) {
if (e is SdkException) {
return TopicPublishError(e);
}
return TopicPublishError(UnknownException("Unexpected error: $e", null));
}
}

TopicValue_ _valueToTopicValue(Value v) {
Expand Down
12 changes: 12 additions & 0 deletions lib/src/messages/Values.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sealed class Value {}
class StringValue implements Value {
String _value;
StringValue(String v) : _value = v;
String get value => _value;
}

class BinaryValue implements Value {
List<int> _value;
BinaryValue(List<int> v) : _value = v;
List<int> get value => _value;
}
13 changes: 2 additions & 11 deletions lib/src/messages/responses/responses_base.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import '../../errors/errors.dart';

class ErrorResponseBase implements AbstractErrorResponseBase {
String _message;
Error? _innerException;

@override
Error? get innerException => _innerException;

@override
String get message => _message;

ErrorResponseBase(SdkError error) : _message = error.message, _innerException = error.innerException;
class ErrorResponseBase extends AbstractExceptionResponseBase {
ErrorResponseBase(SdkException exception) : super(exception.message, exception.innerException);
}
2 changes: 1 addition & 1 deletion lib/src/messages/responses/topics/topic_publish.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ sealed class TopicPublishResponse {}
class TopicPublishSuccess implements TopicPublishResponse { }

class TopicPublishError extends ErrorResponseBase implements TopicPublishResponse {
TopicPublishError(super.error);
TopicPublishError(super.exception);
}
50 changes: 13 additions & 37 deletions lib/src/topic_client.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
// TODO: Put public facing types in this file.

import 'dart:typed_data';

import '../generated/cachepubsub.pb.dart';
import 'package:grpc/grpc.dart';

sealed class Value {}
class StringValue implements Value {
String _value;
StringValue(String v) : _value = v;
String get value => _value;
}

class BinaryValue implements Value {
Uint8List _value;
BinaryValue(Uint8List v) : _value = v;
Uint8List get value => _value;
}
import 'package:client_sdk_dart/src/auth/credential_provider.dart';
import 'package:logging/logging.dart';
import 'internal/pubsub_client.dart';
import 'messages/Values.dart';
import 'messages/responses/topics/topic_publish.dart';

abstract class ITopicClient {
void publish(String cacheName, String topicName, Value value);
Future<TopicPublishResponse> publish(String cacheName, String topicName, Value value);
}

class TopicClient implements ITopicClient {
ClientChannel _channel;

TopicClient() {
_channel = ClientChannel(host)
}
@override
void publish() {
// TODO: implement publish
}
ClientPubsub _pubsubClient;
CredentialProvider _credentialProvider;
final Logger _logger = Logger('MomentoTopicClient');

void close() {
TopicClient(this._credentialProvider) : _pubsubClient = ClientPubsub(_credentialProvider);

@override
Future<TopicPublishResponse> publish(String cacheName, String topicName, Value value) {
return this._pubsubClient.publish(cacheName, topicName, value);
}
}

/// Checks if you are awesome. Spoiler: you are.
class Awesome {
bool get isAwesome => true;
}

PubsubApi api = PubsubApi();
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
grpc: ^3.2.4
jwt_decoder: ^2.0.1
logging: ^1.2.0
protobuf: ^3.1.0
string_validator: ^1.0.2
# path: ^1.8.0
Expand Down
Loading

0 comments on commit 729cdd5

Please sign in to comment.