rs_dapi_client/transport/
tonic_channel.rs1use 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
7pub type PlatformGrpcClient = PlatformClient<Channel>;
9pub type CoreGrpcClient = CoreClient<Channel>;
11
12pub type TokioBackonSleeper = backon::TokioSleeper;
15
16pub 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 let mut tls_config = ClientTlsConfig::new()
28 .with_webpki_roots()
29 .assume_http2(true);
30
31 #[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}