sentry_core/transport/options.rs
1//! Includes the [`TransportOptions`] struct.
2
3use std::borrow::Cow;
4
5use sentry_types::Dsn;
6
7#[cfg(feature = "client")]
8use crate::client_report::Recorder as ClientReportRecorder;
9use crate::ClientOptions;
10
11/// Options for a transport.
12#[derive(Debug)]
13#[must_use]
14#[non_exhaustive]
15pub struct TransportOptions {
16 /// The transport's Sentry DSN.
17 pub dsn: Dsn,
18 /// The user agent sent with transport requests.
19 pub user_agent: Cow<'static, str>,
20 /// An optional HTTP proxy to use.
21 pub http_proxy: Option<Cow<'static, str>>,
22 /// An optional HTTPS proxy to use.
23 pub https_proxy: Option<Cow<'static, str>>,
24 /// Whether TLS certificate validation should be disabled.
25 pub accept_invalid_certs: bool,
26 /// A handle for recording lost Sentry data.
27 #[cfg(feature = "client")]
28 pub client_report_recorder: ClientReportRecorder,
29}
30
31impl TransportOptions {
32 /// Try to convert a [`&ClientOptions`](ClientOptions) to a [`TransportOptions`] by extracting
33 /// the relevant fields from the `ClientOptions`.
34 ///
35 /// This method is provided so that code which expects [`TransportOptions`] can be
36 /// backwards-compatible with older code, which provides `ClientOptions`.
37 ///
38 /// Returns [`None`] if `options.dsn` is `None`, `Some(_)` otherwise.
39 pub fn try_from_client_options(options: &ClientOptions) -> Option<Self> {
40 let ClientOptions {
41 dsn,
42 http_proxy,
43 https_proxy,
44 accept_invalid_certs,
45 user_agent,
46 ..
47 } = options;
48
49 dsn.as_ref().cloned().map(|dsn| Self {
50 dsn,
51 user_agent: user_agent.clone(),
52 http_proxy: http_proxy.clone(),
53 https_proxy: https_proxy.clone(),
54 accept_invalid_certs: *accept_invalid_certs,
55 #[cfg(feature = "client")]
56 client_report_recorder: ClientReportRecorder::new_no_op(),
57 })
58 }
59
60 /// Converts these [`TransportOptions`] into [`ClientOptions`].
61 ///
62 /// This method is provided for backwards-compatibility with custom transports which cannot
63 /// be contructed from [`TransportOptions`] because they expect [`ClientOptions`].
64 ///
65 /// Any fields on [`ClientOptions`] which are not present in [`TransportOptions`] will be
66 /// set to their default values.
67 pub(crate) fn into_client_options(self) -> ClientOptions {
68 let Self {
69 dsn,
70 user_agent,
71 http_proxy,
72 https_proxy,
73 accept_invalid_certs,
74 #[cfg(feature = "client")]
75 client_report_recorder: _,
76 } = self;
77
78 let dsn = Some(dsn);
79
80 ClientOptions {
81 dsn,
82 user_agent,
83 http_proxy,
84 https_proxy,
85 accept_invalid_certs,
86 ..Default::default()
87 }
88 }
89}