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, which is fine since we have webpki roots
32    #[cfg(not(any(target_os = "ios", target_os = "tvos", target_os = "watchos")))]
33    {
34        tls_config = tls_config.with_native_roots();
35    }
36
37    if let Some(settings) = settings {
38        if let Some(timeout) = settings.connect_timeout {
39            builder = builder.connect_timeout(timeout);
40        }
41
42        if let Some(pem) = settings.ca_certificate.as_ref() {
43            let cert = Certificate::from_pem(pem);
44            tls_config = tls_config.ca_certificate(cert).domain_name(host);
45        };
46    }
47
48    builder = builder
49        .tls_config(tls_config)
50        .expect("Failed to set TLS config");
51
52    Ok(builder.connect_lazy())
53}