Skip to main content

rs_dapi_client/transport/
tonic_channel.rs

1use super::TransportError;
2use crate::{request_settings::AppliedRequestSettings, Uri};
3use dapi_grpc::core::v0::core_client::CoreClient;
4use dapi_grpc::platform::v0::platform_client::PlatformClient;
5use dapi_grpc::tonic::transport::{Certificate, Channel, ClientTlsConfig};
6
7/// Platform Client using gRPC transport.
8pub type PlatformGrpcClient = PlatformClient<Channel>;
9/// Core Client using gRPC transport.
10pub type CoreGrpcClient = CoreClient<Channel>;
11
12/// backon::Sleeper
13// #[derive(Default, Clone, Debug)]
14pub type TokioBackonSleeper = backon::TokioSleeper;
15
16/// Create channel (connection) for gRPC transport.
17pub fn create_channel(
18    uri: Uri,
19    settings: Option<&AppliedRequestSettings>,
20) -> Result<Channel, TransportError> {
21    let host = uri.host().expect("Failed to get host from URI").to_string();
22
23    let mut builder = Channel::builder(uri);
24
25    // Start with webpki roots (bundled Mozilla certificates) which work on all platforms
26    // Try to add native roots only on platforms where they're available (not iOS)
27    let mut tls_config = ClientTlsConfig::new()
28        .with_webpki_roots()
29        .assume_http2(true);
30
31    // Try to add native roots - this may fail on iOS/Android, which is fine since we have webpki roots
32    #[cfg(not(any(
33        target_os = "ios",
34        target_os = "tvos",
35        target_os = "watchos",
36        target_os = "android"
37    )))]
38    {
39        tls_config = tls_config.with_native_roots();
40    }
41
42    if let Some(settings) = settings {
43        if let Some(timeout) = settings.connect_timeout {
44            builder = builder.connect_timeout(timeout);
45        }
46
47        if let Some(pem) = settings.ca_certificate.as_ref() {
48            let cert = Certificate::from_pem(pem);
49            tls_config = tls_config.ca_certificate(cert).domain_name(host);
50        };
51    }
52
53    builder = builder
54        .tls_config(tls_config)
55        .expect("Failed to set TLS config");
56
57    Ok(builder.connect_lazy())
58}