Skip to content

Commit

Permalink
[gui] refactor grpc calls using a common function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-toterman committed Dec 16, 2024
1 parent 0e909a0 commit 82e8fb4
Showing 1 changed file with 82 additions and 123 deletions.
205 changes: 82 additions & 123 deletions src/client/gui/lib/grpc_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,189 +91,148 @@ class GrpcClient {
}
}

Future<Rep?> doRequest<Req extends RpcMessage, Rep extends RpcMessage>(
ResponseStream<Rep> Function(Stream<Req> request) action,
Req request, {
bool checkUpdates = false,
bool logReplies = true,
}) {
logger.i('Sent ${request.repr}');
Stream<Rep> replyStream = action(Stream.value(request));
if (checkUpdates) replyStream = replyStream.doOnData(checkForUpdate);
if (logReplies) replyStream = replyStream.doOnEach(logGrpc(request));
return replyStream.lastOrNull;
}

Future<StartReply?> start(Iterable<String> names) {
final request = StartRequest(
instanceNames: InstanceNames(instanceName: names),
return doRequest(
_client.start,
StartRequest(instanceNames: InstanceNames(instanceName: names)),
checkUpdates: true,
);
logger.i('Sent ${request.repr}');
return _client
.start(Stream.value(request))
.doOnData(checkForUpdate)
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<StopReply?> stop(Iterable<String> names) {
final request = StopRequest(
instanceNames: InstanceNames(instanceName: names),
return doRequest(
_client.stop,
StopRequest(instanceNames: InstanceNames(instanceName: names)),
);
logger.i('Sent ${request.repr}');
return _client
.stop(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<SuspendReply?> suspend(Iterable<String> names) {
final request = SuspendRequest(
instanceNames: InstanceNames(instanceName: names),
return doRequest(
_client.suspend,
SuspendRequest(instanceNames: InstanceNames(instanceName: names)),
);
logger.i('Sent ${request.repr}');
return _client
.suspend(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<RestartReply?> restart(Iterable<String> names) {
final request = RestartRequest(
instanceNames: InstanceNames(instanceName: names),
return doRequest(
_client.restart,
RestartRequest(instanceNames: InstanceNames(instanceName: names)),
checkUpdates: true,
);
logger.i('Sent ${request.repr}');
return _client
.restart(Stream.value(request))
.doOnData(checkForUpdate)
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<DeleteReply?> delete(Iterable<String> names) {
final request = DeleteRequest(
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
return doRequest(
_client.delet,
DeleteRequest(
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
),
),
);
logger.i('Sent ${request.repr}');
return _client
.delet(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<RecoverReply?> recover(Iterable<String> names) {
final request = RecoverRequest(
instanceNames: InstanceNames(instanceName: names),
return doRequest(
_client.recover,
RecoverRequest(instanceNames: InstanceNames(instanceName: names)),
);
logger.i('Sent ${request.repr}');
return _client
.recover(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<DeleteReply?> purge(Iterable<String> names) {
final request = DeleteRequest(
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
return doRequest(
_client.delet,
DeleteRequest(
purge: true,
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
),
),
purge: true,
);
logger.i('Sent ${request.repr}');
return _client
.delet(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<List<VmInfo>> info([Iterable<String> names = const []]) {
final request = InfoRequest(
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
return doRequest(
_client.info,
checkUpdates: true,
logReplies: false,
InfoRequest(
instanceSnapshotPairs: names.map(
(name) => InstanceSnapshotPair(instanceName: name),
),
),
);
return _client
.info(Stream.value(request))
.doOnData(checkForUpdate)
.last
.then((r) => r.details.toList());
).then((r) => r!.details.toList());
}

Future<MountReply?> mount(MountRequest request) {
logger.i('Sent ${request.repr}');
return _client
.mount(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
return doRequest(_client.mount, request);
}

Future<void> umount(String name, [String? path]) {
final request = UmountRequest(
targetPaths: [TargetPathInfo(instanceName: name, targetPath: path)],
return doRequest(
_client.umount,
UmountRequest(
targetPaths: [TargetPathInfo(instanceName: name, targetPath: path)],
),
);
logger.i('Sent ${request.repr}');
return _client
.umount(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
}

Future<FindReply> find({bool images = true, bool blueprints = true}) {
final request = FindRequest(
showImages: images,
showBlueprints: blueprints,
);
logger.i('Sent ${request.repr}');
return _client.find(Stream.value(request)).doOnEach(logGrpc(request)).last;
return doRequest(
_client.find,
FindRequest(
showImages: images,
showBlueprints: blueprints,
),
).then((r) => r!);
}

Future<List<NetInterface>> networks() {
final request = NetworksRequest();
logger.i('Sent ${request.repr}');
return _client
.networks(Stream.value(request))
.doOnData(checkForUpdate)
.doOnEach(logGrpc(request))
.last
.then((r) => r.interfaces);
return doRequest(
_client.networks,
NetworksRequest(),
checkUpdates: true,
).then((r) => r!.interfaces);
}

Future<String> version() {
final request = VersionRequest();
logger.i('Sent ${request.repr}');
return _client
.version(Stream.value(request))
.doOnData(checkForUpdate)
.doOnEach(logGrpc(request))
.last
.then((reply) => reply.version);
return doRequest(
_client.version,
VersionRequest(),
checkUpdates: true,
).then((r) => r!.version);
}

Future<String> get(String key) {
final request = GetRequest(key: key);
logger.i('Sent ${request.repr}');
return _client
.get(Stream.value(request))
.doOnEach(logGrpc(request))
.last
.then((reply) => reply.value);
return doRequest(_client.get, GetRequest(key: key)).then((r) => r!.value);
}

Future<void> set(String key, String value) {
final request = SetRequest(key: key, val: value);
logger.i('Sent ${request.repr}');
return _client
.set(Stream.value(request))
.doOnEach(logGrpc(request))
.lastOrNull;
return doRequest(_client.set, SetRequest(key: key, val: value));
}

Future<SSHInfo?> sshInfo(String name) {
final request = SSHInfoRequest(instanceName: [name]);
logger.i('Sent ${request.repr}');
return _client
.ssh_info(Stream.value(request))
.doOnEach(logGrpc(request))
.last
.then((reply) => reply.sshInfo[name]);
return doRequest(
_client.ssh_info,
SSHInfoRequest(instanceName: [name]),
).then((r) => r!.sshInfo[name]);
}

Future<DaemonInfoReply> daemonInfo() {
final request = DaemonInfoRequest();
logger.i('Sent ${request.repr}');
return _client
.daemon_info(Stream.value(request))
.doOnEach(logGrpc(request))
.last;
return doRequest(_client.daemon_info, DaemonInfoRequest()).then((r) => r!);
}
}

Expand Down

0 comments on commit 82e8fb4

Please sign in to comment.