-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathoperations_protocol.http.dart
More file actions
126 lines (119 loc) · 3.86 KB
/
operations_protocol.http.dart
File metadata and controls
126 lines (119 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import 'dart:convert';
import 'package:celest_cloud/src/cloud/base/base_protocol.dart';
import 'package:celest_cloud/src/cloud/cloud.dart';
import 'package:celest_cloud/src/cloud/operations/operations_protocol.dart';
import 'package:celest_cloud/src/proto/google/longrunning/operations.pb.dart';
import 'package:celest_core/_internal.dart';
import 'package:http/http.dart' as http;
final class OperationsProtocolHttp
with BaseProtocol
implements OperationsProtocol {
OperationsProtocolHttp({
required Uri uri,
http.Client? httpClient,
}) : _client = httpClient ?? http.Client(),
_baseUri = uri;
final http.Client _client;
final Uri _baseUri;
@override
Future<void> cancel(CancelOperationRequest request) async {
final path = '/v1alpha1/${request.name}:cancel';
final url = _baseUri.replace(path: path);
final req = http.Request('POST', url)
..body = jsonEncode(
request.toProto3Json(
typeRegistry: CelestCloud.typeRegistry,
),
)
..headers['content-type'] = 'application/json'
..headers['accept'] = 'application/json';
final res = await _client.send(req);
if (res.statusCode != 200) {
final body = await res.stream.toBytes();
throwError(
statusCode: res.statusCode,
bodyBytes: body,
);
}
}
@override
Future<Operation> get(GetOperationRequest request) async {
final path = '/v1alpha1/${request.name}';
final url = _baseUri.replace(path: path);
final req = http.Request('GET', url)
..headers['content-type'] = 'application/json'
..headers['accept'] = 'application/json';
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,
);
}
@override
Future<ListOperationsResponse> list(ListOperationsRequest request) async {
if (request.name == '') {
throw ArgumentError.value(request.name, 'name', 'must not be empty');
}
final path = '/v1alpha1/${request.name}/operations';
final url = _baseUri.replace(
path: path,
queryParameters: {
if (request.hasFilter()) 'filter': request.filter,
if (request.hasPageSize()) 'pageSize': request.pageSize.toString(),
if (request.hasPageToken()) 'pageToken': request.pageToken,
},
);
final req = http.Request('GET', url)
..headers['content-type'] = 'application/json'
..headers['accept'] = 'application/json';
final res = await _client.send(req);
final body = await res.stream.toBytes();
if (res.statusCode != 200) {
throwError(
statusCode: res.statusCode,
bodyBytes: body,
);
}
return ListOperationsResponse()
..mergeFromProto3Json(
JsonUtf8.decode(body),
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,
);
}
}