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};
6use std::time::Duration;
7
8pub type PlatformGrpcClient = PlatformClient<Channel>;
10pub type CoreGrpcClient = CoreClient<Channel>;
12
13pub type TokioBackonSleeper = backon::TokioSleeper;
16
17const HTTP2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(15);
19const HTTP2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(10);
21
22pub fn create_channel(
24 uri: Uri,
25 settings: Option<&AppliedRequestSettings>,
26) -> Result<Channel, TransportError> {
27 let host = uri.host().expect("Failed to get host from URI").to_string();
28
29 let mut builder = Channel::builder(uri);
30
31 let mut tls_config = ClientTlsConfig::new()
34 .with_webpki_roots()
35 .assume_http2(true);
36
37 #[cfg(not(any(
39 target_os = "ios",
40 target_os = "tvos",
41 target_os = "watchos",
42 target_os = "android"
43 )))]
44 {
45 tls_config = tls_config.with_native_roots();
46 }
47
48 if let Some(settings) = settings {
49 if let Some(timeout) = settings.connect_timeout {
50 builder = builder.connect_timeout(timeout);
51 }
52
53 if let Some(pem) = settings.ca_certificate.as_ref() {
54 let cert = Certificate::from_pem(pem);
55 tls_config = tls_config.ca_certificate(cert).domain_name(host);
56 };
57 }
58
59 builder = builder
64 .http2_keep_alive_interval(HTTP2_KEEP_ALIVE_INTERVAL)
65 .keep_alive_timeout(HTTP2_KEEP_ALIVE_TIMEOUT)
66 .keep_alive_while_idle(false);
67
68 builder = builder
69 .tls_config(tls_config)
70 .expect("Failed to set TLS config");
71
72 Ok(builder.connect_lazy())
73}