Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/celest_cloud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 0.1.5-wip

- feat: Add `Operations.waitOperation`
- chore: Update license

# 0.1.4
Expand Down
12 changes: 12 additions & 0 deletions packages/celest_cloud/lib/src/cloud/operations/operations.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:celest_cloud/src/cloud/base/base_service.dart';
import 'package:celest_cloud/src/cloud/operations/operations_protocol.dart';
import 'package:celest_cloud/src/grpc.dart';
import 'package:celest_cloud/src/util/well_known.dart';
import 'package:logging/logging.dart';

final class Operations with BaseService {
Expand Down Expand Up @@ -37,4 +38,15 @@ final class Operations with BaseService {
final request = CancelOperationRequest(name: name);
return run('Operations.Cancel', request: request, action: _protocol.cancel);
}

Future<Operation> wait(
String name, {
Duration? timeout,
}) async {
final request = WaitOperationRequest(
name: name,
timeout: timeout?.toProto(),
);
return run('Operations.Wait', request: request, action: _protocol.wait);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ abstract interface class OperationsProtocol {
Future<Operation> get(GetOperationRequest request);
Future<ListOperationsResponse> list(ListOperationsRequest request);
Future<void> cancel(CancelOperationRequest request);
Future<Operation> wait(WaitOperationRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ final class OperationsProtocolGrpc implements OperationsProtocol {
Future<ListOperationsResponse> list(ListOperationsRequest request) {
return _client.listOperations(request);
}

@override
Future<Operation> wait(WaitOperationRequest request) {
return _client.waitOperation(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,33 @@ final class OperationsProtocolHttp
typeRegistry: CelestCloud.typeRegistry,
);
}

@override
Future<Operation> wait(WaitOperationRequest request) async {
if (request.name == '') {
throw ArgumentError.value(request.name, 'name', 'must not be empty');
}
final url = Uri.parse('/v1alpha1/${request.name}:wait');
final req = http.Request('POST', url)
..headers['content-type'] = 'application/json'
..headers['accept'] = 'application/json'
..body = jsonEncode(
request.toProto3Json(
typeRegistry: CelestCloud.typeRegistry,
),
);
final res = await _client.send(req);
final body = await res.stream.toBytes();
if (res.statusCode != 200) {
throwError(
statusCode: res.statusCode,
bodyBytes: body,
);
}
return Operation()
..mergeFromProto3Json(
JsonUtf8.decode(body),
typeRegistry: CelestCloud.typeRegistry,
);
}
}
10 changes: 10 additions & 0 deletions packages/celest_cloud/lib/src/util/well_known.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:celest_cloud/src/proto.dart';
import 'package:celest_cloud/src/proto/google/protobuf/duration.pb.dart' as pb;
import 'package:celest_cloud/src/proto/google/protobuf/timestamp.pb.dart';
import 'package:fixnum/fixnum.dart';

extension DateTimeProto on DateTime {
Timestamp toProto() {
Expand All @@ -8,3 +10,11 @@ extension DateTimeProto on DateTime {
return ts;
}
}

extension DurationProto on Duration {
pb.Duration toProto() {
return pb.Duration()
..seconds = Int64(inSeconds)
..nanos = inMicroseconds.remainder(Duration.microsecondsPerSecond) * 1000;
}
}
Loading