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(
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}