Skip to content

Commit bc3f025

Browse files
committed
chore(cloud): Improve gRPC interface
- Add separate `.http` and `.grpc` dedicated constructors - Allow specifying an `InternetAddress` for the gRPC host, e.g. a UNIX socket
1 parent aa609bd commit bc3f025

File tree

3 files changed

+97
-28
lines changed

3 files changed

+97
-28
lines changed

packages/celest_cloud/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- feat: Add `Operations.waitOperation`
44
- chore: Update license
5+
- chore: Improve gRPC interface
56

67
# 0.1.4
78

packages/celest_cloud/lib/src/cloud/cloud.dart

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// @docImport 'dart:io';
2+
library;
3+
14
import 'package:celest_cloud/src/cloud/authentication/authentication.dart';
25
import 'package:celest_cloud/src/cloud/cloud_protocol.dart';
36
import 'package:celest_cloud/src/cloud/cloud_protocol.grpc.dart';
@@ -15,32 +18,90 @@ import 'package:os_detect/os_detect.dart' as os;
1518
import 'package:protobuf/protobuf.dart';
1619

1720
class CelestCloud {
18-
CelestCloud({
21+
factory CelestCloud({
1922
required Uri uri,
2023
Authenticator authenticator = const Authenticator.static(),
2124
ClientType? clientType,
2225
Logger? logger,
2326
String? userAgent,
2427
http.Client? httpClient,
25-
}) : _protocol = kIsWeb
26-
? CloudProtocolHttp(
27-
uri: uri,
28-
httpClient: httpClient,
29-
authenticator: authenticator,
30-
logger: logger,
31-
)
32-
: CloudProtocolGrpc(
33-
uri: uri,
34-
userAgent: userAgent,
35-
authenticator: authenticator,
36-
logger: logger,
37-
),
28+
}) {
29+
return kIsWeb
30+
? CelestCloud.http(
31+
uri,
32+
authenticator: authenticator,
33+
clientType: clientType,
34+
logger: logger,
35+
httpClient: httpClient,
36+
)
37+
: CelestCloud.grpc(
38+
uri,
39+
authenticator: authenticator,
40+
clientType: clientType,
41+
logger: logger,
42+
userAgent: userAgent,
43+
httpClient: httpClient,
44+
);
45+
}
46+
47+
CelestCloud._({
48+
required CloudProtocol protocol,
49+
Uri? uri,
50+
Authenticator authenticator = const Authenticator.static(),
51+
ClientType? clientType,
52+
Logger? logger,
53+
http.Client? httpClient,
54+
}) : _protocol = protocol,
3855
_baseUri = uri,
3956
_httpClient = httpClient,
4057
_authenticator = authenticator,
4158
_clientType = clientType ?? _defaultClientType,
4259
_logger = logger ?? Logger('Celest.Cloud');
4360

61+
CelestCloud.http(
62+
Uri uri, {
63+
Authenticator authenticator = const Authenticator.static(),
64+
ClientType? clientType,
65+
Logger? logger,
66+
http.Client? httpClient,
67+
}) : this._(
68+
uri: uri,
69+
authenticator: authenticator,
70+
protocol: CloudProtocolHttp(
71+
uri: uri,
72+
httpClient: httpClient,
73+
authenticator: authenticator,
74+
logger: logger,
75+
),
76+
clientType: clientType,
77+
logger: logger,
78+
httpClient: httpClient,
79+
);
80+
81+
/// Creates a gRPC interface for Celest Cloud.
82+
///
83+
/// [address] must be either a [Uri] or [InternetAddress].
84+
CelestCloud.grpc(
85+
Object address, {
86+
Authenticator authenticator = const Authenticator.static(),
87+
ClientType? clientType,
88+
String? userAgent,
89+
Logger? logger,
90+
http.Client? httpClient,
91+
}) : this._(
92+
uri: address is Uri ? address : null,
93+
authenticator: authenticator,
94+
protocol: CloudProtocolGrpc(
95+
host: address,
96+
userAgent: userAgent,
97+
authenticator: authenticator,
98+
logger: logger,
99+
),
100+
clientType: clientType,
101+
logger: logger,
102+
httpClient: httpClient,
103+
);
104+
44105
static final _defaultClientType = kIsWeb
45106
? ClientType.WEB
46107
: os.isAndroid
@@ -63,7 +124,7 @@ class CelestCloud {
63124
ProjectEnvironment(),
64125
]);
65126

66-
final Uri _baseUri;
127+
final Uri? _baseUri;
67128
final http.Client? _httpClient;
68129
final Authenticator _authenticator;
69130

@@ -72,7 +133,7 @@ class CelestCloud {
72133
final Logger _logger;
73134

74135
late final CloudProtocolHttp _httpProtocol = CloudProtocolHttp(
75-
uri: _baseUri,
136+
uri: _baseUri!,
76137
authenticator: _authenticator,
77138
httpClient: _httpClient,
78139
logger: _logger,

packages/celest_cloud/lib/src/cloud/cloud_protocol.grpc.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,33 @@ import 'package:logging/logging.dart';
1818

1919
final class CloudProtocolGrpc implements CloudProtocol {
2020
CloudProtocolGrpc({
21-
required Uri uri,
21+
required Object host,
2222
required Authenticator authenticator,
2323
String? userAgent,
2424
Logger? logger,
2525
}) : _channel = AuthenticatingGrpcChannel(
26-
uri.host,
26+
switch (host) {
27+
final Uri uri => uri.host,
28+
_ => host,
29+
},
2730
authenticator: authenticator,
2831
options: ChannelOptions(
29-
credentials: uri.scheme == 'http'
30-
? const ChannelCredentials.insecure()
31-
: const ChannelCredentials.secure(),
32+
credentials: switch (host) {
33+
Uri(scheme: 'https') => const ChannelCredentials.secure(),
34+
_ => const ChannelCredentials.insecure(),
35+
},
3236
userAgent: userAgent ?? 'celest/dart',
3337
),
34-
port: uri.hasPort
35-
? uri.port
36-
: switch (uri.scheme) {
37-
'http' => 80,
38-
'https' => 443,
39-
_ => throw Exception('Unsupported scheme: ${uri.scheme}'),
40-
},
38+
port: switch (host) {
39+
final Uri uri => uri.hasPort
40+
? uri.port
41+
: switch (uri.scheme) {
42+
'http' => 80,
43+
'https' => 443,
44+
_ => throw Exception('Unsupported scheme: ${uri.scheme}'),
45+
},
46+
_ => 50051,
47+
},
4148
),
4249
_interceptors = [
4350
RevokingGrpcInterceptor(

0 commit comments

Comments
 (0)